I'm using two applications built using C# on 4.5 .Net Framework
- WPF Desktop Application
- 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();
}