作爲一個選項,您可以配置服務代理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;
}
}
謝謝亞瑟.....! – Dany