AddressAccessDeniedException "您的进程没有访问此命名空间的权限";单元测试 WCF 服务时 [英] AddressAccessDeniedException "Your process does not have access rights to this namespace" when Unit testing WCF service

查看:26
本文介绍了AddressAccessDeniedException "您的进程没有访问此命名空间的权限";单元测试 WCF 服务时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am testing my WCF services but I need to fake the behavior because my services are bootstrapped using a custom factory. This is the code I wrote for my test:

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

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

And I have a test initialize which is creating a new ServiceHost:

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

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

I have configured a fake endpoint in my MsTest project in the following way:

<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>

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

But when I run my tests I get this runtime error:

    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

I am on Windows 8

推荐答案

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

You'll need to use netsh add urlacl to allow access for the process listening to be able to receive on port 80, (or share port 80 with other listeners)

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

其中 YOURUSER 是运行侦听器的进程的凭据,例如域\用户.

Where YOURUSER is the credentials of the process running the listener, e.g. DOMAIN\User.

另见:WCF ServiceHost 访问权限

这篇关于AddressAccessDeniedException "您的进程没有访问此命名空间的权限";单元测试 WCF 服务时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆