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.. :/
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.
Autofac Registration Concepts
Excerpt:
Any component type you register via
RegisterType
must be a concrete type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With