autofac with signalr no parameterless constructor defined for this object

autofac with signalr no parameterless constructor defined for this object

我在当前的 Asp 项目中使用 autofac,一切正常,直到我决定在 signalR Hub 中使用依赖注入

这是我的创业公司 class

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
        builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerLifetimeScope();
        //builder.RegisterHubs(Assembly.GetExecutingAssembly());
        builder.RegisterType<DiscussionHub>();
        // Repositories
        builder.RegisterAssemblyTypes(typeof(LanguagesRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces().InstancePerRequest();
        // Services
        builder.RegisterAssemblyTypes(typeof(LanguageService).Assembly)
           .Where(t => t.Name.EndsWith("Service"))
           .AsImplementedInterfaces().InstancePerRequest();

        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        var config = new HubConfiguration
        {
            Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container)
        };
        app.UseAutofacMiddleware(container);
        AutoMapperConfiguration.Configure();
        app.MapSignalR("/signalr",config);
    }
}

这是我的中心

public class DiscussionHub : Hub
{
    private readonly IDiscussionService _discussionService;
    public DiscussionHub(IDiscussionService discussionService)
    {
        _discussionService = discussionService;
    }}

错误是我的集线器上没有无参数构造函数?有什么建议吗?!

你应该注册你的 hub ExternallyOwned 它应该自己管理 lifetimescope。这意味着 autofac 不会处理它们。

其次,一切都将从您中心的根容器中解决。这意味着 Per DependencyPer LifeTimeScope 将与您的中心一起使用(永远与应用程序一起使用)。所以你应该在你的中心管理生命周期。

即使我们管理您中心的生命周期,Per Request 也不会得到支持。因此,当我们创建新的 lifetimescope 时,我们将使用 AutofacWebRequest 标签创建它。这样,我们就可以解析您的 Per Request 实例。但请注意,此实例与正常请求生命周期范围内的其他实例完全不同

你的 Hub 应该是这样的:

public class DiscussionHub  : Hub
{
  private readonly ILifetimeScope _hubLifetimeScope;
  private readonly IDiscussionService  _discussionService;

  public MyHub(ILifetimeScope lifetimeScope)
  {
    // Create a lifetime scope for the hub.
    _hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");

    // Resolve dependencies from the hub lifetime scope.
    _discussionService = _hubLifetimeScope.Resolve<IDiscussionService>();
  }

  protected override void Dispose(bool disposing)
  {
    // Dipose the hub lifetime scope when the hub is disposed.
    if (disposing && _hubLifetimeScope != null)
    {
      _hubLifetimeScope.Dispose();
    }
    base.Dispose(disposing);
  }
}

你的登记表应该是这样的:

  .
  .
  builder.RegisterType<DiscussionHub>().ExternallyOwned();
  var container = builder.Build();
  GlobalHost.DependencyResolver = new   Autofac.Integration.SignalR.AutofacDependencyResolver(container);
  .
  .

Owin 集成:

public void Configuration(IAppBuilder app)
  {
    var builder = new ContainerBuilder();

    // STANDARD SIGNALR SETUP:

    // Get your HubConfiguration. In OWIN, you'll create one
    // rather than using GlobalHost.
    var config = new HubConfiguration();

    // Register your SignalR hubs.
    builder.RegisterHubs(Assembly.GetExecutingAssembly());

    // Set the dependency resolver to be Autofac.
    var container = builder.Build();
    config.Resolver = new AutofacDependencyResolver(container);

    // OWIN SIGNALR SETUP:

    // Register the Autofac middleware FIRST, then the standard SignalR middleware.
    app.UseAutofacMiddleware(container);
    app.MapSignalR("/signalr", config);
  }

查看更多detail.