6

我正在尝试HttpContextBase在我的 SignalR 集线器中注入一个:

public class EventHub : Hub, IDisconnect
{
    private readonly HttpContextBase _httpContextBase;

    public EventHub(HttpContextBase httpContextBase)
    {
        _httpContextBase = httpContextBase;
    }

    [...]
}

注册码如下所示:

private static void InitAutofac()
{
    var builder = new ContainerBuilder();

    var assembly = typeof (MvcApplication).Assembly;

    builder.RegisterControllers(assembly).PropertiesAutowired();
    builder.RegisterModule(new AutofacWebTypesModule());
    builder.RegisterFilterProvider();

    builder.RegisterAssemblyTypes(assembly)
        .Where(InterfaceBasedInjectedClasses())
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();

    builder.RegisterAssemblyTypes(assembly)
        .Where(InterfaceLessInjectedClasses())
        .InstancePerLifetimeScope();

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

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

    var container = builder.Build();

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));


    GlobalHost.DependencyResolver = new SignalR.Autofac.AutofacDependencyResolver(container);

    RouteTable.Routes.MapHubs();
}

我得到的错误是:

[DependencyResolutionException:从请求实例的范围中看不到带有与“httpRequest”匹配的标记的范围。这通常表明注册为 per-HTTP 请求的组件正在被 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,始终从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime 请求依赖项,而不是从容器本身.]

堆栈跟踪:

Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope) +160
Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable`1 parameters) +57
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) +102
Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) +64
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +164
Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +14
Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) +70
Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Type serviceType, Object& instance) +70
SignalR.Autofac.AutofacDependencyResolver.GetService(Type serviceType) in D:\etc\Dev\SignalR.Autofac\AutofacDependencyResolver.cs:30
SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor) +60
SignalR.Hubs.DefaultHubManager.ResolveHub(String hubName) +27
SignalR.Hubs.HubDispatcher.CreateHub(IRequest request, HubDescriptor descriptor, String connectionId, TrackingDictionary state, Boolean throwIfFailedToCreate) +445
SignalR.Hubs.HubDispatcher.OnReceivedAsync(IRequest request, String connectionId, String data) +246
SignalR.<>c__DisplayClass6.<ProcessRequestAsync>b__4(String data) +29
SignalR.Transports.ForeverTransport.ProcessSendRequest() +63
SignalR.Transports.ForeverTransport.ProcessRequestCore(ITransportConnection connection) +70
SignalR.Transports.ForeverTransport.ProcessRequest(ITransportConnection connection) +5
SignalR.PersistentConnection.ProcessRequestAsync(HostContext context) +560
SignalR.Hubs.HubDispatcher.ProcessRequestAsync(HostContext context) +120
SignalR.Hosting.AspNet.AspNetHandler.ProcessRequestAsync(HttpContextBase context) +422       SignalR.Hosting.AspNet.HttpTaskAsyncHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +68
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我刚刚开始使用 autofac 和 SignalR,我发现自己有点卡住了。SO上的相关主题都没有帮助。知道我做错了什么吗?

4

1 回答 1

5

问题是这一行:

builder.RegisterModule(new AutofacWebTypesModule());

异常消息表明,在解析期间,正在尝试从标记为 的生命周期范围内解析某些内容httpRequest。当你注册一些东西时,你会在 Autofac 中得到这样的注册InstancePerHttpRequest()

// These two are roughly equivalent:
builder.RegisterType<SomeType>().InstancePerHttpRequest();
builder.RegisterType<SomeType>().InstancePerMatchingLifetimeScope("AutofacWebRequest");

如果您查看它的来源,AutofacWebTypesModule它将 Web 抽象(例如HttpContextBase,您正在寻找的东西)注册为InstancePerHttpRequest.

此外,如果您查看其Autofac.Integration.Mvc.AutofacDependencyResolver工作方式,每当您在请求期间解析类型时,它都会创建一个新的、嵌套的、命名的生命周期范围,并带有httpRequest. 这可以让你拥有那种魔力InstancePerHttpRequest

假设SignalR.Autofac您使用的库是这里的库,这也是NuGet 上的库),查看SignalR.Autofac.AutofacDependencyResolver没有为服务解析创建这样的嵌套/命名生命周期范围

因此,当 Autofac 尝试解决HttpContextBase依赖关系时,它找不到该httpRequest标记范围(因为它不存在)并发出您看到的错误。

对此没有简单的答案。嵌套httpRequest范围实际上很重要,因为它基本上不能存在于真实的 Web 请求之外。它使它“安全” -HttpContextBase如果没有网络上下文(例如,在应用程序启动时),您将无法获得。

如果您需要注入一个HttpContextBase并且您确定您EventHub只会为一个网络请求而存在,仅此而已(我不是 SignalR 人,所以请耐心等待),这意味着您需要:

  • 向 SignalR.Autofac 项目请求修复此问题。
  • 实现您自己的自定义 SignalR.Autofac.AutofacDependencyResolver,它以 MVC 解析器的方式处理事情。

如果没有实际完成这项工作并亲自进行测试,我无法真正提供有关如何完成这项工作的具体指导。这个练习留给读者。

于 2012-10-09T23:34:13.070 回答