Unity/WCF object instantiation

Jan 29, 2009 at 3:59 PM
Hello all!
I'm new to Unity and IoC in general, but I really like what I see so far. I've been looking into whether Unity supports object instantiation through WCF, and what would need to be done for that. From what I understand, this would basically require programmatically creating a new StaticFactoryExtension, configuring this StaticFactoryExtension to use a custom factory you create; this factory would be communicating with a WCF service to grab the object from there.
1. I tried implementing this, but ran into issues at the Resolve step because the type of the WCF-returned object didn't match the type that Unity was expecting (Basically, My.Custom.IInterface != MyServiceReference.MyServiceClient.ObjectImplementingInterface)... Should I be using Unity on the WCF side as well to be returning the instance?
2. Is there a better way of doing this?
3. Does it even make sense to do this? 

Thanks for your time!

Jan 29, 2009 at 6:02 PM
Try this way. It works very well.

http://initializecomponent.blogspot.com/2008/06/integrating-unity-with-wcf.html

Chester
Jan 30, 2009 at 9:41 PM
Thanks for the link! Is it correct to say that the link you gave resolves dependencies that a WCF service might require? That's what I'm understanding from it, at least. I'm trying to achieve something slightly different: Use unity at the web application (WCF client) side and try to resolve an object, hitting a WCF service to pull back that object. This WCF service has no dependencies (necessarily) that need to be injected; it just needs to be able to provide an object back to a Unity container, which would then give the object to the web application. 

Or is it that I'm not understanding the link correctly? Maybe what I want to do can be accomplished by artificially making the WCF service be dependant upon those objects?

I know this might be expensive to do with deserialization/serialization, network traffic, etc., but was curious if it was possible.

Thanks again!
Jan 30, 2009 at 10:41 PM
Edited Jan 30, 2009 at 10:50 PM
My.Custom.IInterface and MyServiceReference.MyServiceClient.ObjectImplementingInterface are totally different types. So their objects are different although their contents are same.

What you are using in your web client is a generated WCF proxy. This proxy has more than one constructors, which make Unity's resolution failed. So you need a derived class to explicitly declare your injection constructor.

public class DerivedClient: MyServiceClient
{
     [InjectionConstructor]
     public DerivedClient(){}
}

then you can register it

unityContainer.RegisterType<ObjectImplementingInterface, DerivedClient>();

Hope it helps


Chester.
Feb 2, 2009 at 7:29 PM
You don't need a derived type just to set the constructor. Just use the container API or config file to set it:

container.RegisterType<MyServiceClient>(new InjectionConstructor());

But that's not what the original poster is asking for, I believe. You want resolving an object to call a WCF service, correct? To do that, you've got the right basic approach: Use the static factory extension.

Having said that, I really don't think this is a particularly good idea. WCF return values aren't really objects, they're documents that get presented as objects to the rest of the .NET code. As such, there's not much DI potential. I'd personally get the client proxy from the container and then use that to call my WCF stuff directly rather than hiding it in the container.



Feb 2, 2009 at 7:40 PM
Thanks for the correction of this:

container.RegisterType<MyServiceClient>(new InjectionConstructor());

I really appreciate it.

Chester
Feb 3, 2009 at 7:35 PM
Thanks, ctavares and chesteryang, for taking the time to help out! 

ctavares, I'm currently going down the route you've suggested, and it looks like it's going to work out for me. This stuff is pretty fun to work with :)
Thanks!