最近使用SimpleInjector的AutoFac新手,但由于我已升级到vNext / .NET5,因此AutoFac似乎已更新以使用它,因此请改用AutoFac。
我想要做的就是确保在每个请求上创建我的DbContext。我可以使用.InstancePerRequest()
来做到这一点,但当我这样做时(我已添加到我的所有代码中),我似乎收到了错误消息
Autofac.Core.DependencyResolutionException没有带标记的范围 匹配' AutofacWebRequest'从范围可见 请求实例。这通常表示一个组件 a请求注册为每个HTTP请求 SingleInstance()组件(或类似场景。)在Web下 集成始终请求来自的依赖项 DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime, 从来没有从容器本身。
我相信这是说根据HTTP请求没有请求某些内容,所以什么都不会?是对的吗?如果是这样,有点似乎有点愚蠢。我不在乎我的服务和验证器只创建一次,但我需要在每个请求上创建DbContext,因为当多个请求连接到我的WebApi时,我不断收到错误
第二个操作在此前一个上下文之前开始 异步操作完成。
但是,如果我自己一个接一个地执行这些调用,那么它们都可以正常工作。
我使用的是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()
我的错误会停止,但我不认为这是我想要的所有对象
UPDATE 这是stacktrace
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()
答案 0 :(得分:0)
似乎发生了两件事中的一件或两件:
AutofacWebRequest
的新生命周期范围。InstancePerRequest()
的生命周期范围的生命周期范围内已注册为AutofacWebRequest
的组件。查看堆栈跟踪,似乎第一种情况比第二种情况更可能,因为在构造控制器时发生了异常。
答案 1 :(得分:0)
简短的回答是,你不能有更长的生命取决于短暂的生命
示例:每个请求的实例依赖单个实例是错误的
所以典型的默认值是每个依赖项的实例(瞬态,因此每次需要一个实例时,都会创建一个新实例)。
如果每个请求都有实例A B是单例
然后A取决于B,没问题 但是如果B依赖于A,则会遇到问题,因为B永远不会被重新创建,而取决于A将要在每个范围内重新创建。
其他库有时会让您犯此错误并独自发现(非常痛苦)
似乎AutoFact在让您知道作用域层次结构有问题的方面做得更好。