天天看点

化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)

<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引页]</a>

化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)

介绍

WCF(Windows Communication Foundation) - 宿主(Hosting):WCF服务可以宿主在IIS, Application, WAS, WindowsService。本文以宿主在WindowsService为例。

示例

1、服务

IHello.cs

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.ServiceModel; 

namespace WCF.ServiceLib.Sample 

        /// &lt;summary&gt; 

        /// IHello接口 

        /// &lt;/summary&gt; 

        [ServiceContract] 

        public interface IHello 

        { 

                /// &lt;summary&gt; 

                /// 打招呼方法 

                /// &lt;/summary&gt; 

                /// &lt;param name="name"&gt;人名&lt;/param&gt; 

                /// &lt;returns&gt;&lt;/returns&gt; 

                [OperationContract] 

                string SayHello(string name); 

        } 

}

Hello.cs

        /// Hello类 

        public class Hello : IHello 

                public string SayHello(string name) 

                { 

                        return "Hello: " + name; 

                } 

2、宿主

Hello.cs(WindowsService)

using System.ComponentModel; 

using System.Configuration; 

using System.Configuration.Install; 

using System.ServiceProcess; 

namespace WCF.ServiceHostByWindowsService.Sample 

        /// 初始化 System.Configuration.Install.Installer 类的新实例。 

        [RunInstaller(true)] 

        public class ProjectInstaller : Installer 

                private ServiceProcessInstaller process; 

                private ServiceInstaller service; 

                /// 构造函数 

                public ProjectInstaller() 

                        process = new ServiceProcessInstaller(); 

                        process.Account = ServiceAccount.LocalSystem; 

                        service = new ServiceInstaller(); 

                        service.ServiceName = "WCF.ServiceHostByWindowsService"; 

                        service.Description = "WCF服务宿主在WindowsService[webabcd测试用]"; 

                        base.Installers.Add(process); 

                        base.Installers.Add(service); 

        /// Windows服务类 

        public class WindowsService : ServiceBase 

                public ServiceHost serviceHost = null; 

                /// 主函数 

                public static void Main() 

                        ServiceBase.Run(new WindowsService()); 

                public WindowsService() 

                        base.ServiceName = "WCF.ServiceHostByWindowsService"; 

                /// 启动Windows服务 

                /// &lt;param name="args"&gt;args&lt;/param&gt; 

                protected override void OnStart(string[] args) 

                        if (serviceHost != null) 

                        { 

                                serviceHost.Close(); 

                        } 

                        // 为WCF.ServiceLib.Sample.Hello创建ServiceHost 

                        serviceHost = new ServiceHost(typeof(WCF.ServiceLib.Sample.Hello)); 

                        serviceHost.Open(); 

                        ServiceHost的几个事件(顾名思义) 

                /// 停止Windows服务 

                protected override void OnStop() 

                                serviceHost = null; 

App.config

&lt;?xml version="1.0" encoding="utf-8" ?&gt; 

&lt;configuration&gt; 

    &lt;system.serviceModel&gt; 

        &lt;services&gt; 

            &lt;!--name - 提供服务的类名--&gt; 

            &lt;!--behaviorConfiguration - 指定相关的行为配置--&gt; 

            &lt;service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior"&gt; 

                &lt;!--address - 服务地址--&gt; 

                &lt;!--binding - 通信方式--&gt; 

                &lt;!--contract - 服务契约--&gt; 

                &lt;endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Sample.IHello" /&gt; 

                &lt;!--元数据交换的endpoint--&gt; 

                &lt;!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex--&gt; 

                &lt;endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" /&gt; 

                &lt;host&gt; 

                    &lt;baseAddresses&gt; 

                        &lt;add baseAddress="http://localhost:11233/ServiceHostByWindowsService/"/&gt; 

                    &lt;/baseAddresses&gt; 

                &lt;/host&gt; 

            &lt;/service&gt; 

        &lt;/services&gt; 

        &lt;behaviors&gt; 

            &lt;serviceBehaviors&gt; 

                &lt;behavior name="SampleBehavior"&gt; 

                    &lt;serviceMetadata httpGetEnabled="True"/&gt; 

                    &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt; 

                &lt;/behavior&gt; 

            &lt;/serviceBehaviors&gt; 

        &lt;/behaviors&gt; 

    &lt;/system.serviceModel&gt; 

&lt;/configuration&gt;

3、客户端

Hello.aspx

&lt;%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs" 

        Inherits="Hosting_Hello" Title="宿主Hosting(服务宿主在WindowsService)" %&gt; 

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt; 

&lt;/asp:Content&gt; 

&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt; 

        &lt;div&gt; 

                &lt;ul&gt; 

                        &lt;li style="color: Red;"&gt;本例为宿主在WindowsService的示例&lt;/li&gt; 

                        &lt;li&gt;宿主在IIS请参见本解决方案的ServiceHost项目&lt;/li&gt; 

                        &lt;li&gt;宿主在应用程序请参见本解决方案的ServiceHost2项目&lt;/li&gt; 

                        &lt;li&gt;应用程序自宿主就是把本解决方案的ServiceLib项目和ServiceHost2项目结合在一起&lt;/li&gt; 

                        &lt;li&gt;宿主在Windows Activation Services(WAS),因为我没有环境,就先不写示例了&lt;/li&gt; 

                &lt;/ul&gt; 

        &lt;/div&gt; 

        &lt;asp:TextBox ID="txtName" runat="server" Text="webabcd" /&gt; 

        &lt;asp:Button ID="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" /&gt; 

&lt;/asp:Content&gt;

Hello.aspx.cs

using System.Collections; 

using System.Data; 

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

&lt;?xml version="1.0"?&gt; 

        &lt;client&gt; 

            &lt;!--address - 服务地址--&gt; 

            &lt;!--binding - 通信方式--&gt; 

            &lt;!--contract - 服务契约--&gt; 

            &lt;endpoint address="http://localhost:11233/ServiceHostByWindowsService/" binding="wsHttpBinding" contract="Sample.IHello" /&gt; 

        &lt;/client&gt; 

运行结果:

启动"WCF.ServiceHostByWindowsService"服务,单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"

OK

<a href="http://down.51cto.com/data/100781" target="_blank">[源码下载]</a>

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344113,如需转载请自行联系原作者

继续阅读