2013-02-22 94 views
1

我有一個Windows Azure雲服務項目,其中包含多個Web角色以承載一些服務。我想使用ServiceX in ServiceY(每個在上運行,不同角色)使用相對 URL。配置WCF客戶端端點以使用相對地址(適用於Azure)

,這是我主持ServiceX方式:

<service name="ServiceX"> 
    <endpoint address="" binding="basicHttpBinding" contract="ServiceX" /> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
</service> 

現在我想用在ServiceY該服務。一個絕對的URL它工作得很好:

<system.serviceModel> 
    <client> 
     <endpoint name="ServiceXProxy" 
      address="http://mycloudservice.cloudapp.net:8080/ServiceX.svc" 
      binding="basicHttpBinding" 
      contract="ServiceX"/> 
... 

但是我怎麼能ServiceY ServiceY與相對地址?這不可能,因爲他們在同一個雲服務上運行?

回答

1

你可以以編程方式使用相對地址,但你仍然需要知道的基地址(或只使用localhost:8080爲基礎) - 這是不可能通過web.config除非你建立一個自定義的配置使用相對地址或利用AppSettings

// create bindings & endpoints 
var baseAddress = System.ConfigurationManager.AppSettings["baseAddress"]; 
var binding = new System.ServiceModel.BasicHttpBinding(); 
var endpoint = new EndpointAddress(baseAddress + "/ServiceX.svc"); 

您還可以加載從web.config客戶端點地址,並覆蓋使用UriBuilder類似手段的基址。