asp.net mvc 4 - Using one Dependency Resolver for SignalR and standard MVC controllers? -
currently using unity 3.x ioc. using unity.mvc4 library manage lifetime of resolver. here resolver looks like:
namespace wfm.core.common.mvc.unity { public class wfmdependencyresolver : unitydependencyresolver { public wfmdependencyresolver(iunitycontainer container) : base(container) { } private static wfmdependencyresolver _wfmgrabbrresolver; public static wfmdependencyresolver instance { { return _wfmgrabbrresolver ?? (_wfmgrabbrresolver = new wfmdependencyresolver(instancelocator.instance.container)); } } } }
the unitydependencyresolver
comes unity.mvc4 library. in globabl.asax.cs file setting resolver this:
dependencyresolver.setresolver(wfmdependencyresolver.instance);
here singleton instancelocator class:
public class instancelocator { private static instancelocator _instance; public iunitycontainer container { get; private set; } private instancelocator() { container = new unitycontainer(); } public static instancelocator instance { { return _instance ?? (_instance = new instancelocator()); } } public t resolve<t>() { try { return wfmdependencyresolver.instance.getservice<t>(); } catch(exception ex) { return default(t); } } public t resolvewithoutmanager<t>() { try { return container.resolve<t>(); } catch (exception) { throw; } } }
this works mvc controllers, solution allow application resolve inside hub controllers along mvc controllers. currently, created singleton class allows me resolve types manually. can resolve types inside hubs using class this:
instancelocator.instance.resolve<isomeinterface>();
while works, not ideal development standpoint. reason being, want types injected , not manually instantiated. hubs , controllers inside same mvc application , not want have separate them right now.
there's entire article devoted dependency injection in signalr: http://www.asp.net/signalr/overview/extensibility/dependency-injection
so have write custom dependency resolver signalr simple wrapper commonly shared unity container.
Comments
Post a Comment