WCF服务在IIS7.0中用WAS托管

WAS托管:Windows激活服务(WAS)是一个系统服务,是IIS7的一部分,也可以独立的安装与配置。IIS与WAS的主要区别在于WAS并不局限使用HTTP,它支持所有可用的WCF传输协议、端口与队列,支持的协议包括:HTTP、TCP、IPC、MSMQ。

      WAS提供了大量基于自托管的强大功能,包括应用程序池、回收机制、空闲时间管理(Idle Time Mannagement)、身份管理(Identity Management) 以及隔离(Isolation);宿主进程可以根据情况选择使用这些功能。

      下面我们通过一个DEMO来介绍WAS托管(net.tcp绑定)。

      开发环境:Visual Studio 2010 + Net Framework 4.0

      运行环境:Windows Server 2008 R2 + IIS7 + WAS

      1、创建一个WCf Service Library,主要代码如下:

//接口代码 [ServiceContract(Namespace = "http://schemas.xinhaijulan.com/demos/WASHost")] public interface IWASHostService { [OperationContract] string HelloWCF(string str); } //实现类代码 public class WASHostService : IWASHostService { public string HelloWCF(string str) { return string.Format("Get message from client is : {0}", str); } }

      2、创建一个ASP.NET空白网站,添加Service Library引用,并添加服务文件文件[WASHostService.svc],代码如下:

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceLibrary.WASHostService" %>

      3、添加Web.config文件如下:

<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="ServiceLibrary.WASHostService" behaviorConfiguration="MetaDataBehavior"> <endpoint address="" binding="netTcpBinding" contract="ServiceLibrary.IWASHostService" bindingConfiguration="myNetTcp" /> <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" bindingConfiguration="mexTcp" /> </service> </services> <bindings> <netTcpBinding> <binding name="myNetTcp" portSharingEnabled="true"> </binding> <binding name="mexTcp" portSharingEnabled="true"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="MetaDataBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> </configuration>

      4、编译网站,success,然后在命令行输入:inetmgr,打开IIS管理器,在Default Web Site中添加应用程序:WASHostService,目录指刚才创建的WebSite,并且指定应用程序池,至此,网站配置基本结束,然而我们尚未配置WAS。

      5、安装WAS与WCF激活【HTTP激活和非HTTP激活】:打开服务管理器->添加功能->选择.NET FrameWork 3.5.1和Windows 进程激活服务,如下图:WCF服务在IIS7.0中用WAS托管_microsoftWCF服务在IIS7.0中用WAS托管_wcf_02

然后点击下一步,然后安装。因我已经安装,所以您看到的界面是已经安装的界面,所以下一步和安装按钮不可用。

      6、在安装完成后,我们可以在服务里面看到NetTcpActivator、NetPipeActivator、NetTcpPortSharing和WAS服务已经启动,如下图:WCF服务在IIS7.0中用WAS托管_binding_03

      7、至此,WAS与WCF激活【HTTP激活和非HTTP激活】安装完毕并且成功,下面我们将看到在Default Web Site中会自动绑定多个协议,命令行输入inetmgr,打开IIS管理器,右键点击Drfaul Web Site,点击绑定,如下图:

WCF服务在IIS7.0中用WAS托管_service_04WCF服务在IIS7.0中用WAS托管_service_05

如果点击绑定后没有看到net.tcp等非http协议,则可以通过点击右面的添加来添加非http协议绑定,如下图:WCF服务在IIS7.0中用WAS托管_iis_06

以上为添加非http绑定,除此还需要进行启动net.tcp绑定:点击WASHostService->管理应用程序->高级设置,启动net.tcp绑定,如下图:WCF服务在IIS7.0中用WAS托管_microsoft_07

除了通过IIS管理器添加非http绑定,还可以通过命令行的方式添加非http绑定。

      8、命令行添加绑定:在命令行输入cmd 进入到:C:\Windows\System32\inetsrv>目录,然后输入appcmd,系统会给出appcmd的帮助信息,然后输入appcmd list site。

      8.1、设置端点tcp绑定,输入:appcmd set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']。

      8.2、启用tcp绑定,输入appcmd list app,根据列出的应用程序,再输入:appcmd set app "Default Web Site/WASHostService" /enabledProtocols:http,net.tcp。

      命令行添加绑定过程如下:

Microsoft Windows [版本 6.1.7600] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。 C:\Users\Administrator>cd\ C:\>cd windows C:\Windows>cd system32 C:\Windows\System32>cd inetsrv C:\Windows\System32\inetsrv>appcmd 通用 IIS 命令行管理工具。 APPCMD (命令) (对象类型) <标识符> </参数1:值1 ...> 支持的对象类型: SITE 虚拟站点的管理 APP 管理应用程序 VDIR 管理虚拟目录 APPPOOL 管理应用程序池 CONFIG 管理常规配置节 WP 管理工作进程 REQUEST 管理 HTTP 请求 MODULE 管理服务器模块 BACKUP 管理服务器配置备份 TRACE 使用失败请求跟踪日志 (要列出每个对象支持的命令,请使用 /?,例如“appcmd.exe site /?”) 常用参数: /? 显示上下文相关帮助消息。 /text<:value> 以文本格式(默认)生成输出。 /text:* 在详细信息视图中显示所有对象属性。 /text:<attribute> 显示每个对象的 指定特性的值。 /xml 以 XML 格式生成输出。 使用此参数生成的输出可发送至 以 /in 模式运行的其他命令。 /in or - 自标准输入读取和操作 XML 输入。 使用此参数可操作运行于 /xml 模式的 其他命令生成的输入。 /config<:*> 显示所显示对象的配置。 /config:* 还包括继承的配置。 /metadata 在显示配置时显示配置元数据。 /commit 设置用于保存配置更改的配置路径。 可以指定具体的配置路径、“site”、 “app”、“parent”或“url”以保存到该命令正在编辑的 路径的适当部分,也可以设置为对应的配置级别 的“apphost”、“webroot”或“machine”。 /debug 显示命令执行的调试信息。 -使用“!”以转义与常用参数同名的参数, 例如“/!debug:value”设置一个名为“debug”的配置属性。 C:\Windows\System32\inetsrv>appcmd list site SITE "demo" (id:2,bindings:http/*:80:www.demo.com,state:Started) SITE "HelloWCF" (id:3,bindings:http/*:80:www.hellowcf.com,state:Started) SITE "Default Web Site" (id:1,bindings:http/*:80:www.washost.com,net.tcp/808:*,n et.pipe/*,net.msmq/localhost,msmq.formatname/localhost,state:Started) C:\Windows\System32\inetsrv>appcmd list app APP "demo/" (applicationPool:Demo) APP "HelloWCF/" (applicationPool:HelloWCF) APP "Default Web Site/" (applicationPool:Default Web Site) APP "Default Web Site/MSMQ" (applicationPool:Default Web Site) APP "Default Web Site/WASHostService" (applicationPool:Default Web Site)

      9、至此WAS托管服务端设置完毕,我们通过在浏览器中打开.svc文件测试元数据是否可见,输入:http://www.washost.com/WASHostService/WASHostService.svc

(www.washost .com 在host文件中添加项:127.0.0.1 www.washost.com)可以看到如下信息:WCF服务在IIS7.0中用WAS托管_service_08

此元数据只所以可见,是因为我在配置文件中设置了【<serviceMetadata httpGetEnabled="true" />】和【<endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" bindingConfiguration="mexTcp" />】,请参考第三步的配置文件。

      10、创建Client项目,添加Service引用,如下图:

WCF服务在IIS7.0中用WAS托管_windows_09

完成后,在Program类中添加代码,主要代码如下:

class Program { static void Main(string[] args) { using (Server.WASHostServiceClient client = new Server.WASHostServiceClient()) { Console.WriteLine("--------WAS NET.TCP BEGIN--------"); Console.WriteLine(client.HelloWCF("Hello WCF")); Console.WriteLine("--------WAS NET.TCP END--------"); Console.ReadLine(); } Console.ReadLine(); } }

此时,我们可以看到Client项目中,系统自动生成了一个app.config文件,主要代码如下:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_IWASHostService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://cs/WASHostService/WASHostService.svc" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IWASHostService" contract="Server.IWASHostService" name="NetTcpBinding_IWASHostService"> <identity> <servicePrincipalName value="host/CS" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>

      11、运行客户端,输入如下图:WCF服务在IIS7.0中用WAS托管_microsoft_10

      12、如果在调用服务的过程中出现Error:

An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll Additional information: The message could not be dispatched because the service at the endpoint address 'net.tcp://cs/WASHostService/WASHostService.svc' is unavailable for the protocol of the address.

则请通过重新安装ServiceModel解决:【C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ServiceModelReg.exe -r】,个人理解是ServiceModel安装的版本的问题,如下:

C:\Users\Administrator>cd\ C:\>cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ServiceModelReg -r Microsoft (R) WCF/WF registration tool version 4.0.0.0 Copyright (c) Microsoft Corporation. All rights reserved. Administration utility that manages the installation and uninstallation of WCF and WF components on a single machine. [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [SC] ChangeServiceConfig2 成功 [Warning]The HTTP namespace reservation already exists.

如有其它问题,则请通过【telnet ip 端口号】看服务端是否开启此端口。

      至此,托管(Hosting)-WAS托管(net.tcp)介绍完毕。