[英]Autofac cannot resolve DbContext
I am receiving this error message from Autofac;:我从 Autofac 收到此错误消息;:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyService`1[MyContext]' can be invoked with the available services and parameters: Cannot resolve parameter 'MyContext context' of constructor 'Void .ctor(MyContext)'.在类型 'MyService`1[MyContext]' 上找到的带有 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 的构造函数都不能用可用的服务和参数调用:无法解析构造函数 'Void .ctor 的参数 'MyContext context' (MyContext)'。
The error occurs on this line of code (also shown in the code below):错误发生在这行代码上(也显示在下面的代码中):
IMyService myService = container.Resolve<IMyService>(); // error here
I am interested to note that when I include this line in my registrations, everything works:我有兴趣注意到,当我在我的注册中包含这一行时,一切正常:
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
The fact that it all works when I register AnyConcreteType... leads me to believe I am not not registering something.当我注册 AnyConcreteType 时,这一切都有效...这一事实让我相信我没有注册某些东西。 My question is what am I not registering?我的问题是我没有注册什么? The error message appears to name MyContext as the culprit but clearly I am registering it as shown below.该错误消息似乎将 MyContext 命名为罪魁祸首,但显然我正在按如下所示进行注册。
I really dont want to use the AnyConcreteType... catch all because I want to explicity register only the the classes I need.我真的不想使用 AnyConcreteType ......因为我只想明确注册我需要的类。
My service is constructed like this:我的服务是这样构建的:
public class MyService<T> : BaseService<T>, IMyService where T:DbContext,IMyContext
{
public MyService(T context) : base(context)
{
}
}
MyService derives from BaseService: MyService 派生自 BaseService:
public abstract class BaseService<T> : IDisposable where T:DbContext, IMyContext
{
internal T db;
public BaseService(T context)
{
db = context;
}
}
MyContext is passed to MyService and is constructed like this: MyContext 传递给 MyService 并构造如下:
public partial class MyContext : DbContext, IMyContext
{
public MyContext(INamedConnectionString conn)
: base(conn.ConnectionString)
{
Configuration.ProxyCreationEnabled = false;
}
}
Here is NamedConnectionString这是 NamedConnectionString
public class NamedConnectionString : INamedConnectionString
{
public string ConnectionString { get; set; }
}
Here is how I register the above:以下是我注册上述内容的方法:
builder.RegisterType<MyService<MyContext>>().As<IMyService>();
builder.RegisterType<NamedConnectionString>().As<INamedConnectionString>().SingleInstance();
builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
builder.RegisterType<DbContext>(); // is this necessary??
And here is how I call it:我是这样称呼它的:
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
INamedConnectionString namedConnectionString = container.Resolve<INamedConnectionString>();
namedConnectionString.ConnectionString = myConnectionString;
IMyService myService = container.Resolve<IMyService>(); // error here
Autofac and ASP .Net MVC 4 Web API Autofac 和 ASP .Net MVC 4 Web API
The above thread is related.上面的线程是相关的。 It does not answer the question but it helped me start troubleshooting in the right direction.它没有回答问题,但它帮助我开始朝着正确的方向进行故障排除。 The solution is here:解决方案在这里:
I removed these two lines:我删除了这两行:
builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
builder.RegisterType<DbContext>(); // is this necessary??
and replaced them with this one:并用这个替换它们:
builder.RegisterType<MyContext>().InstancePerLifetimeScope();
This worked for me这对我有用
builder.RegisterType().As().InstancePerLifetimeScope(); builder.RegisterType().As().InstancePerLifetimeScope();
Got this working by using builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
通过使用builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
得到这个工作. .
Context:背景:
I have a DBContext class, I have services that injects the Context and then have Controllers to call the Services (Interface).我有一个 DBContext 类,我有注入上下文的服务,然后有控制器来调用服务(接口)。
And then on global.asax, called the builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
然后在 global.asax 上,调用了builder.RegisterType<InventoryContext>().InstancePerLifetimeScope();
. . This worked well, been a while since I worked with an MVC project again.这很有效,自从我再次使用 MVC 项目以来已经有一段时间了。
If there's anyone having issues with this you can refer to this again.如果有人对此有疑问,您可以再次参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.