gpt4 book ai didi

asp.net-mvc-3 - 无法创建请求生命周期范围,因为 HttpContext 不可用

转载 作者:行者123 更新时间:2023-12-02 22:38:49 25 4
gpt4 key购买 nike

尝试使用一些异步非 httprequest 设置 AutoFac 时遇到困难。

我在 App_Start 上有以下内容

        var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<sfEntities>().As<IUnitOfWork>().InstancePerHttpRequest();
builder.RegisterGeneric(typeof(sfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
builder.RegisterGeneric(typeof(BaseServices<>)).As(typeof(IBaseServices<>)).InstancePerHttpRequest();
builder.RegisterType<EmailServices>().As<IEmailServices>().InstancePerHttpRequest();
builder.RegisterType<UserServices>().As<IUserServices>().InstancePerHttpRequest();
builder.RegisterType<ChatServices>().As<IChatServices>().InstancePerHttpRequest();
builder.RegisterType<DefaultFormsAuthentication>();
builder.RegisterType<WebSecurity>();
builder.RegisterType<Chat>();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

如果我更改为 InstancePerLifetimeScope(),我会遇到 UnitofWork.SaveChanges() 的问题。以这种方式设置工作正常,除了异步调用。

p.s.:UnitOfWork 在服务之间传递 EF DbContext 以确保使用相同的实例并正确处理。如果我更改为 InstancePerLifetimeScope,我在调用 .SaveChanges() 时遇到身份冲突,可能是因为应该有多个 UnitOfWork 实例。

以下代码抛出以下异常:

Timer timer = new Timer(new TimerCallback(OnTimer), null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));

private static void OnTimer(object o)
{
using (var timerScope = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope())
{
var chatServices = timerScope.Resolve<IChatServices>();
chatServices.MarkInactiveUsers();
}
}

No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.

在 SignalR 上,以下代码引发以下异常:

SignalR.GlobalHost.DependencyResolver.Register(typeof(Chat), () => new Chat(DependencyResolver.Current.GetService<IUnitOfWork>(), DependencyResolver.Current.GetService<IChatServices>(), DependencyResolver.Current.GetService<IUserServices>()));

The request lifetime scope cannot be created because the HttpContext is not available

提前致谢!

最佳答案

Having a hard time trying to setup AutoFac with some async non httprequest.

对于非 http 请求,或者更具体地说,对于非 ASP.NET 管道请求(如 WCF 或 ServiceStack),您绝对应该将所有 InstancePerHttpRequest() 代码更改为 InstancePerLifetimeScope( )。您可以而且应该这样做,因为 InstancePerLifetimeScope() will make it resolvable in both ASP.NET pipeline and non-ASP.NET pipeline contexts .

If I change to InstancePerLifetimeScope() I get problems with UnitofWork.SaveChanges(). Setup this way works fine except for async calls... If I change to InstancePerLifetimeScope I was getting identity conflicts when calling .SaveChanges(), probably because there should be more than one instance of UnitOfWork.

是的,应该有多个 UnitOfWork 实例,但您可以通过一个范围为 InstancePerLifetimeScope() 的注册来实现:

例子:

builder.RegisterType<NhUnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

关于asp.net-mvc-3 - 无法创建请求生命周期范围,因为 HttpContext 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com