gpt4 book ai didi

c# - 遵循教程后的 AutoFac.Core.DependencyResolutionException

转载 作者:行者123 更新时间:2023-11-30 22:56:52 24 4
gpt4 key购买 nike

Autofac 的新手,遵循了 Youtube 上的教程(收视率很高)但它抛出异常,不知道为什么。

异常(exception)情况:

DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IDomainRepository (ReflectionActivator), Services = [Solution.Entities.IDomainRepository], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope

NoConstructorsFoundException: No accessible constructors were found for the type 'Solution.Entities.IDomainRepository'.

IDomainRepository

public interface IDomainRepository
{
List<Domain> GetAll();

string Insert(Domain obj);

bool Update(Domain obj);

bool Delete(string URL);
}

域资源库

public class DomainRepository : IDomainRepository
{
public List<Domain> GetAll()
{
using (IDbConnection db = new SqlConnection(Helper.ConnectionString))
{
if (db.State == ConnectionState.Closed)
{
db.Open();
}
return db.Query<Domain>("SELECT * FROM Domains", commandType: CommandType.Text).ToList();

}
}

public string Insert(Domain obj)
{
using (IDbConnection db = new SqlConnection(Helper.ConnectionString))
{
if (db.State == ConnectionState.Closed)
{
db.Open();
}
db.Query<Domain>("INSERT INTO Domains (Domain) VALUES ("+obj.URL+")", commandType: CommandType.Text);
return obj.URL;

}
}

public bool Update(Domain obj)
{
using (IDbConnection db = new SqlConnection(Helper.ConnectionString))
{
if (db.State == ConnectionState.Closed)
{
db.Open();
}
int result = db.Execute("UPDATE Domains SET Domain="+obj.URL+" WHERE Domain="+obj.URL+")", commandType: CommandType.Text);
return result != 0;

}
}

public bool Delete(string URL)
{
using (IDbConnection db = new SqlConnection(Helper.ConnectionString))
{
if (db.State == ConnectionState.Closed)
{
db.Open();
}
int result = db.Execute("delete from Domains where Domain = @Url", new { Url = URL }, commandType: CommandType.Text);
return result != 0;
}
}
}

域处理器

static Autofac.IContainer _container;

static DomainHandler()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<IDomainRepository>().As<IDomainRepository>();
_container = builder.Build();
}

public static bool Delete(string Url)
{
return _container.Resolve<IDomainRepository>().Delete(Url);
}

public static List<Domain> GetAll()
{
return _container.Resolve<IDomainRepository>().GetAll();
}

public static Domain Save(Domain obj, EntityState state)
{
if (state == EntityState.Added)
obj.URL = _container.Resolve<IDomainRepository>().Insert(obj);
else
_container.Resolve<IDomainRepository>().Update(obj);

return obj;
}

任何人都知道可能导致此错误的原因是什么?阅读有关忘记在界面上设置公共(public)访问权限的信息,但这不是这里的问题..:/

最佳答案

Anyone know what might be causing this error?

builder.RegisterType<IDomainRepository>().As<IDomainRepository>();
^^

应该是

builder.RegisterType<DomainRepository>().As<IDomainRepository>();
^

因为您需要 Concrete 类型的 RegisterType,而不是接口(interface)。

Autofac Registration Concepts

摘录:

Any component type you register via RegisterType must be a concrete type.

关于c# - 遵循教程后的 AutoFac.Core.DependencyResolutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991719/

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