【问题标题】:Autofac cannot resolve DbContextAutofac 无法解析 DbContext
【发布时间】:2020-11-17 22:21:41
【问题描述】:

我从 Autofac 收到此错误消息;:

在“MyService`1[MyContext]”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的任何构造函数都不能被可用的服务和参数调用: 无法解析构造函数“Void .ctor(MyContext)”的参数“MyContext context”。

这行代码出现错误(下面代码也有):

IMyService myService = container.Resolve<IMyService>();  // error here

我有兴趣注意到,当我在我的注册中加入这一行时,一切正常:

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());

当我注册 AnyConcreteType... 时,这一切都有效,这让我相信我没有注册任何东西。我的问题是我没有注册什么?错误消息似乎将 MyContext 命名为罪魁祸首,但显然我正在注册它,如下所示。
我真的不想使用 AnyConcreteType... 因为我只想显式注册我需要的类。

我的服务是这样构建的:

public class MyService<T> : BaseService<T>, IMyService where T:DbContext,IMyContext
{
    public MyService(T context) : base(context)
    {
    }
}

MyService 派生自 BaseService:

public abstract class BaseService<T> : IDisposable where T:DbContext, IMyContext
{
    internal T db;

    public BaseService(T context)
    {
        db = context;
    }
}

MyContext 被传递给 MyService 并且构造如下:

public partial class MyContext : DbContext, IMyContext
{
    public MyContext(INamedConnectionString conn)
        : base(conn.ConnectionString)
    {
        Configuration.ProxyCreationEnabled = false;
    }
}

这里是命名连接字符串

public class NamedConnectionString : INamedConnectionString
{
    public string ConnectionString { get; set; }
}

这是我如何注册上述内容:

builder.RegisterType<MyService<MyContext>>().As<IMyService>();  
builder.RegisterType<NamedConnectionString>().As<INamedConnectionString>().SingleInstance();
builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
builder.RegisterType<DbContext>(); // is this necessary??

我是这样称呼它的:

    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    INamedConnectionString namedConnectionString = container.Resolve<INamedConnectionString>();
    namedConnectionString.ConnectionString = myConnectionString;
    IMyService myService = container.Resolve<IMyService>();  // error here

【问题讨论】:

    标签: c# autofac


    【解决方案1】:

    Autofac and ASP .Net MVC 4 Web API

    上面的线程是相关的。它没有回答问题,但它帮助我开始朝着正确的方向进行故障排除。解决方法在这里:

    我删除了这两行:

    builder.RegisterType<MyContext>().As<IMyContext>().InstancePerLifetimeScope();
    builder.RegisterType<DbContext>(); // is this necessary??
    

    并用这个替换它们:

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

    【讨论】:

      【解决方案2】:

      这对我有用

      builder.RegisterType().As().InstancePerLifetimeScope();

      【讨论】:

        【解决方案3】:

        通过使用builder.RegisterType&lt;InventoryContext&gt;().InstancePerLifetimeScope(); 得到这个工作。

        上下文:

        我有一个 DBContext 类,我有注入上下文的服务,然后有控制器来调用服务(接口)。

        然后在 global.asax 上,称为builder.RegisterType&lt;InventoryContext&gt;().InstancePerLifetimeScope();。这很好用,我已经有一段时间没有再次使用 MVC 项目了。

        如果有人对此有疑问,可以再次参考。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多