加载配置时出现 Unity 错误:ObjectContext 类型具有多个长度为 1 的构造函数。无法消除歧义
你好,
我在使用 Unity 容器注册存储库时遇到问题。我有以下配置和类:
/* 通用存储库: */
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;
}
}
/* 具体存储库: */
public class SourceRepository : Repository<Source>
{
public SourceRepository(EntityContext context) : base(context) { }
}
/* 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="ObjectContext" mapTo="EntityContext" name="EntityContext">
<constructor/>
</register>
/* 注册通用存储库(I不确定是否需要注册) */
<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>
当我尝试加载配置时,出现错误:
依赖项解析失败,类型 = "BL.DataAccess.Repository.SourceRepository", name = "(没有任何)”。 while:解决时发生异常。 异常是: InvalidOperationException - ObjectContext 类型有多个长度为 1 的构造函数。无法消除歧义。
我理解此异常的含义,但我不知道我的配置中哪里有错误。
你能帮忙吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从异常情况来看,正在调用
Resolve
来进行SourceRepository
的未命名注册。确保您的配置将所有依赖于)。
SourceRepository
的类设置为使用正确的命名注册(通过或者删除源存储库注册中的名称,这样您最终会得到:
By the looks of the exception,
Resolve
is being called for an unnamed registration forSourceRepository
.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: