tags:

views:

228

answers:

2

I'm trying to dynamically creating the ChannelFactory :


var serviceType = GetServiceProxy();
var interfaceType = serviceType.GetServiceInterface(); //return IServiceInterface
var service = new ChannelFactory(binding, address);

the problem is, as you can see, on the second line, where I don't have the generic type, and unfortunately, ChannelFactory does not have an overload that accepts the Type.

Any way around it??

A: 

Hadi: does this forum post here (check out the answer by Roman Kiss, presenting a custom ChannelFactory2 class) address what you're looking for??

If so, you can stop reading my reply :-)

Well, typically you would do this:

1) have your service interface (IMyServiceInterface)

2) create / retrieve your binding and endpoint information

3) Create a channel factory for that interface:

ChannelFactory<IMyServiceInterface> myChannelFactory =
    new ChannelFactory<IMyServiceInterface>(myBinding, myEndpoint);

4) from that channel factory, create your client proxy:

IMyServiceInterface client = myChannelFactory.CreateChannel();

5) Call methods on that client:

client.DoStuff();

So which part is it that you want to make more generic / more dynamic, and why?? What's the motivation / driving force behind that idea??

Marc

marc_s
The idea behind it is complicated. I've a set of existing classes, implementing the same interface locally, I want to reroute the calls to these interfaces to their equivalent implementation on a remote machine, hosting them as a WCF serice. that's why it is more dynamic. All I have is the type of the interface, and the methods available on the type info.
Hadi Eskandari
A: 

Found that I can only do this with reflection. Of course you also have to call the methods using reflection.

to create the "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 just the interface type is available, I only can get the methods to invoke:


var serviceType = service.GetType();
var remoteMethod = service.GetMethod(invocation.Method.Name);

remoteMethod.Invoke(service, invocation.Arguments);

Hadi Eskandari