AddressAccessDeniedException"单元测试WCF服务时,您的进程没有此命名空间的访问权限"

Raf*_*aeu 2 wcf

我正在测试我的WCF服务,但我需要伪造行为,因为我的服务是使用自定义工厂引导的.这是我为测试编写的代码:

var channelFactory = new ChannelFactory<IDomainWriteService>(
   new CustomBinding(
      new BinaryMessageEncodingBindingElement(), 
      new HttpTransportBindingElement()),
   new EndpointAddress(new Uri("local")));
var service = channelFactory.CreateChannel();
Run Code Online (Sandbox Code Playgroud)

我有一个测试初始化​​,它正在创建一个新的ServiceHost:

internal static void StartService()
{
    Instance = new ServiceHost(typeof (DomainWriteService));
    Instance.Open();
}
Run Code Online (Sandbox Code Playgroud)

我通过以下方式在MsTest项目中配置了一个假端点:

<service name="xxx.DomainWriteService">
  <endpoint binding="basicHttpBinding" bindingConfiguration=""
    name="local" bindingName="http" contract="xxx.IDomainWriteService" />
  <host>
    <baseAddresses>
      <add baseAddress="http://xxx/service" />
    </baseAddresses>
  </host>
</service>
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的测试时,我得到了这个运行时错误:

    Class Initialization method xxx.ClassInitialize threw exception. System.ServiceModel.AddressAccessDeniedException: 
System.ServiceModel.AddressAccessDeniedException: 
HTTP could not register URL **http://+:80/service/**. 
Your process does not have access rights to this namespace 
(see http://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> 
System.Net.HttpListenerException: Access is denied.
Run Code Online (Sandbox Code Playgroud)

我在Windows 8上

Stu*_*tLC 6

您需要使用netsh add urlacl以允许访问过程,以便能够在端口80上接收(或与其他侦听器共享端口80)

netsh http add urlacl url=http://+:80/service user=YOURUSER
Run Code Online (Sandbox Code Playgroud)

YOURUSER运行侦听器的进程的凭据在哪里,例如DOMAIN\User.

另请参阅:WCF ServiceHost访问权限