2009-11-01 68 views
11

我創建了一個WCF Serice,在IIS上託管時可以正常工作。無效的操作異常

現在,我採取了同樣的服務,並在WPF創建一個主機應用程序,並試圖從該應用程序啓動服務的時候,我得到這個異常:

The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the 
HttpGetUrl property is a relative address, but there is no http base address. 
Either  supply an http base address or set HttpGetUrl to an absolute address. 
+0

什麼是你的代碼創建代理? – Dani 2009-11-01 21:51:31

+0

productsServiceHost = new ServiceHost(typeof(Products.ProductsService)); productsServiceHost.Open(); stop.IsEnabled = true; start.IsEnabled = false; status.Text =「服務正在運行...」; – Attilah 2009-11-01 22:00:44

回答

22

的錯誤是很清楚 - 你'使用HTTP,你已經在你的ServiceMetadata行爲上啓用了HttpGetEnabled,但是你沒有在你的配置中提供一個基地址。

在IIS中,由於* .svc文件的位置定義了您的服務地址,所以既不需要也不使用基址。當您自行託管時,您可以並應該使用基地址。

更改你的配置看起來是這樣的:

<system.serviceModel> 
    <services> 
    <service name="YourService"> 
     <host> 
     <baseAddresses> 
      <add baseAddress="http://localhost:8080/YourService" /> 
     </baseAddresses> 
     </host> 
     <endpoint address="mex" binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
     ..... (your own other endpoints) ........... 
    </service> 
    </services> 
</system.serviceModel> 

現在,「HttpGetEnabled」有基址http://localhost.8080/YourService去擺脫的元數據。

或者,如果你不喜歡這一點,再次,該錯誤信息是你選擇的很清楚:定義一個絕對URL在您ServiceMetadata的HttpGetUrl:

<serviceBehaviors> 
    <behavior name="Default"> 
     <serviceMetadata 
      httpGetEnabled="true" 
      httpGetUrl="http://localhost:8282/YourService/mex" /> 
    </behavior> 
    </serviceBehaviors> 

客戶可以從中獲取元數據您的「mex」端點,或者在第二個示例中定義的固定URL處,或者它們將轉到元數據服務的基地址(如果有的話)。

如果你從IIS來了,還沒有適應任何東西,你既沒有基址,也不是爲你的元數據交換終結的明確,絕對URL,所以這就是爲什麼你得到你所看到的錯誤。

馬克

+1

我在這裏結束了同樣的問題,但不是因爲我不知道該怎麼做。相反,我不知道要在我的配置中添加「基址」。您使用了哪些資源來確定「」和「」屬性的存在和語法? – Matt 2016-01-03 00:12:46

0

,當我試圖使用net.pipe binding.In我來說,我遇到這個錯誤,默認服務行爲發佈的服務元數據,這是我的錯誤的原因。我的解決方案是爲您的服務使用不同的行爲。 ,然後我根據@marc_s回答改變了我的配置文件,使不同的服務行爲如下:

<serviceBehaviors> 
     <!--Default behavior for all services (in my case net pipe binding)--> 
     <behavior > 

      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" /> 

      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     <!--for my http services --> 
     <behavior name="MyOtherServiceBehavior"> 

      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 

      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
0

檢查服務類是正確的。

它解決了我的問題

// Create a ServiceHost for the CalculatorService type and 
// provide the base address. 
serviceHost = new ServiceHost(typeof(ServiceClass)); 
相關問題