NamedPipe 激活

NamedPipeActivation 示例演示如何托管使用 Windows 进程激活服务(WAS)来激活通过命名管道进行通信的服务。 此示例基于 入门 ,需要运行 Windows Vista。

注释

本示例的设置过程和生成说明位于本主题末尾。

示例详细信息

该示例由客户端控制台程序(.exe)和服务库(.dll)包含在由 Windows 进程激活服务(WAS)激活的辅助进程中托管。 客户端活动在控制台窗口中可见。

该服务实现定义请求-回复通信模式的协定。 协定由 ICalculator 接口定义,该接口公开数学运算(Add、Subtract、Multiply 和 Divide),如以下示例代码所示。

[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.

设置、生成和运行示例

  1. 确保已安装 IIS 7.0。 WAS 激活需要 IIS 7.0。

  2. 请确保已执行 Windows Communication Foundation 示例的一次性安装过程

    此外,必须安装 WCF 非 HTTP 激活组件:

    1. “开始” 菜单中,选择 “控制面板”。

    2. 选择 “程序和功能”。

    3. 单击“ 打开或关闭 Windows 组件”。

    4. 展开 Microsoft .NET Framework 3.0 节点,并检查 Windows Communication Foundation 非 HTTP 激活功能

  3. 配置 Windows 进程激活服务(WAS)以支持命名管道激活。

    为方便起见,以下两个步骤在示例目录中名为AddNetPipeSiteBinding.cmd的批处理文件中实现。

    1. 若要支持 net.pipe 激活,必须先将默认网站绑定到 net.pipe 协议。 可以使用随 IIS 7.0 管理工具集一起安装的 appcmd.exe来完成此作。 在提升权限的(管理员)命令提示符下运行以下命令。

      %windir%\system32\inetsrv\appcmd.exe set site "Default Web Site"
      -+bindings.[protocol='net.pipe',bindingInformation='*']
      

      注释

      此命令是单行文本。

      此命令将 net.pipe 网站绑定添加到默认网站。

    2. 尽管站点中的所有应用程序共享通用 net.pipe 绑定,但每个应用程序可以单独启用 net.pipe 支持。 若要为 /servicemodelsamples 应用程序启用 net.pipe,请从具有管理员权限的命令提示符运行以下命令。

      %windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http,net.pipe
      

      注释

      此命令是单行文本。

      此命令允许通过http://localhost/servicemodelsamplesnet.tcp://localhost/servicemodelsamples两种方式访问 /servicemodelsamples 应用程序。

  4. 若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。

  5. 删除为此示例添加的 net.pipe 站点绑定。

    为方便起见,以下两个步骤在示例目录中名为RemoveNetPipeSiteBinding.cmd的批处理文件中实现:

    1. 通过在具有提升权限的命令提示符处运行以下命令,从启用的协议列表中移除 net.tcp。

      %windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http
      

      注释

      此命令必须以单行文本形式输入。

    2. 通过在具有提升权限的命令提示符处运行以下命令移除 net.tcp 站点绑定。

      %windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" --bindings.[protocol='net.pipe',bindingInformation='*']
      

      注释

      此命令必须键入为单行文本。

另请参阅