我创建了第一个自托管 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