2013-01-23 68 views
0

我正在Silverlight中創建一個應用程序。該應用程序的XAP文件夾包含ServiceReferencesClientConfig文件。我在web服務器上部署了該應用程序,並且每當我從其他機器訪問該網站時,如(http://192.168.1.15/SampleApplication/Login.aspx) ,我想將該IP地址(192.168.1.15)寫入ServiceReferencesClientConfig,然後將Xap文件下載到客戶端。但我沒有想到通過編程編輯ServiceReferencesClientConfig文件。 (我想在更改部署應用程序的Web服務器的IP地址時進行更改,因此應該自動更改ServiceReferencesClientConfig,因此不需要手動更改ServiceReferencesClientConfig文件。)通過編程在Silverlight中編輯服務引用客戶端配置文件

回答

1

作爲一個選項,您可以配置服務代理dinamically,改變默認的構造函數使用dinamically產生的端點和綁定,或使用一個工廠做相同的:

public MyService() 
     : base(ServiceEx.GetBasicHttpBinding(), ServiceEx.GetEndpointAddress<T>()) 
{ 
} 



public static class ServiceEx 
{ 
    private static string hostBase; 
    public static string HostBase 
    { 
     get 
     { 
      if (hostBase == null) 
      { 
       hostBase = System.Windows.Application.Current.Host.Source.AbsoluteUri; 
       hostBase = hostBase.Substring(0, hostBase.IndexOf("ClientBin")); 
       hostBase += "Services/"; 
      } 
      return hostBase; 
     } 
    } 

    public static EndpointAddress GetEndpointAddress<TServiceContractType>() 
    { 
     var contractType = typeof(TServiceContractType); 

     string serviceName = contractType.Name; 

     // Remove the 'I' from interface names 
     if (contractType.IsInterface && serviceName.FirstOrDefault() == 'I') 
      serviceName = serviceName.Substring(1); 

     serviceName += ".svc"; 

     return new EndpointAddress(HostBase + serviceName); 
    } 

    public static Binding GetBinaryEncodedHttpBinding() 
    { 
     // Binary encoded binding 
     var binding = new CustomBinding(
      new BinaryMessageEncodingBindingElement(), 
      new HttpTransportBindingElement() 
      { 
       MaxReceivedMessageSize = int.MaxValue, 
       MaxBufferSize = int.MaxValue 
      } 
     ); 

     SetTimeouts(binding); 

     return binding; 
    } 

    public static Binding GetBasicHttpBinding() 
    { 
     var binding = new BasicHttpBinding(); 
     binding.MaxBufferSize = int.MaxValue; 
     binding.MaxReceivedMessageSize = int.MaxValue; 

     SetTimeouts(binding); 

     return binding; 
    } 
} 
+0

謝謝亞瑟.....! – Dany