WCF_IIS宿主服务

来源:互联网 发布:淘宝上传3c认证入口 编辑:程序博客网 时间:2024/05/29 19:39

IIS宿主WCF服务是将服务发布并部署到IIS站点上


IIS宿主WCF服务有如下步骤:

  • 添加类库项目,编写WCF服务
  • 添加web项目,配置服务信息
  • 发布web项目,部署IIS站点
  • IIS启动服务
  • 服务客户端调用(三种方式)

添加类库项目

项目中添加必要的引用 (参考:自宿主服务)然后添加一个wcf服务

添加服务接口及接口实现

具体代码可参考上一篇,服务编写是一样的。参考:自宿主服务


添加一个web项目

添加必要引用以及上面类库项目的引用。

在项目中添加一个wcf服务,命名为HelloService

删除接口类IHelloService,删除实现类中包含的HelloService.svc.cs类

再次双击HelloService.svc,可以看到一段配置信息,删除codebehind,修改Service对应的位置。

接下来在web.config中编写服务的通信配置信息

因为IIS宿主服务,服务的地址是有IIS部署时决定,所以配置信息不需要配置服务地址信息

http协议绑定服务的配置文件内容如下;

<system.serviceModel>    <services>      <service name="WCFIISHost.HelloService" behaviorConfiguration="HttpGetEnable">        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoSecurity" contract="WCFIISHost.IHelloService">        </endpoint>      </service>    </services>    <bindings>      <basicHttpBinding>        <binding name="NoSecurity" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>          <security mode="None" />        </binding>      </basicHttpBinding>    </bindings>    <behaviors>      <serviceBehaviors>        <behavior name="HttpGetEnable">          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>        <behavior name="">          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>      </serviceBehaviors>    </behaviors>    <!--multipleSiteBindingsEnabled 允许多地址-->    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"        multipleSiteBindingsEnabled="true" />  </system.serviceModel>

net.tcp协议绑定服务的配置信息如下

  <system.serviceModel>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"      multipleSiteBindingsEnabled="true" />    <services>      <service name="WCFNetTCPHost.Service.UserInfoService" behaviorConfiguration="HttpGetEnable">        <endpoint address="" binding="netTcpBinding" bindingConfiguration="NoSecurity" contract="WCFNetTCPHost.Service.IUserInfoService" />        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />      </service>    </services>    <behaviors>      <serviceBehaviors>        <behavior name="HttpGetEnable">          <serviceMetadata />          <serviceDebug includeExceptionDetailInFaults="true" />        </behavior>        <behavior name="">          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>      </serviceBehaviors>    </behaviors>    <bindings>      <netTcpBinding>        <binding name="NoSecurity" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>          <security mode="None"></security>        </binding>      </netTcpBinding>    </bindings>  </system.serviceModel>

需要注意,其中的终结点还配置元数据的通信信息。

    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />


配置完成之后,就可以在项目中进行测试服务

设置HelloService.svc为起始页,然后点击调试web项目,就可以启动wcf测试客户端进行该服务测试。

具体可见实用小工具

HTTP协议绑定服务部署IIS

就是一般的web服务部署,发布添加站点就可以了,不做详细介绍。

NET.TCP协议绑定服务部署IIS

同HTTP协议,但是需要注意两点

1.站点启用协议net.tcp

高级设置-->启用协议 
如果没有net.tcp协议 ,用逗号分隔,添加该协议


2.绑定协议是否添加net.tcp协议

右键-->编辑绑定
如果没有绑定net.tcp,需要添加上,并设置绑定信息为808:*


下面详细介绍一下服务的客户端调用

添加一个客户端的控制台项目。

方式一:通过管道工厂创建服务客户端

添加WCF必要的引用以及服务项目引用

客户端服务通信的配置:

客户端配置内容包含 客户端通信服务配置,绑定信息配置两部分

客户端的配置要与服务端配置一致:比如绑定协议类型,安全性,是否启用可靠会话以及传输方式等。

配置内容如下:

 <system.serviceModel>    <client>      <endpoint address="http://localhost:8088/HelloService.svc" name="HelloService" bindingConfiguration="HelloService_binding" binding="basicHttpBinding" contract="WCFIISHost.IHelloService">      </endpoint>    </client>    <bindings>      <basicHttpBinding>        <binding name="HelloService_binding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>          <security mode="None"></security>        </binding>      </basicHttpBinding>    </bindings>  </system.serviceModel>
然后就可以创建客户端,调用服务啦

   static void Main(string[] args)        {            //使用终结点的配置名称初始化            var factory = new ChannelFactory<IHelloService>("HelloService");            //创建客户端            var client = factory.CreateChannel();            try            {                Console.WriteLine(client.SayHello());                factory.Close();            }            catch (Exception)            {                factory.Abort();                throw;            }            Console.ReadKey();        }


需要注意,WCF客户端调用服务,不能使用Using,具体原因见:客户端不使用Using


方式二:通过代理类客户端调用

这就需要使用外部工具SvcUtil.exe工具,生成客户端代理类和配置文件。具体操作见:实用小工具

生成之后,包含到项目中,将配置文件的信息粘贴到App.config文件中,就可以调用服务啦。

注意这种方式,不需要引用服务项目

调用代码如下:

 static void Main(string[] args)        {            //自动生成的客户端代理类,且每个方法包含异步和同步两种实现            var client = new HelloServiceClient();            try            {                Console.WriteLine(client.SayHelloAsync().GetAwaiter().GetResult());                client.Close();            }            catch (Exception)            {                client.Abort();                throw;            }            Console.ReadKey();        }

方式三:添加服务引用

项目右键添加服务引用,正确写入服务地址即可。

调用方式同方式二。










原创粉丝点击