本文介绍了加载配置时的Unity错误:ObjectContext类型有多个长度为1的构造函数。无法消除歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hell o,



使用unity容器注册存储库时出现问题。我有以下配置和类:



/ *通用存储库:* /

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

public ObjectContext上下文
{
get {return _ctx; }
}

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

/ *混凝土库:* /

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


b $ b

/ * EF生成的上下文:* /

  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();
}
}

/ *注册EntityContext类型, /

 < register type =ObjectContextmapTo =EntityContextname =EntityContext> 
< constructor />
< / register>

/ *注册通用仓库(我不知道是否需要注册) / p>

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

/ *注册具体仓库。我想注入EntityContext类型的构造函数依赖性* /

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

当我尝试加载配置时遇到错误:



依赖关系的解析失败,type =BL.DataAccess.Repository.SourceRepository,name =(none)。
在解析时发生异常。
异常是:InvalidOperationException - 类型ObjectContext有多个长度为1的构造函数。无法消除歧义。



我理解这个异常意味着什么,我不知道在我的配置中有什么错误。



您能帮忙吗?



解决方案

通过异常的外观, Resolve SourceRepository 的未命名注册。



请确保您的配置设置所有依赖 SourceRepository 使用正确的命名注册(通过< param>< dependency name =SourceRepository/>< / param> )。



或删除源存储库注册中的名称,因此您最终会得到:

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


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.

解决方案

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>

这篇关于加载配置时的Unity错误:ObjectContext类型有多个长度为1的构造函数。无法消除歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 12:02