zoukankan      html  css  js  c++  java
  • WCF中常见的几种Host,承载WCF服务的方法详解

    1:写在前面

    我们都知道WCF在运行的时候必须自己提供宿主来承载服务。WCF 本身没有附带宿主,而是提供了一个 ServiceHost 的类,该类允许您在自己的应用程序中host WCF 服务。然后调用 ServiceHost 的 Open 方法即可。我们知道WCF是针对SOA的一套技术.对于SOA而言,我们必须确保服务能够正常运行,平稳的运行,所以此时如何host我们的服务,用什么来Host我们的服务是很重要的,所以我们要为我们的应用程序选择一个合适的Host方式是很有必要的.

    2:常见的几种Host

    使用Visual Studio.NET可以创建以下几种不同类型的托管应用程序:

    WinForms 应用程序

    控制台应用程序

    Windows 服务

    承载于 Internet 信息服务 (IIS) 中的 Web 应用程序

    下面我会以一个Demo为例,来介绍这几种常用的host.

    3:Hoster特征

    在选择应用程序类型时,必须考虑某些特定要求.ServiceHost 必须被实例化才能提供运行服务所需的承载环境。控制台应用程序和 WinForms 应用程序通常运行在用户桌面计算机上。在桌面上是可见的,它们可以承载您的服务,安全性较差.因此并不适用于企业的宿主。因为我们要让我们的企业服务能够支持更大更多的服务,在使用控制台应用程序和 WinForms 应用程序作为host的时候,显然不适合,在这种体系结构中,。这些适用于企业的宿主通常能够满足诸如高可用性的要求。因此,我们不能将控制台或 WinForms 应用程序做为适用于企业的宿主。唯一可行的就是在 IIS 上承载服务或将其Host到Windows 服务。事实上在项目开发过程中我们一般都用windows applicatin或console application做为host.这样有利于调试和测试,而在项目部署的时候,我们一般会选择IIS或windows service作为host. IIS有很多优点,他本身提供了很多功能,

    4:Soultion结构图

    hostSoultion

    在这个Soultion主要介绍了Console Applicatin,Windows service,IIS三种Host,因为Windows Application和Console Application没有多大区别,暂不作介绍

    首先我们介绍在Windows Services中Host WCF service.Windows service 是由操作系统管理的进程,在.net环境中已经提供了这样的模板,以Windows services作为host是一个很不错的选择:

    Windows services Host对应用solution中的WCFWindowsServicesHost.

    Services Contract和Services

    [ServiceContract] public interface ITest {     [OperationContract]     double add(double x, double y); }

    public class Test:ITest {    

          public double add(double x, double y)       {           return x + y;       }

    }

    5:Windows services Host

    首先我们建立一个windows service 项目.

    ws

    windows services项目中包含两个文件:service1.cs 文件和 program.cs 文件。其中 service1.cs 文件包含服务实现,而 program.cs 文件则用于实例化并实质上承载 Windows 服务。要在 Windows 服务内部承载 WCF 服务,只需执行 Windows service的 Start() 方法和 Stop() 方法

         ServiceHost host;       public WindowServiceTest()       {           InitializeComponent();       }

          protected override void OnStart(string[] args)       {           host = new ServiceHost(typeof(Test));           host.Open();         
          }

          protected override void OnStop()       {           host.Close();       } 配置文件:

    <system.serviceModel> <behaviors>     <endpointBehaviors >       <behavior name="ConsoleApplicationHost">       </behavior>     </endpointBehaviors> </behaviors> <bindings>     <basicHttpBinding>       <binding name="basicBinding"></binding>     </basicHttpBinding> </bindings> <services>     <service name="WCFHostServices.Test">       <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="WCFHostContract.ITest" behaviorConfiguration="ConsoleApplicationHost">       </endpoint>       <host>         <baseAddresses>           <add baseAddress="http://localhost/WindowsServiceTest"/>         </baseAddresses>       </host>     </service> </services>    </system.serviceModel>

    事实我们可以看到编写host在windows services上的WCF服务很简单,windows services固然有很多好处,比如说随操作系统一起启动,无须人为的开启,而且不限定你的绑定技术,如果我们使用IIS作为host的话,我们就得使用Http.那么我们的binding也会受到一定受的限制,话说windows service 有这么多好处,当然也有不好的地方,比如安装windows service.我们必须通过Installutil.exe 工具或通过安装包中的自定义操作来安装服务。这个工具在安装SDK后会自带的,InstallUtil的使用也很简单,下面我们以自定义安装服务为例

    serviceshost

    在 Windows 服务项目中打开 Service 类的设计器视图。在右击窗口中,单击“添加安装程序”,这样我们就会添加了一个名为ProjectInstaller的组件,在项目中双击这个文件,有一个servicesinstall1的组件,我们右击可以修改他的名字,起动类型等,

    windowsservieinstall

    为了创建安装 Windows 服务的安装程序,我们需要向solution中添加安装程序和部署项目(windowsserviceSetup)。以下步骤描述了如何向解决方案添加安装程序和部署项目:

    新建一个安装项目:

    setup

    1:右键单击安装项目,指向“添加”,然后选择“项目输出”,选择windows service appliation...选择主输出,

    2:右键单击安装项目,指向 view->custome action.

    customeaction

    3:右击Customer action,将主输出的文件加入custome action中

    4:生成项目,并安装,这样我们就可以启动我们的servcies里.我们可以打开windows services管理工具查看我们安装后的servcie

    testservice

    6:IIS Host

    若要在 IIS 中host WCF Services,需要有一个扩展名为 .svc 的文件。事实上在我们项目模板中已经提供了这样的模板.该文件将服务与其实现相关联,并且是 IIS 自动创建 ServiceHost 的手段。IIS 将接管服务与 ServiceHost 之间的交互,不必再由您自己实例化和启动 ServiceHost。.svc 文件的第一行包含一条夹在 ASP.NET <% Page %> 指令内的指令,用于指示文件指向哪个服务。

    <%@ ServiceHost Language="C#" Debug="true" Service="WCFIISHost.Test" CodeBehind="Test.svc.cs" %>

    IIS config:

    <system.serviceModel> <services>     <service behaviorConfiguration="WCFIISHost.Service1Behavior"       name="WCFIISHost.Test">       <endpoint address="" binding="wsHttpBinding" contract="WCFIISHost.ITest">         <identity>           <dns value="localhost" />         </identity>       </endpoint>       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />     </service> </services> <behaviors>     <serviceBehaviors>       <behavior name="WCFIISHost.Service1Behavior">         <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->         <serviceMetadata httpGetEnabled="true"/>         <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->         <serviceDebug includeExceptionDetailInFaults="false"/>       </behavior>     </serviceBehaviors> </behaviors> </system.serviceModel>

    在 IIS 中host WCF services的默认行为是该 IIS 控制 ServiceHost 的实例化。在Client端我们直接引用地址,默认会在我们的客户端生成代理文件,无须我们使用svcutil工具来生成客户端的代理.

    7:Console Application Host

    这种方法再简单不过了,因为大多数时候在测试项目的时候,我们都会用这样的host,

    Config:

    <system.serviceModel> <behaviors>     <endpointBehaviors >       <behavior name="ConsoleApplicationHost">       </behavior>     </endpointBehaviors> </behaviors> <bindings>     <basicHttpBinding>       <binding name="basicBinding"></binding>     </basicHttpBinding> </bindings> <services>     <service name="WCFHostServices.Test">       <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="WCFHostContract.ITest" behaviorConfiguration="ConsoleApplicationHost">       </endpoint>       <host>         <baseAddresses>           <add baseAddress="http://localhost/ConsoleTest"/>         </baseAddresses>       </host>     </service> </services>    </system.serviceModel>

    启动Host:

    using (ServiceHost host = new ServiceHost(typeof(Test)))            {                host.Open();                Console.WriteLine("Services has begun listenting");                Console.ReadKey();            }

    Client:

    public class CoClient : ClientBase<WCFHostContract.ITest>, ITest {     public double add(double x, double y)     {         return this.Channel.add(x, y);     } }

  • 相关阅读:
    与众不同 windows phone (12) Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任务)
    直观理解图像的傅里叶变换
    简明Python3教程 13.面向对象编程
    简明Python3教程 4.安装
    简明Python3教程 8.控制流
    简明Python3教程 11.数据结构
    简明Python3教程 16.标准库
    简明Python3教程 12.问题解决
    简明Python3教程 9.函数
    简明Python3教程 2.序言
  • 原文地址:https://www.cnblogs.com/wujy/p/2921695.html
Copyright © 2011-2022 走看看