I am creating a windows service that is supposed to host a WCF service with NetNamedPipeBinding binding. Following is the code:
protected override void OnStart(string[] args)
{
var serviceType = typeof(IServeHi);
var namedPipeBinding = new NetNamedPipeBinding();
namedPipeBinding.Security.Mode = NetNamedPipeSecurityMode.None;
var namedPipeEndpoint = "";
var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation:51301/"); // Line # 41
host = new ServiceHost(typeof(ServeHi), baseNamedPipeUri);
host.AddServiceEndpoint(serviceType, namedPipeBinding, namedPipeEndpoint);
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true});
host.Open();
}
The installutil.exe
is able to successfully install it. However, when I try to start the service, I get a message The WindowsServiceHost1 on a Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
Further, the Windows Log reads as:
Service cannot be started. System.UriFormatException: Invalid URI: The hostname could not be parsed.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at WindowsServiceHost1.WindowsServiceHost1.OnStart(String[] args) in C:\WindowsServiceHost1\Service1.cs:line 41
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
I have tried WSHttpBinding & NetTcpBinding and they work fine.
You'll want to remove the port number from the net.pipe URI. Try changing line 41 to:
var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation/");
or if you need a more descriptive pipe name:
var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation_51301/");
This blog post has some helpful insight on this issue.