在WCF初探-8:WCF服务承载 (上)中,我们对宿主的概念、环境、特点做了文字性的介绍和概括,接下来我们将通过实例对这几种寄宿方式进行介绍。为了更好的说明各寄宿环境特点,本实例采用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 服务
1. 完成IISHost代码
- 引用Service程序集
- 添加ServiceCalculator.svc新文件,代码如下
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceCalculator" %>
- 添加ServiceCalculator.svc新文件,代码如下
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceMessage" %>
- 配置服务文件代码如下,这里我将配置两个服务一个是ServiceMessage用于Http通讯,一个是ServiceCalculator用于net.tcp通讯,如果不清楚服务配置,请参照WCF初探-6:WCF服务配置和WCF初探-7:WCF服务配置工具使用
<?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. 寄宿服务
- 生成IISHost程序,将bin文件目录、ServiceCalculator.svc、ServiceMessage.svc、Web.config拷贝到新建的WCFHost文件夹中
- 新建网站配置该程序以便承载服务。
点击IIS菜单的应用程序池,找到WCFHost程序池,将.net framework版本设置为v4.0,托管管道模式设置为集成
在浏览器中输入http://localhost:1234/ServiceMessage.svc可以看到服务发布成功
- 在浏览器中输入http://localhost:1234/ServiceCalculator.svc可以看到服务寄宿失败
这是因为ServiceCalculator.svc启用的是net.tcp通讯,而在IIS中启用net.tcp通讯就必须依靠Windows 进程激活服务(也称为 WAS)
- 要使用WAS寄宿程序,就需要配置几个地方
在控制面板->程序和功能->打开或关闭windows功能勾选以下几个功能,安装WCF 激活组件
配置承载服务的WCFHost网站,添加net.tcp通讯。
点击网站的高级设置,在已启用的协议后追加net.tcp协议
- 在浏览器中重新输入http://localhost:1234/ServiceCalculator.svc,可以看到服务寄宿成功
3. 客户端验证服务
- 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端
在托管应用程序中承载 WCF 服务
1. 完成AppHost代码
- 添加对service程序集的引用,配置文件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>
- Program.cs代码如下
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. 寄宿服务
- 生成AppHost工程,找到bin目录下的AppHost.exe,点击运行,查看到服务寄宿成功
3. 客户端验证服务
- 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端。分别添加服务地址:
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/
在托管 Windows 服务中承载 WCF 服务
1. 完成NTHost代码
- 添加windows服务程序services1.cs,在设计界面上单击右键添加安装程序ProjectInstaller.cs,在ProjectInstaller.cs设计界面上有serviceProcessInstaller1和serviceInstaller1两个安装组件,分别设置他们的属性
添加配置文件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>
- Service1.cs代码如下:
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. 寄宿服务
- 生成NTHost工程,安装windows服务程序NTHost.exe,在命令行中输入
Cd C:\Windows\Microsoft.NET\Framework\v4.0.30319,回车后输入installutil.exe 程序生成的bin目录绝对地址\NTHost.exe –i,回车后安装服务程序,程序注册成功后启动服务。
在开始菜单输入services.msc命令,打开服务管理程序将NTServiceHost服务设置为启动
3. 客户端验证服务
- 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端。分别添加服务地址:
http://localhost:2234/ServiceMessage/
http://localhost:1235/ServiceCalculator/