Autofac - How to resolve instances registered using JSON Configuration

纵饮孤独 提交于 2019-12-11 16:02:46

问题


Our requirement is to have different Logger instances for different user defined threads.

{
  "defaultAssembly": "Framework",
  "components": [
{
  "type": "SynapseMiddleware.Core.Framework.LoggerServicePerContext, Framework",
  "services": [
    {
      "type": "SynapseMiddleware.Core.Framework.ILoggerServicePerContext, Framework"
    }
  ],
  "parameters": {
    "loggerConfig": "18105"
  }
},
{
  "type": "SynapseMiddleware.Core.Framework.LoggerServicePerContext, Framework",
  "services": [
    {
      "type": "SynapseMiddleware.Core.Framework.ILoggerServicePerContext, Framework"
    }
  ],
  "parameters": {
    "loggerConfig": "18122"
  }
}

] }

I created this JSON and registered it as a Module so that there is individual registration for each of the values - 18122 and 18105. But I do not understand as to how do I resolve them during runtime and where exactly.

I get 2 folders created but the logger does not write to its file, it writes to the other file as well. in short, the instance that I get by resolving is not the instance for that particular value.

If I inject it in the constructor, only 18105 is getting resolved.

Could someone help please!

Thanks!


回答1:


You're registering two instances of the same service type and resolving just one - by type - so what you're saying is expected. You won't be able to do what you want via config like this.

Check out these docs:

  • Configuration - can tell you about what config can and can't do
  • Picking a service by context - explains ways to work around your challenge


来源:https://stackoverflow.com/questions/46353207/autofac-how-to-resolve-instances-registered-using-json-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!