2012-09-25 93 views
4

我對autofac和singalR庫都是全新的,所以請對我輕鬆點!我在bootstrapper中有下面的代碼,它在沒有signalR的情況下自行工作。Autofac + SignalR

var builder = new ContainerBuilder(); 
builder.RegisterControllers(Assembly.GetExecutingAssembly()); 
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); 

builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest(); 
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest(); 

builder.RegisterAssemblyTypes(typeof(adminRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest(); 

builder.RegisterAssemblyTypes(typeof(adminService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest(); 

builder.RegisterFilterProvider(); 
IContainer container = builder.Build(); 

DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container)); 

上面的代碼工作得很好,但註冊我的集線器後,下面的代碼,它只是不起作用。

builder.RegisterType<Chat>().InstancePerLifetimeScope(); 

builder.RegisterFilterProvider(); 
IContainer container = builder.Build(); 

DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container)); 

SignalR.IDependencyResolver resolver = new SignalR.Autofac.AutofacDependencyResolver(container); 
GlobalHost.DependencyResolver = resolver; 
RouteTable.Routes.MapHubs(resolver); 

我使用SignalR依賴解析器從這個結合Autofac(https://github.com/pszmyd/SignalR.Autofac)。

我有一個簡單的樞紐這樣,

public class Chat : Hub 
{ 
    private readonly IadminService adminService; 
    public Chat(IadminService adminService) 
    { 
     this.adminService = adminService; 
    } 

    public void Send(string message) 
    { 
     Clients.addMessage(message); 
    } 
} 

這是當我試圖用DI到輪轂上,我得到了錯誤。

「沒有範圍與標籤匹配的'httpRequest'從請求實例的範圍中可見。」

不管我做什麼,我似乎都無法使它工作,如果有人能告訴我上面的代碼有什麼問題,我將不勝感激。

非常感謝 利奧

+0

Btw - 您正在運行哪個版本的SignalR?明天我會詳細研究你的代碼,但我很確定這裏有些事情不對。請更正粘貼的代碼 - 第一個框中的最後一行似乎成爲孤兒:) –

+0

感謝您的回覆。我正在使用SignalR版本0.5.3,如果我將InstancePerHttpRequest更改爲InstancePerLifetimeScope,那麼很不幸,我不能再向數據庫添加新記錄(使用EF作爲數據層)。任何幫助將不勝感激,謝謝! – Leo

回答

1

設置DependencyResolver之後你還需要將它傳遞給MapHubs,那就是試試這個來代替:

GlobalHost.DependencyResolver = resolver; 

RouteTable.Routes.MapHubs(); 

我知道SignalR但從未使用Autofac但它可能是值得看的這些問題的答案對可能Autofac問題的詳細信息:

How to resolve Autofac InstancePerHttpRequest

Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible

+0

感謝您的快速回復。不幸的是,我仍然遇到了同樣的錯誤信息。 – Leo

3

對於託管在IIS的ASP.NET應用程序中添加這的Application_Start:

var container = AutofacConfig.BuildContainer(); 
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 
var signalRDependencyResolver = new SignalRAutofacDependencyResolver(container); 
// old SignalR 1.0 way - routes.MapHubs(signalRDependencyResolver); 
RouteTable.Routes.MapHubs(new HubConfiguration { Resolver = signalRDependencyResolver }); 

您可能會發現SignalRAutofacDependencyResolver here

例轂注入ISecurity服務:

public class ExampleHub : Hub 
{ 
    private static int _count = 0; 

    private readonly ISecurity _security; 
    public ExampleHub(ISecurity security) 
    { 
     _security = security; 
    } 

    public void GetCount() 
    { 
     _count++; 
     Clients.All.SetCount(_count);   
    } 
} 

這也適用於SignalR self host release。只需使用像這樣的啓動類:

// These are static variables in Program.cs - Probably a better way to do this 
_container = AutofacConfig.BuildContainer(); 
_webServer = WebApp.Start<WebServerStartup>("http://localhost:8080"); 

public class WebServerStartup 
{ 
    private readonly SignalRAutofacDependencyResolver _signalRDependencyResolver; 

    public WebServerStartup() 
    { 
     _signalRDependencyResolver = new SignalRAutofacDependencyResolver(_container); 
    } 

    public void Configuration(IAppBuilder app) 
    { 
     app.MapSignalR(new HubConfiguration { Resolver = _signalRDependencyResolver }); 
    } 
}