我正在创建一个 Windows 服务,它应该使用NetNamedPipeBinding绑定来托管 WCF 服务。以下是代码:
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();
}
installutil.exe
能够成功安装它。但是,当我尝试启动该服务时,我收到一条消息本地计算机上的 WindowsServiceHost1 已启动然后停止。如果某些服务没有被其他服务或程序使用,它们会自动停止。
此外,Windows 日志显示为:
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)
我已经尝试过WSHttpBinding和NetTcpBinding并且它们工作正常。