AutoFac.Core.DependencyResolutionException after following tutorial
Solution 1
Anyone know what might be causing this error?
builder.RegisterType<IDomainRepository>().As<IDomainRepository>();
^^
should be
builder.RegisterType<DomainRepository>().As<IDomainRepository>();
^
because you need to RegisterType
of the Concrete type, not the interface.
Excerpt:
Any component type you register via
RegisterType
must be a concrete type.
Solution 2
I know that the question was resolved. But I had the same error and it was from a different source.
My registered class had a constructor in which a service is injected. This service was not registered.
All dependency chain needs to be registered.
cleastie
Updated on June 15, 2022Comments
-
cleastie almost 2 years
New to Autofac, followed a tutorial on Youtube(with great ratings) but its throwing an exception, no idea why.
Exceptions:
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
And
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); }
DomainRepository
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; } } }
DomainHandler
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; }
Anyone know what might be causing this error? Read about forgetting to set public access on the interface but thats not the issue here.. :/
-
itcoder over 3 yearsHi @ihebiheb, did you manage to resolve this issue? I think I am having a similar one now.
-
ihebiheb over 3 yearsyes. This error says that you have some classes that aren't known by autofac. So you need to register all the classes that need to be injected AND ALL their dependencies. Example if your injected class has a service injected in its constructor, you need to inject it too. if this last class have an other service injected in its constructor, you need to inject it too also.
-
itcoder over 3 yearsMmm that makes sense, I've put this part of my project to the side for now. But thats good insight when I pick it up again later. Thanks!