2012-03-24 116 views
0

我創建了一個名爲MiniCalc的基本計算器服務,該服務只有兩個操作。添加和Mul,並將其託管在控制檯應用程序中。無法使用「添加服務參考」生成自託管服務的代理

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), 
             new Uri("http://localhost:8091/MiniCalcService"))) 
{ 
    host.AddServiceEndpoint(typeof(MiniCalcService.IService), 
          new BasicHttpBinding(), 
          "Service"); 
    host.Open(); 
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . "); 
} 

然後創建了一個控制檯應用程序來消費服務和通過創建一個代理類手動創建的代理,然後創建的ChannelFactory來調用服務。

EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service"); 
IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(),ep); 

我能夠正確調用服務契約並按預期檢索結果。

現在我想使用Add Service Reference創建代理。

我收到以下錯誤,當我在添加服務引用窗口

There was an error downloading 'http://localhost:8091/MiniCalcService/Service'. 
The request failed with HTTP status 400: Bad Request. 
Metadata contains a reference that cannot be resolved: 'http://localhost:8091/MiniCalcService/Service'. 
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8091/MiniCalcService/Service. The client and service bindings may be mismatched. 
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

我缺少什麼或者做錯了點擊Go?

+0

當您訪問.SVC在瀏覽器中它顯示了你的服務沒有提供有關它的信息如何使用一些例如什麼? – 2012-03-24 13:18:52

+0

沒有.svc文件。我只創建了一個Service.cs及其對應的IService.cs。請不要介意我的無知,但.svc文件是必要的,我們是否應該使用工具來生成代理並使用該服務? – Animesh 2012-03-24 13:25:35

+0

無知是如果你從未問過。沒有人知道一切。在某些時候,你需要從零開始。看到我的答案。 – 2012-03-24 13:50:27

回答

3

在您的ServiceHost中啓用元數據交換行爲。

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service), 
          new Uri("http://localhost:8091/MiniCalcService"))) 
{ 
    host.AddServiceEndpoint(typeof(MiniCalcService.IService), 
          new BasicHttpBinding(), 
          "Service"); 

    //Enable metadata exchange 
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
    smb.HttpGetEnabled = true; 
    host.Description.Behaviors.Add(smb); 

    host.Open(); 
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . "); 
} 

http://wcftutorial.net/WCF-Self-Hosting.aspx

+0

minmin,我已經添加了上面幾行來啓用元數據交換,但是在嘗試添加服務引用時仍然出現相同的錯誤。 – Animesh 2012-03-24 13:41:53

+0

minmin,你的答案適合我。我的錯誤是認爲端點和地址是一樣的。所以URL:'http:// localhost:8091/MiniCalcService/Service'是端點,應該在手動代理中使用,正如我在本文中描述的那樣,並且URL:'http:// localhost:8091/MiniCalcService'是地址,並應在「添加服務參考」窗口中使用。 – Animesh 2012-03-24 17:31:37

1

既然你沒有.SVC我想你一定在你的服務的.config有這樣的:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"> 
     <serviceActivations> 
      <add relativeAddress="Service.svc" service="MiniCalcService.Service" /> 
     </serviceActivations> 
    </serviceHostingEnvironment> 

之後,你需要一個選項,允許服務元數據:

<serviceMetadata httpGetEnabled="true" /> 

這有點複雜所以我勸你tu創建一個新的WCF S在一個新的解決方案中,你可以看到這個配置是怎樣的。所以你只需要做一些複製/粘貼配置。

該點之後:

http://localhost:8091/MiniCalcService/Service.svc

+0

Vitor,是否有可能在代碼而不是配置上實現上述功能?我問這是因爲我還沒有app.config文件。 – Animesh 2012-03-24 16:17:08

+0

您可以輕鬆添加一個app.config文件 - >添加 - >應用程序配置文件。如果你不想要這個配置,我想你可以用@minmin寫的這樣的方式達到這個目的。 – 2012-03-24 16:40:19

+0

我現在已經用minmin的代碼實現了我想要的。我在他的回答下發布了一個解釋。謝謝您的幫助。 – Animesh 2012-03-24 17:32:37

相關問題