Reputation: 26762
Normally i would instantiate a SoapClient like this:
public static TestWSSoapClient Test()
{
string endpoint = "endpoint";
var soapClient = new TestWSSoapClient(endpoint);
return soapClient;
}
But i'd like to use a more generic approach:
public static ICommunicationObject SoapClient<TSoap>()
{
string endpoint = "endpoint";
var soapClient = new TSoap(endpoint);
return soapClient;
}
But obviously you cannot create an instance like that from a generic type. How can i create this instance and still pass the endpoint
string to it?
Upvotes: 1
Views: 1185
Reputation: 5121
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);
Upvotes: 1