keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp > 05/2008

 #1  
05-23-08, 06:32 AM
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(IMyCo ntract));
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
 #2  
05-23-08, 06:48 AM
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!
[url down]


Kevin
 #3  
05-23-08, 07:58 AM
Marc Gravell
Another option:
[url down]

Marc
 #4  
05-23-08, 07:38 PM
Kevin S. Goff
Marc Gravell wrote:
> Another option:
> [..]
>
> Marc




Thanks, Marc! I actually use typed datasets, so that will come in
handy. Thanks!

Kevin
Similar Threads
Generic Copy paste routine

Is there a generic copyFile API available on mac which can take care of all kinds of FSRefs(network and local) ? FsCopyFile routine in MoreFilesX.cpp seems to be converting...

Part 1 of Short Steps Toward Generic Programming: The GENERIC keyword

As I mentioned in the introduction, this part of my generic programming proposal is separate from the rest. It represents a syntactic convenience that make code more legible...

Generic Error Handling Routine

Hi, I am writing a Generic Error handling routine for my project which traps all possible errors in the project and stores them into a Log file. I am unable to retrieve the...

CODE: Generic Sort Routine

I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def...

Generic Error Handling routine

"Savvoulidis Iordanis" <iordanis_sav> wrote in message news:a556 > How can I provide a generic error handling routine on every form > to handle common errors, like PKey,...


All times are GMT. The time now is 07:09 AM. | Privacy Policy