How can I host multiple WCF MVC services in MVC?

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

I can do IoC using StructureMap in services and inject my constructor dependencies using the example that Jimmy Bogard wrote about here :

Jimmy's article is great, but I'm trying to extend it to work with several services hosted in the same MVC application . In fact, the part below is the part that causes me some 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); } } 

Using the WCF single service service - routing MVC requests to a specific URL through StructureMapServiceHostFactory shown above works brilliantly - but - if (for example) I create StructureMapServiceHostFactory2 to call /services/bar to allow the use of a different registry when the MVC application spins , it, in turn, calls each factory when it starts through RouteConfig.cs and adds routes, so in the end I do not get the configured instances that the first ServiceHostFactory should provide.

It doesnโ€™t matter if I call Initialize(); or trying to grab the Container property and call Configure on it.

Am I hiding something from this? The main reason for the need to isolate the registry is due to a different NHibernate configuration, but I can configure named instances of SessionFactory and Session for NHibernate, and then use one registry to get around this. In my opinion, I wanted the WCF service and MVC hosting to be able to isolate their IoC containers, so I went along this route.

Is there a way I can do this?

+6
source share
1 answer

Well, so it would seem that the only person able to answer this question was me because of rethinking and "restructuring" the solution so that the problem does not exist in the first place.

Now I have a way to organize these services and maintain IoC using StructureMap neatly for each service without any conflicting problems.

If you find yourself in a similar position when capturing SOA (SOATO?) - take a step back - this is a good start;)

0
source

All Articles