gpt4 book ai didi

c# - 从范围内看不到标签匹配 'AutofacWebRequest' 的范围

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:59 24 4
gpt4 key购买 nike

AutoFac 的新功能,因为最近使用的是 SimpleInjector,但自从我升级到 vNext/.NET5 以来,AutoFac 似乎已更新以使用它,因此改用 AutoFac。

我想要做的就是确保在每个请求上创建我的 DbContext。我可以通过使用 .InstancePerRequest() 来做到这一点,但是当我这样做时(我已将其添加到我的所有代码中),我似乎收到了错误消息

Autofac.Core.DependencyResolutionException No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested 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.

我相信这是在说没有按照 HTTP 请求请求某些东西,所以什么都不会?是对的吗?如果是这样,似乎有点愚蠢。我不关心我的服务和验证器是否只创建一次,但我需要在每个请求上创建 DbContext,因为当多个请求连接到我的 WebApi 时,我不断收到错误提示

A second operation started on this context before a previous asynchronous operation completed.

但是,如果我自己一个接一个地执行这些调用,那么它们都可以正常工作。

我使用的是 EntityFramework 6.1,而不是 EF7,因为 EF7 还远未完成。

我有我的 AutoFac 模块,像这样

public class SetupModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
// IDataContext
builder.Register((c) =>
{
var appEnv = c.Resolve<IApplicationEnvironment>();
var configBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json");
var configuration = configBuilder.Build();

return new DataContext(configuration["Data:MyConnection:ConnectionString"]);
})
.As<IDataContext>()
.InstancePerRequest();

// Validators
builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetAssembly(typeof(IValidator)))
.Where(t => typeof(IValidator).IsAssignableFrom(t))
.AsImplementedInterfaces()
.InstancePerRequest();

// Services
builder.RegisterType<ContentService>().As<IContentService>().InstancePerRequest();

}
}

我的 DataContext 就像一个带有构造函数的标准 DbContext 类,只是采用 connectionString

public DataContext : IDataContext {
public DataContext(string connectionString)
: base(connectionString)
{

}
}

在我的 Startup.cs 文件中,我使用这个进行注册

public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Create the Autofac container builder.
var builder = new ContainerBuilder();
builder.RegisterModule(new AutoFac.SetupModule());
builder.Populate(services);

// Build the container.
var container = builder.Build();

// Resolve and return the service provider.
return container.Resolve<IServiceProvider>();
}

有人能准确地告诉我我错过了什么吗?这是否与我创建 DataContext 的方式有关,因为需要这样做才能从 config.json 文件中获取 connectionString。

提前致谢

注意:如果我更改为 .InstancePerDependency(),我的错误就会停止,但我不认为这就是我想要的所有对象

更新这是堆栈跟踪

at Autofac.Core.Lifetime.MatchingScopeLifetime.FindScope(ISharingLifetimeScope mostNestedVisibleScope) 
at Autofac.Core.Resolving.InstanceLookup..ctor(IComponentRegistration registration, IResolveOperation context, ISharingLifetimeScope mostNestedVisibleScope, IEnumerable<Parameter> parameters)
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable<Parameter> parameters)
at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable<Parameter> parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable<Parameter> parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable<Parameter> parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable<Parameter> parameters, Object& instance)
at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable<Parameter> parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable<Parameter> parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)
at Autofac.Framework.DependencyInjection.AutofacServiceProvider.GetService(Type serviceType)
at Microsoft.Framework.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
.lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNet.Mvc.DefaultTypeActivatorCache.CreateInstance<TInstance>(IServiceProvider serviceProvider, Type implementationType)
at Microsoft.AspNet.Mvc.DefaultControllerActivator.Create(ActionContext actionContext, Type controllerType)
at Microsoft.AspNet.Mvc.DefaultControllerFactory.CreateController(ActionContext actionContext)
at Microsoft.AspNet.Mvc.Core.ControllerActionInvoker.CreateInstance()
at Microsoft.AspNet.Mvc.Core.FilterActionInvoker.<InvokeAllActionFiltersAsync>d__49.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Mvc.Core.FilterActionInvoker.<InvokeExceptionFilterAsync>d__48.MoveNext()
--- exception rethrown ---
at Microsoft.AspNet.Mvc.Core.FilterActionInvoker.<InvokeAsync>d__41.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Mvc.MvcRouteHandler.<InvokeActionAsync>d__7.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Mvc.MvcRouteHandler.<RouteAsync>d__6.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.<RouteAsync>d__10.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Hosting.Internal.HostingEngine.<>c__DisplayClass29_0.<<Start>b__0>d.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Loader.IIS.RuntimeHttpApplication.<ProcessRequestAsyncImpl>d__13.MoveNext()
--- exception rethrown ---
at Microsoft.AspNet.Loader.IIS.RuntimeHttpApplication.<ProcessRequestAsyncImpl>d__13.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.AspNet.Loader.IIS.HttpApplicationBase.<InvokeProcessRequestAsyncImpl>d__9.MoveNext()

最佳答案

简短的回答是,你不能让长寿的东西依赖于短命的东西

示例:单个依赖于每个请求的实例是错误的

因此典型的默认值是每个依赖项的实例( transient ,因此每次您需要一个时都会创建一个新的)。

如果你有一个 A 是每个请求的实例B是单例

然后A依赖B,没问题但是,如果 B 依赖于 A,您会遇到问题,因为 B 永远不会被重新创建,但依赖于 A,而 A 将在每个范围内重新创建。

其他libs有时会让你犯这个错误,自己找(很痛苦)

似乎 AutoFact 在让您知道范围层次结构出现问题方面做得更好。

关于c# - 从范围内看不到标签匹配 'AutofacWebRequest' 的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105853/

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