2013-11-14 121 views
1

嘗試在MVC Web應用程序中使用Unity解析SignalR中心時,出現了令人討厭的ResolutionFailedException錯誤。我的代碼顯示在下面。使用Unity的SignalR Hub ResolutionFailedException

我的引導程序類

public static class Bootstrapper 
{ 
    public static IUnityContainer Initialise() 
    { 
     var container = BuildUnityContainer(); 
     var unityDependencyResolver = new UnityDependencyResolver(container); 

     //Used for MVC 
     DependencyResolver.SetResolver(unityDependencyResolver); 

     //Used for SignalR 
     GlobalHost.DependencyResolver = new SignalRUnityDependencyResolver(container); 

     return container; 
    } 

    private static IUnityContainer BuildUnityContainer() 
    { 
     var container = new UnityContainer(); 

     // register all your components with the container here 
     // it is NOT necessary to register your controllers 

     // e.g. container.RegisterType<ITestService, TestService>(); 

     RegisterTypes(container); 

     return container; 
    } 

    public static void RegisterTypes(IUnityContainer container) 
    { 

     //Repositories 

     container.RegisterType<ChatMessageRepository>(); 


     //Services 
     container.RegisterType<CapturePoolService>(); 
     container.RegisterType<HistoricalDataService>(); 

     //Context 
     container.RegisterType<ICustomPrincipal, CustomPrincipal>(new PerRequestLifetimeManager()); 
     container.RegisterType<IDatabaseFactory, DatabaseFactory>(new PerRequestLifetimeManager()); 
     container.RegisterType<UnitOfWork>(new PerRequestLifetimeManager()); 

     //Hubs 
     container.RegisterType<ChatHub>(new InjectionFactory(CreateChatHub)); 

    } 

    private static object CreateChatHub(IUnityContainer container) 
    { 
     return new ChatHub(container.Resolve<ChatMessageRepository>(), container.Resolve<UnitOfWork>()); 
    } 

} 

我聊天樞紐

public class ChatHub : Hub 
{ 
    private readonly ChatMessageRepository _chatMessageRepository; 
    private readonly UnitOfWork _unitOfWork; 

    public ChatHub(
     ChatMessageRepository chatMessageRepository, 
     UnitOfWork unitOfWork) 
    { 
     _chatMessageRepository = chatMessageRepository; 
     _unitOfWork = unitOfWork; 
    } 

}

現在,我有每個使用聊天集線器的一個實例一些不同的看法(比如想它作爲獨立的聊天室,它們都使用了寫在我的集線器中的相同邏輯)。當我進入這些「房間」時,我的邏輯按預期工作。但是,當我離開該頁面並轉到我的應用程序中的任何其他頁面時,就會出現問題。這裏的錯誤:

Resolution of the dependency failed, type = "Core.Repositories.ChatMessageRepository", name = "(none)".

Exception occurred while: while resolving.

Exception is: InvalidOperationException - Operation is not valid due to the current state of the object.


At the time of the exception, the container was:

Resolving Repositories.ChatMessageRepository,(none)

Resolving parameter "dbFactory" of constructor Repositories.ChatMessageRepository(Library.IDatabaseFactory dbFactory)

Resolving Context.DatabaseFactory,(none) (mapped from Library.IDatabaseFactory, (none))

從我SignalRUnityDependencyResolver

public override Object GetService(Type serviceType) 
    { 
     return _container.IsRegistered(serviceType) ? _container.Resolve(serviceType): base.GetService(serviceType); 
    } 

明確了 「_container.Resolve(的serviceType)」 呼叫觸發。

有人可以幫忙嗎?

謝謝。

回答

0

我通過使用HierarchicalLifetimeManager註冊我的集線器來解決這個問題。