为ASP网站引用WCF后台

编码环境: 

系统:Win8   平台:Visual Studio 10   框架:.Net Framework4.0    WCF后台:IIS 8.5


步骤:

1、打开VS,新建一个ASP.NET Web应用程序,取名为WCFClient。

为ASP网站引用WCF后台_第1张图片


2、选中WCFClient项目,右键菜单——>添加服务引用。

为ASP网站引用WCF后台_第2张图片

3、添加Web窗体项,命名为WcfTest.aspx。

为ASP网站引用WCF后台_第3张图片


4、WcfTest.aspx页面代码:

<span style="font-family:System;font-size:18px;"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WcfTest.aspx.cs" Inherits="WCTClient.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div>
    <form id="form2" runat="server">
     <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
     <asp:Button ID="btnSubmit" runat="server" Text="测试WCF服务" OnClick="btnClick" />
     </form>
    </div>
</body>
</html></span>


5、WcfTest.aspx.cs 文件代码:

<span style="font-family:System;font-size:18px;">using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCTClient.UserClient;

namespace WCTClient
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnClick(object sender, EventArgs e)
         {
             UserClient.UserClient user = new UserClient.UserClient("BasicHttpBinding_IUser"); //有多个终端节点的情况下,需制定需访问的节点。
             string result = user.ShowName(this.txtName.Text);
             Response.Write(result);
         }
    }
}</span>

6、选中WcfTest.aspx文件,单击右键菜单,将WcfTest.aspx设为起始页。

7、按F5运行网站,默认浏览器弹出起始页面:



8、测试后台方法,在文本框中输入文字,会有文字返回至网页:



你可能感兴趣的:(asp.net,WCF)