1
我想使用Autofac配置创建一个服务对象。Autofac配置与KeyFilter构造属性
public class Service : IService
{
public Service([KeyFilter("eod")]ISimpleMongoClient eodClient,
[KeyFilter("live")]ISimpleMongoClient liveClient
) : base(config)
{
_eodClient = eodClient;
_liveClient = liveClient;
}
}
public class SimpleMongoClient : ISimpleMongoClient
{
public SimpleMongoClient(string connectionString, string database)
{
IMongoClient client = new MongoClient(connectionString);
MongoDatabase = client.GetDatabase(database);
}
}
不知何故,使用以下配置,它无法正确解析ISimpleMongoClient参数。我还有什么遗漏?
{
"components": [
{
"type": "Service, TestProject",
"services": [
{
"type": "IService, TestProject"
}
],
"instanceScope": "single-instance"
},
{
"type": "SimpleMongoClient, TestProject",
"services": [
{
"type": "ISimpleMongoClient, TestProject",
"key": "eod"
}
],
"parameters": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary",
"database": "eod"
},
"instanceScope": "single-instance"
},
{
"type": "SimpleMongoClient, TestProject",
"services": [
{
"type": "ISimpleMongoClient, TestProject",
"key": "live"
}
],
"parameters": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary",
"database": "live"
},
"instanceScope": "single-instance"
}
]
}
感谢您的澄清。你认为如果值得通过配置使其可行吗?我应该提出这个功能要求吗? –
不,配置不应该是与编程设置功能匹配的功能。如果您需要这样做,推荐的解决方案是编写带有额外编程位的Autofac模块并配置模块。有关示例,请参阅文档。 –