System.ServiceModel.AddressAccessDeniedException:HTTP 无法注册 URL http::8080

2024-02-04

我创建了第一个自托管 WCF 服务。我将其托管在 C# 控制台应用程序中,但它引发错误:

System.ServiceModel.AddressAccessDeniedException:HTTP 无法注册 URL http:8080

当我以管理员身份运行 Visual Studio 2013 时,它运行良好,但如果我不运行,则运行不佳。那么有什么方法可以自动完成它而不是以管理员身份启动 VS 呢?

到目前为止我创建了一个HelloService我在其中添加了一个由接口组成的 WCF 服务的类库IHelloService and HelloService.

IHelloService:

namespace HelloService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        String GetMsg();
    }
}

HelloService:

namespace HelloService
{
    public class HelloService : IHelloService
    {
        public String GetMsg()
        {
            return "Service Accessed";
        }
    }
}

然后我创建了一个 C# 控制台应用程序HelloServiceHost其中有一个app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors >
        <behavior name="MexBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="HelloService.HelloService" 
               behaviorConfiguration="MexBehaviour" >
        <endpoint 
            address="HelloService" 
            binding="basicHttpBinding" 
            contract="HelloService.IHelloService"></endpoint>
        <endpoint 
            address="HelloService" 
            binding="netTcpBinding" 
            contract="HelloService.IHelloService"></endpoint>
        <endpoint 
            address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8081/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration> 

和program.cs文件:

using HelloService;
using System.ServiceModel;

namespace HelloServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using(ServiceHost sh = new ServiceHost(typeof(HelloService.HelloService)))
            {
                sh.Open();
                Console.WriteLine("Host Started @"+ System.DateTime.UtcNow.ToShortDateString());
                sh.Close();
            }
        }
    }
}

我完全按照视频教程进行操作,但它不起作用,为什么?

我正在使用 VS 2013,.net 4


以管理员身份启动cmd并输入:

netsh http add urlacl url=http://+:8080/MyUri user=DOMAIN\user

这对我有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

System.ServiceModel.AddressAccessDeniedException:HTTP 无法注册 URL http::8080 的相关文章

随机推荐