本示例演示如何承载使用 Windows 进程激活服务 (WAS) 的服务以激活通过命名管道进行通信的服务。 此示例基于Getting Started并且需要Windows Vista运行。
Note: 本主题的最后介绍了此示例的设置过程和生成说明。
Important: 您的计算机上可能已安装这些示例。 在继续操作之前,请先检查以下(默认)目录:
<InstallDrive>:\WF_WCF_Samples如果此目录不存在,请转到Windows Communication Foundation (WCF) 和.NET Framework 4 的 Windows Workflow Foundation (WF) 示例若要下载所有 Windows Communication Foundation (WCF) 和WF示例。 此示例位于以下目录:
<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Services\Hosting\WASHost\NamedPipeActivation
示例详细信息
本示例由客户端控制台程序 (.exe) 和由 Windows 进程激活服务 (WAS) 激活的辅助进程中承载的服务库 (.dll) 组成。 客户端活动显示在控制台窗口中。
该服务实现定义“请求-答复”通信模式的协定。 该协定由 ICalculator 接口定义,该接口公开数学运算(加、减、乘和除),如下面的示例代码所示:
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
客户端向给定的数学运算发出同步请求,服务实现进行计算并返回相应的结果。
// Service class that implements the service contract.
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
示例使用经过修改的无安全性的 netNamedPipeBinding 绑定。 绑定是在客户端和服务的配置文件中指定的。 服务的绑定类型是在终结点元素的 binding 属性中指定的,如下面的示例配置所示。
如果你想使用安全的命名管道绑定,请将服务器的安全模式更改为所需的安全设置,并在客户端上重新运行 svcutil.exe 以获取更新的客户端配置文件。
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<!-- This endpoint is exposed at the base address provided by host: net.pipe://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="netNamedPipeBinding"
bindingConfiguration="Binding1"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<!-- the mex endpoint is exposed at net.pipe://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexNamedPipeBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netNamedPipeBinding>
<binding name="Binding1" >
<security mode = "None">
</security>
</binding >
</netNamedPipeBinding>
</bindings>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端的终结点信息按下面的示例代码所示进行配置。
<system.serviceModel>
<client>
<endpoint name=""
address="net.pipe://localhost/servicemodelsamples/service.svc"
binding="netNamedPipeBinding"
bindingConfiguration="Binding1"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>
<bindings>
<!-- Following is the expanded configuration section for a NetNamedPipeBinding.
Each property is configured with the default value. -->
<netNamedPipeBinding>
<binding name="Binding1"
maxBufferSize="65536"
maxConnections="10">
<security mode = "None">
</security>
</binding >
</netNamedPipeBinding>
</bindings>
</system.serviceModel>
运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 Enter 可以关闭客户端。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
设置、生成和运行示例
-
确保安装了 IIS 7.0。 为使 WAS 激活需要 IIS 7.0。
-
请确保具有执行的 Windows Communication Foundation 示例的一次性安装过程。
此外,必须安装 WCF 非 HTTP 激活组件:
-
从“开始”菜单中,选择“控制面板” 。
-
选择程序和功能。
-
单击打开或关闭 Windows 组件。
-
展开Microsoft.NET Framework 3.0节点并检查Windows Communication Foundation 非 HTTP 激活功能。
-
-
将 Windows 进程激活服务 (WAS) 配置为支持命名管道激活。
为方便起见,在位于示例目录中名为 AddNetPipeSiteBinding.cmd 的批处理文件中实现以下两个步骤。
-
若要支持 net.pipe 激活,必须首先将默认的网站绑定到 net.pipe 协议。 可以通过使用随 IIS 7.0 管理工具集安装的 appcmd.exe 来执行此操作。 在具有提升权限的(管理员)命令提示符处,运行下列命令。
%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.pipe',bindingInformation='*']NOTE 此命令是单行文本。
此命令可将 net.pipe 网站绑定添加到默认网站。
-
尽管网站内的所有应用程序共享一个公共 net.pipe 绑定,但是每个应用程序可以单独启用 net.pipe 支持。 若要启用 /servicemodelsamples 应用程序的 net.pipe,请在具有提升权限的命令提示符处运行以下命令。
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http,net.pipeNOTE 此命令是单行文本。
此命令将启用 /servicemodelsamples 应用程序使用同时访问
http://localhost/servicemodelsamples和net.tcp://localhost/servicemodelsamples。
-
-
若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照 Building the Windows Communication Foundation Samples中的说明进行操作。
-
移除为此示例添加的 net.pipe 网站绑定。
为方便起见,在位于示例目录中名为 RemoveNetPipeSiteBinding.cmd 的批处理文件中实现以下两个步骤:
-
通过在具有提升权限的命令提示符处运行以下命令,从启用的协议列表中移除 net.tcp。
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:httpNOTE 必须以单行文本的形式输入此命令。
-
通过在具有提升权限的命令提示符处运行以下命令移除 net.tcp 站点绑定。
%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" --bindings.[protocol='net.pipe',bindingInformation='*']NOTE 必须以单行文本的形式键入此命令。
-
请参阅
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站展开固定版本仓库内的引用内容,将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。
