通常我会像这样实例化一个SoapClient:
public static TestWSSoapClient Test()
{
string endpoint = "endpoint";
var soapClient = new TestWSSoapClient(endpoint);
return soapClient;
}
但我想使用更通用的方法:
public static ICommunicationObject SoapClient<TSoap>()
{
string endpoint = "endpoint";
var soapClient = new TSoap(endpoint);
return soapClient;
}
但显然你无法从泛型类型创建这样的实例。如何创建此实例并仍将endpoint
字符串传递给它?
答案 0 :(得分:1)
public static T GetChannel<T>(Binding binding, EndpointAddress address)
{
var channelFactory = new ChannelFactory<T>(binding, address);
var channel = channelFactory.CreateChannel();
return channel;
}
var binding = BindingFactory.GetBindingX();
var address = new EndpointAddress("endpoint");
Program.GetChannel<IMyInterface>(binding, address);
channel.DoStuff(parameters);