小编Bri*_*Lam的帖子

具有KeyFilter属性的构造函数的Autofac配置

我正在尝试使用Autofac配置来创建服务对象.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

不知何故,使用以下配置,它无法正确解析ISimpleMongoClient参数.我还缺少什么?

{
  "components": [
    {
      "type": "Service, TestProject",
      "services": [
        {
          "type": "IService, TestProject"
        }
      ],
      "instanceScope": "single-instance"
    },
    {
      "type": "SimpleMongoClient, TestProject",
      "services": [
        {
          "type": "ISimpleMongoClient, TestProject",
          "key": "eod"
        }
      ],
      "parameters": …
Run Code Online (Sandbox Code Playgroud)

configuration json autofac

2
推荐指数
1
解决办法
1029
查看次数

标签 统计

autofac ×1

configuration ×1

json ×1