首页 > 解决方案 > System.ServiceModel.CommunicationException:'服务端点未能侦听 URI'xxx',因为访问被拒绝

问题描述

我有一个在某些计算机上工作而在其他计算机上失败的单元测试。它确实启动了我们的一个组件,该组件具有自托管的 WCF 服务。

当它尝试启动时,我得到了这个异常:

System.ServiceModel.CommunicationException: 'The service endpoint failed to listen on the URI 'net.tcp://0.0.0.0:55000/VF1/IAccessRightsService' because access was denied.  Verify that the current user is granted access in the appropriate allowAccounts section of SMSvcHost.exe.config.'

   at System.ServiceModel.Channels.SharedConnectionListener.SharedListenerProxy.ReadEndpoint(String sharedMemoryName, String& listenerEndpoint)
   at System.ServiceModel.Channels.SharedConnectionListener.SharedListenerProxy.HandleServiceStart(Boolean isReconnecting)
   at System.ServiceModel.Channels.SharedConnectionListener.SharedListenerProxy.Open(Boolean isReconnecting)
   at System.ServiceModel.Channels.SharedConnectionListener.StartListen(Boolean isReconnecting)
   at System.ServiceModel.Channels.SharedTcpTransportManager.OnOpenInternal(Int32 queueId, Guid token)
   at System.ServiceModel.Channels.SharedTcpTransportManager.OnOpen()
   at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
   at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
   at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ReliableChannelListenerBase`1.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

我见过一些类似的问题:net.tcp 的 WCF 错误“服务端点未能在 URI 上侦听,因为访问被拒绝

但就我而言,一切都是在代码中配置的,我找不到任何方法来设置那些“通过代码允许的帐户”。

以下是 TCP 绑定的配置方式:

public ServiceBinding BuildBinding(String instanceName, string serviceName, bool isFastFailing)
        {
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
            binding.ReliableSession.Enabled = true;
            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
            binding.MaxBufferPoolSize = Int64.MaxValue;
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.SendTimeout = new TimeSpan(0, 0, isFastFailing ? TIMEOUT_FOR_FAST_FAILING_SERVICE : TIMEOUT_FOR_NORMAL_SERVICE);
            binding.ReceiveTimeout = WcfOperationsConstants.INACTIVITY_TIMEOUT;
            binding.OpenTimeout = new TimeSpan(0, 0, 2);
            binding.CloseTimeout = new TimeSpan(0, 0, 2);
            binding.PortSharingEnabled = true;

            Uri uri = new Uri(String.Format(URI_FORMAT, m_ip, m_port, instanceName, serviceName));
            return new ServiceBinding(uri, binding);
        }

(然后,它们只是添加了ServiceHost.

那么,如何在代码中配置呢?或者您对可能是什么问题有任何其他想法?为什么它可以在某些计算机上运行?

谢谢

标签: c#.netwcfself-hosting

解决方案


你可以试试这个方法:

1.下载PsGetSid。下载完成后,从cmd进入该工具所在的文件夹,执行以下命令获取当前用户的sid。 在此处输入图像描述

2.打开SMSvcHost.exe.config(这个文件一般在C:\Windows\Microsoft.NET\Framework\ v4.5),把你的sid添加到配置文件中。可以参考这个链接添加sid:配置Net.TCP端口共享服务


推荐阅读