Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoFac.Core.DependencyResolutionException after following tutorial

Tags:

c#

autofac

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.. :/

like image 713
cleastie Avatar asked Dec 31 '18 21:12

cleastie


2 Answers

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.

like image 135
Erik Philips Avatar answered Sep 22 '22 16:09

Erik Philips


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.

like image 26
ihebiheb Avatar answered Sep 22 '22 16:09

ihebiheb