2014-09-04 118 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,則只能更改端口號,不應更改所有地址。如果您更改了所有地址,則地址找不到服務位置,並且服務一直給出錯誤。