标签:
我们对宿主的概念、环境、特点做了文字性的介绍和概括,接下来我们将通过实例对这几种寄宿方式进行介绍。为了更好的说明各寄宿环境特点,本实例采用Http和net.tcp两种服务通讯方式,同时寄宿在不同的宿主中。程序结构如下:
服务契约的接口和实现代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace Service{ [ServiceContract] public interface IServiceCalculator { [OperationContract] double Add(double n1, double n2); }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace Service{ [ServiceContract] public interface IServiceMessage { [OperationContract] string ReturnMessage(); }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{ public class ServiceCalculator:IServiceCalculator { public double Add(double n1, double n2) { return n1 + n2; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{ public class ServiceMessage:IServiceMessage { public string ReturnMessage() { return "调用服务计算结果如下"; } }}
在 IIS 中承载 WCF 服务和WAS 中承载 WCF 服务
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceCalculator" %>
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceMessage" %>
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" /> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
2. 寄宿服务
点击IIS菜单的应用程序池,找到WCFHost程序池,将.net framework版本设置为v4.0,托管管道模式设置为集成
在浏览器中输入http://localhost:1234/ServiceMessage.svc可以看到服务发布成功
这是因为ServiceCalculator.svc启用的是net.tcp通讯,而在IIS中启用net.tcp通讯就必须依靠Windows 进程激活服务(也称为 WAS)
在控制面板->程序和功能->打开或关闭windows功能勾选以下几个功能,安装WCF 激活组件
配置承载服务的WCFHost网站,添加net.tcp通讯。
点击网站的高级设置,在已启用的协议后追加net.tcp协议
3. 客户端验证服务
在托管应用程序中承载 WCF 服务
1. 完成AppHost代码
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:2234/ServiceMessage/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1235/ServiceCalculator/"/> <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/> </baseAddresses> </host> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" > <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true" > <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Service;using System.ServiceModel;namespace AppHost{ class Program { static void Main(string[] args) { try { ServiceHost MessageHost = new ServiceHost(typeof(ServiceMessage)); ServiceHost CalculatorHost = new ServiceHost(typeof(ServiceCalculator)); MessageHost.Open(); CalculatorHost.Open(); Console.WriteLine("服务已经启动。。。"); Console.ReadLine(); MessageHost.Close(); CalculatorHost.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } } }}
2. 寄宿服务
3. 客户端验证服务
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/
在托管 Windows 服务中承载 WCF 服务
添加配置文件App.config代码,代码如下:
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <services> <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:2234/ServiceMessage/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1235/ServiceCalculator/"/> <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/> </baseAddresses> </host> <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" > <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="PortSharingBinding" portSharingEnabled="true" > <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
using System.ServiceProcess;using Service;using System.ServiceModel;namespace NTHost{ public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } ServiceHost MessageHost = null; ServiceHost CalculatorHost = null; protected override void OnStart(string[] args) { MessageHost = new ServiceHost(typeof(ServiceMessage)); CalculatorHost = new ServiceHost(typeof(ServiceCalculator)); MessageHost.Open(); CalculatorHost.Open(); } protected override void OnStop() { MessageHost.Close(); CalculatorHost.Close(); MessageHost = null; CalculatorHost = null; } }}
2. 寄宿服务
Cd C:\Windows\Microsoft.NET\Framework\v4.0.30319,回车后输入installutil.exe 程序生成的bin目录绝对地址\NTHost.exe –i,回车后安装服务程序,程序注册成功后启动服务。
在开始菜单输入services.msc命令,打开服务管理程序将NTServiceHost服务设置为启动
3. 客户端验证服务
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/
标签:
原文地址:http://www.cnblogs.com/anyben/p/4409595.html