简体   繁体   中英

Unable to start Windows Service that hosts WCF service with NetNamedPipeBinding binding

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM