Autofac - How to resolve instances registered using JSON Configuration

967 views Asked by At

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

There are 1 answers

3
Travis Illig On

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: