WCF Question when using ChannelFactory as part of a generic routine....

K

Kevin S. Goff

I may be trying to do something that can't be done (or "shouldn't" be
done), but here goes.

I'm trying to construct a generalized method for creating a client-side
factory channel using WCF. So that I can pass it any one of dozens of
interface contracts, and the method will pump out a proxy.

So essentially, I want to do this...

public T GetGeneralFactoryChannel<T>(T interfaceType)
{

Binding myBinding = new WSHttpBinding();
ChannelFactory<T> oChannelFactory = new ChannelFactory<T>(myBinding);
oChannelFactory.Endpoint.Address = new
EndpointAddress("http://MyServer//testwcf/service.svc");

T MyProxyChannel = oChannelFactory.CreateChannel();

return MyProxyChannel;
}

And then I can call it like so:

IMyContract MyContract;
MyContract = (IMyContract)GetGeneralFactoryChannel(typeof(IMyContract));
MyContract.CallSomeMethod();

The problem is that I get errors when the code tries to create a channel
factory..."The type argument passed to the generic ChannelFactory class
must be an interface type".

I tried using MakeGenericType, but that didn't help.

So anyway, has anyone tried to do something similar? (using
ChannelFactory inside something generic, so that you could pass any
contract interface?)

I was hoping to avoid reflection or Activator.GetObject, but if that's
the only way to get this to work...

Feel free to suggest an alternate approach....

Thanks,
Kevin
 
K

Kevin S. Goff

too much pizza tonight, must have killed my brain cells...figured it out...


public T GetGeneralFactoryChannel<T>()
{

Binding myBinding = new WSHttpBinding();
ChannelFactory<T> oChannelFactory = new ChannelFactory<T>(myBinding);
oChannelFactory.Endpoint.Address = new
EndpointAddress("http://MyServer//testwcf/service.svc");

T MyProxyChannel = oChannelFactory.CreateChannel();

return MyProxyChannel;
}

And then I can call it like so:

IMyContract MyContract;
MyContract = this.GetRemoteFactoryChannel<IUser>();
MyContract.CallSomeMethod();


Thanks to the following blog!
http://aabs.wordpress.com/2007/12/31/the-ambient-context-design-pattern-in-net/


Kevin
 
Top