Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an interface from a WCF service

I have some .NET remoting code where a factory method, implemented in some server side class, returns interfaces to concrete objects, also executing on the very same server. .NET remoting automagically creates proxies and allows me to pass the interfaces across to the client, which can then call them directly.

Example interfaces:

public interface IFactory
{
    IFoo GetFoo();
}

public interface IFoo
{
    void DoSomething();
}

Example client code:

...
IFactory factory = (IFactory) System.Activator.GetObject (typeof (IFactory), url);
...
IFoo foo = factory.GetFoo ();  // the server returns an interface; we get a proxy to it
foo.DoSomething ();
...

This all works great. However, now I am trying to migrate my code to WCF. I wonder if there is a means to pass around interfaces and having WCF generate the proxies on the fly on the client, as does the original .NET remoting.

And I don't want to return class instances, since I don't want to expose real classes. And serializing the full instance and sending it back and forth between the server and the client is not an option either. I really just want the client to talk to the server object through an interface pointer/proxy.

Any ideas?

like image 262
Pierre Arnaud Avatar asked Nov 13 '08 21:11

Pierre Arnaud


2 Answers

Use Sessions instead of Client-Activated Objects (MSDN)

like image 70
Brian Avatar answered Oct 20 '22 03:10

Brian


Sorry, jezell, I don't get it.

Yes, I can use ChannelFactory on the client to create a proxy to IFactory, since that singleton object has been "published" by the server through an URI on the ServiceHost.

But my IFoo instances on the server have not been associated with any ServiceHost; I just want to get them back by calling my IFactory proxy on the client, and let WCF do the call to the server IFactory, which would provide some IFoo, which would then be marshalled back to the client and wrapped into a dynamically generated proxy. I really just want to be able to write factory.GetFoo (); on my client...

In the meantime, Brian pointed me to a very interesting document I had overlooked on MSDN, which explains how to mimmick the .NET Remoting interface marshalling by using sessions and EndPointAddress10 and ... as you wrote, ChannelFactory to get the client side proxies.

So, now I know how to replicate my .NET remoting code, but paying a relatively high cost for it. The code complexity involved with WCF is quite a bit higher than with the plain old .NET remoting solution.

like image 26
Pierre Arnaud Avatar answered Oct 20 '22 02:10

Pierre Arnaud