How can I host multiple IoC-driven WCF services in MVC?
Asked Answered
T

1

6

I have around 6 WCF services that I want to host in an MVC application, routing requests to /services/foo to WcfFooService and /services/bar to WcfBarService

I can accomplish IoC with StructureMap within the services and inject my constructor dependencies by using the example that Jimmy Bogard blogged about here:

Jimmy's article is great, but I'm trying to extend it to work with multiple services hosted within the same MVC application. Essentially, the part at the bottom is the part that is causing me a few headaches:

public class StructureMapServiceHostFactory : ServiceHostFactory
{
    public StructureMapServiceHostFactory()
    {
        ObjectFactory.Initialize(x => x.AddRegistry<FooRegistry>());
        //var iTriedThisToo = ObjectFactory.Container;
        //container.Configure(x => x.[etc]);
    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new StructureMapServiceHost(serviceType, baseAddresses);
    }
}

With a single WCF service - routing MVC requests to a specific url via the StructureMapServiceHostFactory shown above works brilliantly - but - If (for example) I create a StructureMapServiceHostFactory2 for the /services/bar call, to allow for a different Registry to be used, when the MVC app spins up, it appears to call each factory in turn as it runs through RouteConfig.cs and adds the routes, so ultimately I don't get configured instances that the first ServiceHostFactory should provide.

It doesn't make a difference if I call Initialize(); or attempt to grab the Container property and call Configure on it, either.

Am I on a hiding to nothing with this? The major reason for requiring registry isolation is due to different NHibernate configuration, but I could configure Named instances of SessionFactory and Session for NHibernate purposes and then use a single registry to get around this. In my mind I wanted the WCF service and MVC-hosting to be capable of using their own IoC containers in isolation, which is why I went down this route.

Is there any way that I can accomplish this?

Tellus answered 16/3, 2013 at 8:38 Comment(2)
6 WCF services? Wow! I hope you're using message based communication with those WCF services (as this article explains), otherwise it would probably be pretty painful.Ceiling
Interesting article Steven! Thanks for sharing, we certainly could do with a neater architecture like that. It might also be the enabler for what I need....will give it a close lookTellus
T
0

Ok, so it would appear the only person capable of answering this was me, by virtue of a re-think and 're-architecting' the solution so that the problem doesn't exist in the first place.

I now have a capable way of hosting these services and maintaining IoC with StructureMap neatly per service, without any conflicting concerns.

If you find yourself in a similar position with SOA taking over (SOATO?) - taking a step back is a good start ;)

Tellus answered 13/5, 2013 at 15:28 Comment(1)
What was the re-architectured solution?Delisle

© 2022 - 2024 — McMap. All rights reserved.