w00
w00

Reputation: 26762

Create instance of generic soapClient

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

Answers (1)

Janne Matikainen
Janne Matikainen

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

Related Questions