gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:29:16 24 4
gpt4 key购买 nike

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

System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http: 8080

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

到目前为止,我创建了一个 HelloService我在其中添加了一个 WCF 服务的类库,该服务由一个接口(interface) IHelloService 组成和 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文件:

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

这对我有用。

关于c# - System.ServiceModel.AddressAccessDeniedException:HTTP 无法注册 URL http::8080,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22686469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com