3、客户端
Hello.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs" Inherits="Hosting_Hello" Title="宿主Hosting(服务宿主在WindowsService)" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div> <ul> <li style="color: Red;">本例为宿主在WindowsService的示例</li> <li>宿主在IIS请参见本解决方案的ServiceHost项目</li> <li>宿主在应用程序请参见本解决方案的ServiceHost2项目</li> <li>应用程序自宿主就是把本解决方案的ServiceLib项目和ServiceHost2项目结合在一起</li> <li>宿主在Windows Activation Services(WAS),因为我没有环境,就先不写示例了</li> </ul> </div> <asp:TextBox ID="txtName" runat="server" Text="webabcd" /> <asp:Button ID="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" /> </asp:Content> |
Hello.aspx.cs using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Hosting_Hello : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSayHello_Click(object sender, EventArgs e) { var proxy = new HostingByWindowsService.HelloClient(); Page.ClientScript.RegisterStartupScript( this.GetType(), "js", string.Format("alert('{0}')", proxy.SayHello(txtName.Text)), true); proxy.Close(); } } |
Web.config
<?xml version="1.0"?> <configuration> <system.serviceModel> <client> <!--address - 服务地址--> <!--binding - 通信方式--> <!--contract - 服务契约--> <endpoint address="http://localhost:11233/ServiceHostByWindowsService/" binding="wsHttpBinding" contract="Sample.IHello" /> </client> </system.serviceModel> </configuration> |
运行结果:
启动"WCF.ServiceHostByWindowsService"服务,单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"
原文链接:http://www.cnblogs.com/webabcd/archive/2008/04/07/1139938.html
关注此文的读者还看过: