从零开始学WCF(4)--承载服务
-
在IIS中承载
- WCF可以方便的通过IIS承载。此承载模型与ASP.NET和ASP.NET Web Service使用的模型类似。
IIS承载的好处
- 可像处理其他任何类型的IIS应用程序(包括ASP.NET应用程序和ASMX)一样,部署和管理IIS中承载的WCF服务。
- IIS提供进程激活,运行状况管理和回收功能以提高承载的应用程序的可靠性。
- 像ASP.NET一样,ASP.NET中承载的WCF服务可以利用ASP.NET共享宿主模型。在此模型中,多个应用程序驻留在一个公共辅助进程中以提高服务器密度的可伸缩性。
- IIS中承载的WCF服务与ASP.NET2.0使用相同的动态编译模型,该模型简化了承载的服务的开发和部署。
- 当决定在IIS中承载WCF服务时,一定要记住IIS5.1和IIS6.0仅限于HTTP通信。
<system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true--> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
在Windows进程激活服务(WAS)中承载(tcp/ip,命名管道)
- Windows进程激活服务(WAS)管理辅助进程的激活和生存期,该辅助进程包含WCF服务的应用程序。
- WAS进程模型通过移除对HTTP的依赖性使HTTP服务器IIS6.0进程模型通用化。(可以像http一样进行其他协议的部署)
- 允许WCF服务在宿主环境中同时使用Http和非http协议(如Net.TCp),该宿主环境支持基于消息的激活并提供在给定计算机上承载大量应用程序的能力。
- 基于消息的应用程序激活和辅助进程应用程序会动态地启动和停止,以响应使用Http和非Http网络协议送达的传入工作项。
- 可靠的应用程序和辅助进程回收可以使应用程序保持良好的运行状况。
- 集中的应用程序配置和管理(利用IIS的管理工具)
- 允许应用程序利用IIS进程模型,而无需完全IIS安装的部署需求量。
<services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <!-- This endpoint is exposed at the base address provided by host: net.tcp://localhost/servicemodelsamples/service.svc --> <endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- the mex endpoint is exposed at net.tcp://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true"> <security mode="None" /> </binding> </netTcpBinding> </bindings>
在托管应用程序中承载
服务可以承载于任何.Net framework应用程序中,自承载服务是最灵活的服务选项。因为此服务部署所需的基础结构最少。但是此服务也是最不可靠的服务选项。
public static void Main() { // Create a ServiceHost for the CalculatorService type. using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService))) { // Open the ServiceHost to create listeners and start listening for messages. serviceHost.Open(); // The service can now be accessed. Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); } }
<system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/ServiceModelSamples11/service"/> </baseAddresses> </host> <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/> <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true--> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>