1

I'm using two applications built using C# on 4.5 .Net Framework

  1. WPF Desktop Application
  2. Windows Service

and want them talk to each other using IPC(Inter Process Communication) approach as they need to frequently share some data/object state based on condition.

WCF with netNamedPipeBinding seems a very flexible solution. I've created a WCF Server and Client & tested successfully hosting in Console Application.

Since this solution worked, I wanted WCF Server to be hosted in Windows Service(which is my eventual requirement).

I could host the application successfully(guess because I don't see any visible error) but I cannot connect any client to it. I've used WcfTestClient.exe tool(Visual Studio default tool) as well as tried connecting from a console application, none of them seem working, as I keep on getting the error - Cannot obtain Metadata from net.pipe://localhost/TestWCFService/mex . Details added below.

I've registered the windows service with Admin privilege and running the console app test client & WcfTestClient.exe with the same user.

Naturally, I'm missing something, appreciate your help on fixing it.

Here is the code, I'm using in Windows Service to Host WCF netNamedPipeBinding Service:

protected override void OnStart(string[] args)
    {
        try
        {
            if (wcfServiceHostObj != null)
                wcfServiceHostObj.Close();

            wcfServiceHostObj = new ServiceHost(typeof(TestWCFService));
            wcfServiceHostObj.Open();
            EventLog.WriteEntry(ServiceName, "WCF Host - Started Successfully ", EventLogEntryType.Information);

        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ServiceName, "exception raised " + ex.InnerException, EventLogEntryType.Error);
            throw;
        }

    }

Error: Cannot obtain Metadata from net.pipe://localhost/TestWCFService/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: net.pipe://localhost/TestWCFService/mex Metadata contains a reference that cannot be resolved: 'net.pipe://localhost/TestWCFService/mex'. There was no endpoint listening at net.pipe://localhost/TestWCFService/mex that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The pipe endpoint 'net.pipe://localhost/TestWCFService/mex' could not be found on your local machine.


Here my WCF Server Config Setting:

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="TestWCFServiceNetPipeBehavior">
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <services>
            <service behaviorConfiguration="TestWCFServiceNetPipeBehavior"
                name="WCFHostTest.WCFService.TestWCFService">

              <endpoint address="net.pipe://localhost/TestWCFService" binding="netNamedPipeBinding"  bindingConfiguration=""
                  name="TestWCFServiceNetPipeEndPoint" contract="WCFHostTest.WCFService.ITestWCFService" >

              </endpoint>

                <endpoint address="net.pipe://localhost/TestWCFService/mex" binding="mexNamedPipeBinding" bindingConfiguration=""
                    name="TestWCFServiceMexPipeEndpoint" contract="IMetadataExchange" />
                <host>
                    <!--<baseAddresses>
                        <add baseAddress="net.pipe://localhost/TestWCFService" />
                    </baseAddresses>-->
                </host>
            </service>
        </services>
    </system.serviceModel>

For Client(which is in Console Application), I'm using inline. Here the code

private static void testClient()
    {
        try
        {
            string address = "net.pipe://localhost/TestWCFService";
             NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            EndpointAddress ep = new EndpointAddress(address);

            ITestWCFService channel = ChannelFactory<ITestWCFService>.CreateChannel(binding, ep);

            using (channel as IDisposable)
            {
                channel.SendMessage("First Message");
            }

            Console.ReadLine();

        }
        catch (Exception ex)
        {

            Console.Write(ex.InnerException);
            Console.ReadLine();

        }
manvendra
  • 461
  • 2
  • 4
  • 15
  • Do you see any entries in the event log? Is the Windows Service running when you try to connect to the hosted WCF service? Post your client and windows service config files as well. – Tim Jul 28 '16 at 16:17
  • Also, you're not adding any endpoints or specifying any endpoint configurations in your service's `OnStart` method. – Tim Jul 28 '16 at 19:59
  • In the Event log, I see "WCF Host - Started Successfully" message which means, there were no issues(at least code thinks so) starting the WCF Server. – manvendra Jul 29 '16 at 05:15
  • I'm using the config file to load config setting in case of WCF Server. – manvendra Jul 29 '16 at 05:16
  • Here is the WCF Server Config: – manvendra Jul 29 '16 at 05:17
  • Config setting, I added in the note above – manvendra Jul 29 '16 at 05:36

1 Answers1

0

The most likely reason you can't connect is because there is no service address passed into the ServiceHost constructor.

Try this in your OnStart method in your Windows service:

wcfServiceHostObj = new ServiceHost(typeof(TestWCFService), 
    new Uri[] { "net.pipe://localhost/TestWCFService" });

Seeing the "WCF Host - Started Successfully" only means ServiceHost didn't throw an error; it doesn't guarantee that the WCF service is listening and ready to receive messages.

Also, using the using statement with WCF clients like this:

using (channel as IDisposable)
{
    channel.SendMessage("First Message");
}

is considered bad practice.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • I've made the suggested changes but get the same error. – manvendra Jul 29 '16 at 11:09
  • @manvendra, did you find the solution? I have the same problem. – aura May 31 '18 at 07:48
  • can wcf soap be hosted in windows service with netNamedPipeBinding? – aura May 31 '18 at 07:48
  • @aura - yes, wcf soap can be hosted in windows service with netNamedPipeBinding. I've this solution running in production for some time now. – manvendra Jun 01 '18 at 09:15
  • @aura - The above issue, I was never able to find the root cause. I tried debugging for many days at various levels but could never find root cause. Ultimately, I tried a new machine & that worked. Don't know why it didn't on my earlier machine – manvendra Jun 01 '18 at 09:18