我编写了一个 C# 控制台应用程序来使用 NetNamedPipeBinding 使用本地 WCF 服务。我使用的 WCF 服务作为本地 Windows 应用程序运行。我的 WCF 客户端可以作为控制台应用程序正常工作。但是,当我将本质上相同的 WCF 客户端实现为不是作为控制台应用程序而是作为 Windows 服务时,我的 WCF 客户端找不到本地 WCF 端点并引发 System.ServiceModel.EndpointNotFoundException 异常。
为什么 WCF 客户端作为控制台应用程序运行良好,然后在客户端作为 Windows 服务运行时抛出 EndpointNotFoundException?
这就是我在客户端代码中创建通道工厂的方式:
// Define some local variables needed to create the channel factory
string strendpoint = string.Format("net.pipe://localhost/{0}", BluehillAPIService.ServiceName);
EndpointAddress endpoint = new EndpointAddress(strendpoint);
NetNamedPipeBinding binding = new NetNamedPipeBinding();
InstanceContext instanceContact = new InstanceContext(this);
// Create the channel factory
ChannelFactory = new DuplexChannelFactory<IBluehillAPIService>(instanceContact, binding, endpoint);
然后我像这样创建频道:
_bluehillAPIService = ChannelFactory.CreateChannel();
到目前为止没有错误,但是当我尝试按如下方式实际使用通道时,出现异常:
if (!_bluehillAPIService.APIHeartbeat()) ...
当此代码作为以本地系统登录的 Windows 服务运行时,在我的 WCF 客户端中对 _bluehillAPIService.APIHeartbeat() 的调用会生成 System.ServiceModel.EndpointNotFoundException,但当本质上相同的代码作为控制台应用程序运行时工作正常。