Delen via


NamedPipe Activation

Download sample

This sample demonstrates hosting a service that uses Windows Process Activation Service (WAS) to activate a service that communicates over names pipes. This sample is based on the Getting Started Sample and requires Windows Vista to run.

Note

The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Hosting\WASHost\NamedPipeActivation.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

The sample consists of a client console program (.exe) and a service library (.dll) hosted in a worker process activated by the Windows Process Activation Services (WAS). Client activity is visible in the console window.

The service implements a contract that defines a request-reply communication pattern. The contract is defined by the ICalculator interface, which exposes math operations (Add, Subtract, Multiply, and Divide), as shown in the following sample code.

[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);
}

The client makes synchronous requests to a given math operation and the service implementation calculates and returns the appropriate result.

// 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;
    }
}

The sample uses a modified netNamedPipeBinding binding with no security. The binding is specified in the configuration files for the client and service. The binding type for the service is specified in the endpoint element’s binding attribute as shown in the following sample configuration.

If you want use a secured named pipe binding, change the server's security mode to the desired security setting and run svcutil.exe again on the client to obtain an updated client configuration file.

<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 explosed 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>

The client’s endpoint information is configured as shown in the following sample code.

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

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

    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.

To set up, build, and run the sample

  1. Ensure you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

    In addition, you must install the WCF non-HTTP activation components:

    1. From the Start menu, choose Control Panel.

    2. Select Programs and Features.

    3. Click Turn Windows Components on or Off.

    4. Expand the Microsoft .NET Framework 3.0 node and check the Windows Communication Foundation Non-HTTP Activation feature.

  2. Configure the Windows Process Activation Service (WAS) to support named pipe activation.

    As a convenience, the following two steps are implemented in a batch file called AddNetPipeSiteBinding.cmd located in the sample directory.

    1. To support net.pipe activation, the default web site must first be bound to the net.pipe protocol. This can be done using appcmd.exe, which is installed with the IIS 7.0 management toolset. From an elevated (administrator) command prompt, run the following command:

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

      Note

      This command is a single line of text.

      This command adds a net.pipe site binding to the default Web site.

    2. Although all applications within a site share a common net.pipe binding, each application can enable net.pipe support individually. To enable net.pipe for the /servicemodelsamples application, run the following command from an elevated command prompt:

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

      Note

      This command is a single line of text.

      This command enables the /servicemodelsamples application to be accessed using both https://localhost/servicemodelsamples and net.tcp://localhost/servicemodelsamples.

  3. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  4. Remove the net.pipe site binding you added for this sample.

    As a convenience, the following two steps are implemented in a batch file called RemoveNetPipeSiteBinding.cmd located in the sample directory.

    1. Remove net.tcp from the list of enabled protocols by running the following command from an elevated command prompt:

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

      Note

      This command must be entered as a single line of text.

    2. Remove the net.tcp site binding by running the following command from an elevated command prompt:

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

      Note

      This command must be entered as a single line of text.

© 2007 Microsoft Corporation. All rights reserved.