2

我正在测试我的 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 上

4

1 回答 1

6

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

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

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

另请参阅:WCF ServiceHost 访问权限

于 2013-01-28T14:44:31.963 回答