Autofac-如何解析使用JSON配置注册的实例 [英] Autofac - How to resolve instances registered using JSON Configuration
问题描述
我们的要求是为不同的用户定义线程使用不同的Logger实例.
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"
}
}
] }
我创建了这个JSON并将其注册为一个模块,以便为每个值18122和18105进行单独注册.但是我不知道如何在运行时解析它们以及在哪里解析.
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.
我创建了2个文件夹,但记录器未写入其文件,它也写入了其他文件. 简而言之,我通过解析得到的实例不是该特定值的实例.
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.
如果我将其注入构造函数中,则只有18105得到解决.
If I inject it in the constructor, only 18105 is getting resolved.
有人可以帮忙吗!
谢谢!
推荐答案
您正在注册两个具有相同服务类型的实例,并且仅按类型解析一个实例,因此您的意思是正确的.您将无法通过这样的配置来完成您想做的事情.
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.
查看以下文档:
- Configuration - can tell you about what config can and can't do
- Picking a service by context - explains ways to work around your challenge
这篇关于Autofac-如何解析使用JSON配置注册的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!