2009-05-22 52 views
0

我有一個使用WCF和wsHttpBindings公開Web服務的Web應用程序。可以在不同的機器和不同的URL上安裝應用程序。這意味着每個WCF服務位置都會有所不同。使用WCF在運行時確定wsHttpBinding

我正在構建一個Windows服務,它將引用每個應用程序並執行任務。每個任務都需要調用Web應用程序上的服務。我知道綁定都是在app.config中設置的,但是有沒有更簡單的方法來動態調用服務,或者我將如何構造app.config?

<webApplication WebServiceUrl="http://location1.com/LunarChartRestService.svc" /> 
<webApplication WebServiceUrl="http://location2.com/LunarChartRestService.svc"/> 

回答

1

您的客戶端的配置文件可能看起來是這樣的:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <client> 
     <endpoint name="Endpoint1" 
       address="http://location1.com/LunarChartRestService.svc" 
       binding="wsHttpBinding" 
       contract="(whatever-your-contract-is)" /> 
     <endpoint name="Endpoint2" 
       address="http://location2.com/LunarChartRestService.svc" 
       binding="wsHttpBinding" 
       contract="(whatever-your-contract-is)" /> 
     <endpoint name="Endpoint3" 
       address="http://location3.com/LunarChartRestService.svc" 
       binding="wsHttpBinding" 
       contract="(whatever-your-contract-is)" /> 
    </client> 
    </system.serviceModel> 
</configuration> 

然後在代碼中,你可以根據它的名字創建這樣一個端點(客戶端代理),因此你可以選擇你需要的任何一個位置。沒有什麼能阻止你創建多個客戶端代理,無論是!因此,您可以使用多個客戶端代理連接到多個服務器端點,沒問題。或者,你當然也可以在代碼中創建一個「WsHttpBinding」和「EndpointAddress」的實例,並設置必要的屬性(如果有的話),然後用這個現成的對象調用客戶端代理的構造函數,從而覆蓋整個馬戲團的app.config和創造任何你覺得需要:

EndpointAddress epa = 
    new EndpointAddress(new Uri("http://location1.com/LunarChartRestService.svc")); 
WSHttpBinding binding = new WSHttpBinding(); 

馬克

0

從您的描述中,聽起來好像所有服務器都暴露相同的服務合同。如果是這樣,您可以在web.config中聲明多個endpoints,並根據端點名稱在運行時選擇一個。

當然,您可能不希望處理WCF配置的那部分內容,而只想更簡單的URL列表並完成它。這也是完全可能的;你只需要在代碼端做更多的工作來實例化客戶端代理/通道對象。

+0

你知道這樣做的「簡單的方法」的任何實例,並在訪問/使用多個端點運行? – mickyjtwin 2009-05-22 02:14:41

+0

Web應用程序上的服務設置也是wshttp,而不是基本的。 – mickyjtwin 2009-05-22 02:23:14