5

I am getting the following error:

Cannot listen on pipe name 'net.pipe://localhost/ServiceModelSamples/service/' because another pipe endpoint is already listening on that name

I am calling WCF self hosted service inside windows service from another WCF call as follows.

$

ServiceHost _host;
var guidid = Guid.NewGuid().ToString() ;

_host = new ServiceHost(typeof(CalculatorService), new Uri[] { new Uri("net.pipe://localhost/" + guidid)});

_host.AddServiceEndpoint(typeof(ICalculator),
        new NetNamedPipeBinding(),
        "");

_host.Open();

ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(
    new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),
    new EndpointAddress("net.pipe://localhost/" + guidid));
ICalculator proxy = factory.CreateChannel();
proxy.SendPin(pin);
((IClientChannel)proxy).Close();
factory.Close();

Self-Hosted WCF Service

$

namespace PINSender
{
    // Define a service contract.    
    
    public interface ICalculator
    {
        [OperationContract]
        void SendPin(string pin);
    }

    // Implement the ICalculator service contract in a service class.
    public class CalculatorService : ICalculator
    {
        // Implement the ICalculator methods.
        public void  SendPin(string pin)
        {
            
        }
    }

    public class CalculatorWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "PINSenderService";
        }

        public static void Main()
        {
            ServiceBase.Run(new CalculatorWindowsService());
        }

        // Start the Windows service.
        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(CalculatorService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }

    // Provide the ProjectInstaller class which allows 
    // the service to be installed by the Installutil.exe tool
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "PINSenderService";
            Installers.Add(process);
            Installers.Add(service);
        }
    }
}

App.Config

$

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
  <!-- 
    Following is the expanded configuration section for a NetNamedPipeBinding.
    Each property is configured with the default value.
 -->
  <netNamedPipeBinding>
    <binding name="Binding1"
             closeTimeout="00:01:00"
             openTimeout="00:01:00"
             receiveTimeout="00:10:00"
             sendTimeout="00:01:00"
             transactionFlow="false"
             transferMode="Buffered"
             transactionProtocol="OleTransactions"
             hostNameComparisonMode="StrongWildcard"
             maxBufferPoolSize="524288"
             maxBufferSize="65536"
             maxConnections="10"
             maxReceivedMessageSize="65536">
      <security mode="Transport">
        <transport protectionLevel="EncryptAndSign" />
      </security>
    </binding>
  </netNamedPipeBinding>
</bindings>

<services>
  
  <service name="PINSender.CalculatorService"
           behaviorConfiguration="CalculatorServiceBehavior">
    <host>
      <baseAddresses>            
        <add baseAddress="net.pipe://localhost/PINSenderService"/>
      </baseAddresses>          
    </host>
    
    <endpoint address=""
              binding="netNamedPipeBinding"
              contract="PINSender.ICalculator" bindingConfiguration="Binding1" />
    <endpoint address="mex"
              binding="mexNamedPipeBinding"
              contract="IMetadataExchange" />                 
  </service>
</services>
<behaviors>      
  <serviceBehaviors>        
    <behavior name="CalculatorServiceBehavior">
      <serviceMetadata httpGetEnabled="False"  />
      <serviceDebug includeExceptionDetailInFaults="False"/>
    </behavior>
  </serviceBehaviors>
  </behaviors>
 </system.serviceModel>
/configuration>
riQQ
  • 9,878
  • 7
  • 49
  • 66
Hammad Bukhari
  • 459
  • 2
  • 11
  • 30
  • 1
    Sounds like you have two listeners fighting over the same port. Make sure you don't have an instance running already, then try again. – Brian May 07 '14 at 12:20
  • 1
    Will this in any way help? http://stackoverflow.com/questions/2118114/prevent-named-pipes-conflict – Ani May 07 '14 at 12:23
  • Ok i have changed some code now i am getting another error when i try to access proxy.SendPin(pin). "There was an error reading from the pipe: The pipe has been ended." Please see original post – Hammad Bukhari May 07 '14 at 12:41

0 Answers0