2013-01-23 41 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