Unity error while loading configuration: The type

2019-09-01 07:23发布

问题:

Hell o,

I have a problem to register a repository with unity container. I have a following configuration and classes:

/* Generic repository: */

public class Repository<E> : IRepository<E> where E : EntityObject
{
    private readonly ObjectContext _ctx;

    public ObjectContext Context
    {
        get { return _ctx; }
    }

    public Repository(ObjectContext context)
    {
       _ctx = context;            
    }
}   

/* Concrete repository: */

public class SourceRepository : Repository<Source>
{
    public SourceRepository(EntityContext context) : base(context) { }    
}

/* EF generated context: */

public partial class EntityContext : ObjectContext
{
    public EntityContext() : base("name=EntityContext", "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    public EntityContext(string connectionString) : base(connectionString, "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    public EntityContext(EntityConnection connection) : base(connection, "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }
}   

/* Registration of EntityContext type with constructor without parameters */

<register type="ObjectContext" mapTo="EntityContext" name="EntityContext">
    <constructor/>
</register>

/* Registration for generic repository (I'm not sure if this registration is needed) */

<register type="IRepository[]" mapTo="Repository[]" name="Repository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>

/* Registration for concrete repository. I want to inject constructor dependency of EntityContext type */

<register type="IRepository[Source]" mapTo="SourceRepository" name="SourceRepository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>

When I'm trying to load configuration I'm getting error:

Resolution of the dependency failed, type = "BL.DataAccess.Repository.SourceRepository", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type ObjectContext has multiple constructors of length 1. Unable to disambiguate.

I understand what this exception means, but I don't know where I have an error in my configuration.

Can you help please?

thanks.

回答1:

By the looks of the exception, Resolve is being called for an unnamed registration for SourceRepository.

Either ensure your configuration sets up all classes which depend on SourceRepository to use the correct named registration (through <param><dependency name="SourceRepository" /></param>).

Or remove the name in the source repository registration, so you'd end up with:

<register type="IRepository[Source]" mapTo="SourceRepository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>