Dynamic ChannelFactory Building
I am trying to dynamically create a ChannelFactory:
var serviceType = GetServiceProxy();
var interfaceType = serviceType.GetServiceInterface(); //return IServiceInterface
var service = new ChannelFactory(binding, address);
the problem, as you can see, is on the second line where I don't have a generic type and unfortunately ChannelFactory doesn't have an overload that takes a type.
Anyway around him?
source to share
It turned out that I can only do this with reflection. Of course, you also need to call methods using reflection.
to create a "ChannelFactory" and call the "CreateChannel" method:
private ChannelFactory CreateChannelFactory()
{
var channelFactoryType = typeof (ChannelFactory);
channelFactoryType = channelFactoryType.MakeGenericType(serviceType);
return (ChannelFactory)Activator.CreateInstance(channelFactoryType, binding, address);
}
private object CreateChannel()
{
var createchannel = channelFactory.GetType().GetMethod("CreateChannel", new Type[0]);
return createchannel.Invoke(channelFactory, null);
}
Now the channel is created, but since only the interface type is available, I can only receive method calls:
var serviceType = service.GetType();
var remoteMethod = service.GetMethod(invocation.Method.Name);
remoteMethod.Invoke(service, invocation.Arguments);
source to share
Hadi : is this forum post here (check Roman Kiss's answer by submitting a custom ) address you are looking for? ChannelFactory2
If so, you can stop reading my answer :-)
Well, normally you would do this:
1) have your service interface (IMyServiceInterface)
2) create / get binding and endpoint information
3) Create a factory channel for this interface:
ChannelFactory<IMyServiceInterface> myChannelFactory =
new ChannelFactory<IMyServiceInterface>(myBinding, myEndpoint);
4) from this factory channel, create your client proxy:
IMyServiceInterface client = myChannelFactory.CreateChannel();
5) Calling methods for this client:
client.DoStuff();
So which part is you wanting to be more general or more dynamic, and why? What is the motivation / driving force behind this idea?
Mark
source to share