我在发送信号器和autofac集成时遇到麻烦。我试图让他们两个都和一个简单的mvc 5项目一起玩。我将使用一个简单的示例。首先,我有一个信号中心,我总是从客户端调用它,因为它是在_Layout.cshtml
中设置的public class BookHub : Hub
{
private readonly IUserService UserService;
public BookHub(IUserService userService)
{
this.UserService = userService;
}
public void InvokeMe()
{
}
}
每次都在呼叫集线器,我想注入用户服务界面。然后我有一个简单的控制器,在其中我还想注入相同的接口
public class UserController : Controller
{
IUserService UserService;
public UserController(IUserService userService)
{
this.UserService = userService; ;
}
public ActionResult Index()
{
return View();
}
}
每次我调用集线器和要注入IUserService接口的控制器
因此,为了实现此目的,请使用2种方法。一个为signalr注册依赖项的人
public static class SignalR_Autofac
{
public static void RegisterSignalR(ContainerBuilder builder)
{
// Register Autofac resolver into container to be set into HubConfiguration later
builder.RegisterType<AutofacDependencyResolver>()
.As<Microsoft.AspNet.SignalR.IDependencyResolver>()
.SingleInstance();
// Register ConnectionManager as IConnectionManager so that you can get
// hub context via IConnectionManager injected to your service
builder.RegisterType<Microsoft.AspNet.SignalR.Infrastructure.ConnectionManager>()
.As<Microsoft.AspNet.SignalR.Infrastructure.IConnectionManager>()
.SingleInstance();
//register services
builder.RegisterType<TestService>().As<ITestService>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();
// Register hubs one by one
builder.RegisterType<AuthorHub>().ExternallyOwned();
builder.RegisterType<BookHub>().ExternallyOwned();
}
}
和一个用于mvc
public static class Bootstrapper
{
public static void SetAutofacMVCContainerLess(ContainerBuilder builder)
{
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
builder.RegisterModule(new AutofacDataModule());
builder.RegisterModule(new AutofacServiceModule());
builder.RegisterFilterProvider();
}
}
所以我最终会遇到这样的startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
IContainer container = AutofacConfig(app);
app.UseAutofacMiddleware(container);
System.Web.Mvc.DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
var hubConfiguration = new Microsoft.AspNet.SignalR.HubConfiguration()
{
Resolver = container.Resolve<Microsoft.AspNet.SignalR.IDependencyResolver>(),
};
app.MapSignalR(hubConfiguration);
}
private static IContainer AutofacConfig(IAppBuilder app)
{
var builder = new ContainerBuilder();
Bootstrapper.SetAutofacMVCContainerLess(builder);
SignalR_Autofac.RegisterSignalR(builder);
return builder.Build();
}
}
在每个http请求上,接口都被注入到我的控制器和集线器中。我想知道是否有更好的方法将IUserService注册一次,而不是两次将其注册为mvc的InstancePerRequest和一次作为signer的InstancePerLifetimeScope。如您所见,我还必须两次注册其他一些依赖项,例如IUnitOfWork,IDbFactory和IUserRepository。
信号器注册的部分解决方案来自here