Yes, it is possible through Generics:
public static T CreateProxyChannel<T>()
{
string endpointUri = GetServiceEndpoint(typeof(T));
ChannelFactory<T> factory = new ChannelFactory<T>(myBinding, new EndpointAddress(new Uri(endpointUri)));
return factory.CreateChannel();
}
And the GetServiceEndpoint
method to return the endpoint based on the type of T:
private static string GetServiceEndpoint(Type service)
{
string serviceTypeName = service.Name;
// Code to get and return the endpoint for this service type
}
Note that in this case, I expect the config file to have an endpoint associated with the service type name (e.g. IService1
and http://localhost/Service1.svc
).
And finally to use it:
IService1 serviceProxy1 = CreateProxyChannel<IService1>();
serviceProxy1.MyMethod();
IService2 serviceProxy2 = CreateProxyChannel<IService2>();
serviceProxy2.AnotherMethod();