【发布时间】:2014-02-04 11:08:25
【问题描述】:
问题:
我无法让 StructureMap 在 MongoRepostiory 命名空间中找到 IRepository 类型的默认实例。
异常信息:
"结构图异常代码:202 没有为 PluginFamily MongoDB.Driver.MongoUrl、MongoDB.Driver、Version=1.8.3.9、Culture=neutral、PublicKeyToken=f686731cfb9cc103 定义默认实例"
似乎 StructureMap 正在使用错误的构造函数实例化 MonogoRepository 类...
MonogoRepository 具有以下构造函数:
public class MongoRepository<T> : MongoRepository<T, string>, IRepository<T>, IRepository<T, string>, IQueryable<T>, IEnumerable<T>, IQueryable, IEnumerable where T : MongoRepository.IEntity<string>
{
public MongoRepository();
public MongoRepository(MongoUrl url);
public MongoRepository(string connectionString);
public MongoRepository(MongoUrl url, string collectionName);
public MongoRepository(string connectionString, string collectionName);
}
我想要公共的 MongoRepository(string connectionString);要调用的构造函数...但从异常消息看来,由于 StructureMap 正在尝试解析 MongoUrl,因此它没有调用所需的。
我想知道:
问题:
- 我是否设置了错误,导致调用了不正确的构造函数? 或
- 这是默认行为吗?如果是,我该如何更改它以确保调用所需的构造函数?
设置:
我有以下注册表类:
public class IocRegistry : Registry
{
public IocRegistry()
{
this.For(typeof(IRepository<>)).Use(typeof(MongoRepository<>))
.CtorDependency<string>("connectionString")
.Is("MongoServerSettings");
}
}
以及下面的Container初始化:
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.LookForRegistries();
scan.WithDefaultConventions();
});
});
return ObjectFactory.Container;
}
当我在其构造函数中放置断点并调试应用程序时,IocRegistry 肯定会受到影响。
使用 IRepository 的代码是:
public class ImageContentService : IImageContentService
{
private IRepository<ImageItem> imageRepository;
public ImageContentService(IRepository<ImageItem> imageRepository)
{
this.imageRepository = imageRepository;
}
}
ObjectFactory.Container.WhatDoIHave() 的相关输出(所有注册完成后)是:
===========================================================================================================
Configuration Sources:
0) Registry: StructureMap.InitializationExpression, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
1) Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
2) Registry: StructureMap.Configuration.DSL.Registry, StructureMap, Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
3) Registry: Mch.Core.IocRegistry, Mch.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
============================================================================================================================================================================================================================================================================================================================================
PluginType Name Description
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IRepository`1<T> (IRepository`1<T>) d26c76dd-8550-4d02-bf8a-25c6f0075346 Configured Instance of MongoRepository.MongoRepository`1, MongoRepository.Net45, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient
d26c76dd-8550-4d02-bf8a-25c6f0075346 Configured Instance of MongoRepository.MongoRepository`1, MongoRepository.Net45, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null
【问题讨论】:
-
ObjectFactory.Container.WhatDoIHave() 的输出是什么?
-
我已经添加了 ObjectFactory.Container.WhatDoIHave() 的输出
标签: mongodb structuremap mongodb-.net-driver