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

时间:2013-01-28 13:53:34

标签: wcf

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

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

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

internal static void StartService()
{
    Instance = new ServiceHost(typeof (DomainWriteService));
    Instance.Open();
}

我已经通过以下方式在我的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>

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

    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.

我在Windows 8上

1 个答案:

答案 0 :(得分:5)

您需要使用netsh add urlacl来允许侦听能够在端口80上接收的进程(或与其他侦听器共享端口80)

netsh http add urlacl url=http://+:80/service user=YOURUSER

其中YOURUSER是运行侦听器的进程的凭据,例如DOMAIN\User

另请参阅:WCF ServiceHost access rights