2014-09-04 117 views
1

我写了一个简单的WCF代码。并想要托管它。但是当我运行它时显示出一个异常无法托管我的WCF

HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace 我有一个类库项目,其中我写了我的WCF代码。然后我加入App.config文件和写一些代码

的App.config代码:

<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 
    </startup> 
    <system.serviceModel> 
    <services> 
     <service name="DEMO1.HelloService" behaviorConfiguration="mexBehavior"> 
     <endpoint address="HelloService" binding="basicHttpBinding" contract="DEMO1.IHelloService"></endpoint> 
     <endpoint address="HelloService" binding="netTcpBinding" contract="DEMO1.IHelloService"></endpoint> 
     <endpoint address="Mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080"/> 
      <add baseAddress="net.tcp://localhost:8090"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="mexBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

这是我的接口

[ServiceContract] 
    public interface IHelloService 
    { 
     [OperationContract] 
     string GetMessage(string name); 
    } 

,我实现了这个

public class HelloService : IHelloService 
    { 
     public string GetMessage(string name) 
     { 
      return "Name : " + name; 
     } 
    } 

然后我加入另一个控制台项目到该解决方案和

class Program 
    { 
     static void Main() 
     { 
      using(ServiceHost host=new ServiceHost(typeof(DEMO1.HelloService))) 
      { 
       try 
       { 
        host.Open(); 
        Console.WriteLine("Started"); 
       }catch(Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
       Console.ReadLine(); 
      } 
     } 
    } 

我的层次 enter image description here

+1

你能告诉我你的服务运行具有管理员或没有? – Developer 2014-09-04 17:15:57

+1

你是否试过以管理员的身份运行应用程序? – Lawrence 2014-09-04 17:17:47

+0

@Singh:我如何检查它,因为WCF代码是在类库中编写的,当我在浏览器中编写localhost:8080,然后什么都不在,页面显示'无法连接' – 2014-09-04 17:20:56

回答

0

通过HTTP.SYS(包括WCF)requires permission to the namespace托管的任何服务正在尝试使用(http://localhost:8080,就您的情况而言)。

要么以管理员身份运行,要么以add an urlacl运行,它允许您的进程访问命名空间。

在管理员命令提示符:

netsh http add urlacl url=http://+:8080/ user=BUILTIN\Users 
1

这个问题来的时候,你可以手动改变baseaddress。因此,wcf服务找不到服务所在的服务位置,因此如果您可以更改服务端口baseaddress,则只能更改端口号,不应更改所有地址。如果您更改了所有地址,则地址找不到服务位置,并且服务一直给出错误。