【问题标题】:Exception when starting a ServiceHost with NamedPipe使用 NamedPipe 启动 ServiceHost 时出现异常
【发布时间】:2013-10-02 14:01:39
【问题描述】:

我有一个自托管 WCF 服务的应用程序(以允许另一个应用程序试运行该应用程序)。

该服务在我们所有的测试计算机(Windows 7、Windows XP)上运行良好,但我们的一位客户(在 Windows xp 上)刚刚打电话给我们,在发布时报告了一个异常。

通过我们的软件跟踪,我们发现了以下内容:

似乎当我们进行ServiceHost.Open 调用时,我们得到了这个异常:

System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.ServiceModel.Channels.UdpUtility.BindSocket(Socket socket, IPEndPoint localEndpoint)
   at System.ServiceModel.Channels.UdpUtility.CreateListenSocket(IPAddress ipAddress, Int32& port, Int32 receiveBufferSize, Int32 timeToLive, Int32 interfaceIndex, Boolean allowMulticastLoopback, Boolean isLoopbackAdapter)
   at System.ServiceModel.Channels.UdpChannelListener.InitSockets(Boolean updateListenPort)
   at System.ServiceModel.Channels.UdpChannelListener..ctor(IUdpTransportSettings settings, BindingContext context)
   at System.ServiceModel.Channels.UdpTransportBindingElement.BuildChannelListener[TChannel](BindingContext context)
   at System.ServiceModel.Channels.BindingContext.BuildInnerChannelListener[TChannel]()
   at System.ServiceModel.Channels.MessageEncodingBindingElement.InternalBuildChannelListener[TChannel](BindingContext context)
   at System.ServiceModel.Channels.TextMessageEncodingBindingElement.BuildChannelListener[TChannel](BindingContext context)
   at System.ServiceModel.Channels.BindingContext.BuildInnerChannelListener[TChannel]()
   at System.ServiceModel.Channels.Binding.BuildChannelListener[TChannel](Uri listenUriBaseAddress, String listenUriRelativeAddress, ListenUriMode listenUriMode, BindingParameterCollection parameters)
   at System.ServiceModel.Description.DispatcherBuilder.MaybeCreateListener(Boolean actuallyCreate, Type[] supportedChannels, Binding binding, BindingParameterCollection parameters, Uri listenUriBaseAddress, String listenUriRelativeAddress, ListenUriMode listenUriMode, ServiceThrottle throttle, IChannelListener& result, Boolean supportContextSession)
   at System.ServiceModel.Description.DispatcherBuilder.BuildChannelListener(StuffPerListenUriInfo stuff, ServiceHostBase serviceHost, Uri listenUri, ListenUriMode listenUriMode, Boolean supportContextSession, IChannelListener& result)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
   at System.ServiceModel.ServiceHostBase.InitializeRuntime()
   at System.ServiceModel.ServiceHostBase.OnBeginOpen()
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

我看了一下这个错误,似乎它可能与提供的 IP 错误有关。

首先,这是我们创建服务的方式:

m_host = new ServiceHost(m_ourServiceInstance);
NetNamedPipeBinding namedPipeBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
namedPipeBinding.Security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;
SetBindingDefaultTimeouts(namedPipeBinding);
String url = String.Format("net.pipe://localhost/ServiceName_{0}", Process.GetCurrentProcess().Id);
m_host.AddServiceEndpoint(typeof(IOurServiceType), namedPipeBinding, url);
m_host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
m_host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
m_host.Open();//Crash on this line !!!

对我来说有几个谜团:

  1. 为什么本地 NamedPipe 使用套接字?
  2. 在不同的计算机之间可以改变什么?除了我看不到的进程 ID?
  3. 我可以做些什么来进行更多调查?我有点迷路了。

非常感谢

【问题讨论】:

  • 我不知道这是否是问题所在,但后缀为 process_id 的服务名称是 ..“对我来说很奇怪”。您的客户需要能够找到您的服务......并且此代码每次都会为您提供不同的服务名称。 "net.pipe://localhost/EmployeeService" , "net.pipe://localhost/ServiceName_EmployeeService" 这将是更常见的恕我直言。您的客户端如何连接到这个公开的服务(因为 process_id 波动)?
  • 事实上:假设托管服务的应用程序是应用程序“A”。连接到该服务的应用程序是应用程序“B”。应用程序B其实就是启动应用程序“A”然后使用WCF服务配置应用程序A的很多参数的应用程序
  • 好像有人取出了“SelfHost”的“Self”。我完全不明白那个动作。
  • 这是一个问题和观察。为什么要在命名管道上设置安全性?什么可以在与本地机器对话的本地机器上截获该消息?
  • @granadaCoder :你完全正确,我看不出有任何正当理由在这里使用它。我将与创建者一起了解为什么要设置此安全性。

标签: c# .net wcf windows-xp named-pipes


【解决方案1】:

为什么本地 NamedPipe 使用套接字?

因为你这里有UdpDiscoveryEndpoint

m_host.AddServiceEndpoint(new UdpDiscoveryEndpoint());

这里的不同计算机之间可以改变什么?除了我看不到的进程ID?

  1. 试图以访问权限禁止的方式访问套接字。
  2. 您已经拥有相同的限定地址(可能来自之前未正确处理它的非托管资源的进程。或者尚未关闭) 欲了解更多信息:MSDN

【讨论】:

  • 访问权限呢?我不认为是 2),因为我们总是使用这台特定的计算机,即使在重新启动后也是如此。
  • 我认为您的问题出在 'UdpDiscoveryEndpoint' 中。尝试删除这行代码并再次检查。也许问题是你没有为它设置一个 URL
  • 是的,我也认为这是造成麻烦的部分,看到调用堆栈。我们目前不需要它(但很快我们会需要)。所以首先我要在没有这个的情况下向我们的客户部署一个新版本,看看是否是问题所在。但我很快就会需要它,所以我很想知道什么会导致麻烦。我应该如何为它设置一个网址?
  • 我不知道那里到底发生了什么,我建议您查看这里以获取有关如何使用发现服务的更多信息,祝你好运! msdn.microsoft.com/en-us/library/dd483326.aspx
猜你喜欢
  • 2011-08-30
  • 2014-03-07
  • 2016-02-23
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2018-03-05
相关资源
最近更新 更多