c# - Autofac Keyed/Named 方法不提供服务映射函数的重载
问题描述
该As
方法提供带Func<Type, Type> serviceMapping
参数的重载,但Keyed
andNamed
方法不提供。它们仅分别提供Func<Type, object> serviceKeyMapping
和Func<Type, string> serviceNameMapping
参数。
但是,我想注册一组类型,RegisterAssemblyTypes
对所有类型使用相同的键,但使用由类型本身确定的不同接口。我期待找到一个方法重载,例如Keyed(object serviceKey, Func<Type, Type> serviceMapping)
or Keyed(Func<Type, object> serviceKeyMapping, Func<Type, Type> serviceMapping)
。
这是 API 设计中的疏忽吗?还是我错过了什么?
解决方案
API似乎没有这样的功能。但是,您可以将As(Func<Type, Service> serviceMapping)
重载与KeyedService
对象一起使用。
例如
builder.RegisterAssemblyTypes(typeof(Parent).Assembly)
.Where(t => t.IsAssignableTo<ICommon>())
.As(t => new KeyedService(keyObject, t.GetType().GetInterfaces()[0]));
KeyedService
在Autofac.Core
命名空间中。没有NamedService
对象,但您可以将 aKeyedService
与 a 一起使用string
推荐阅读
- c# - 如何在 C# 应用程序中强制使用 ODBC 版本 2.x
- c# - 无法在存储库中注入 DbContext
- java - Spring Boot 测试如何使用不同的 application.properties 进行集成测试
- c# - 弹性搜索:如何提取与查询短语/字符串匹配的所有“字段名称”
- kotlin - 如何在 Ktor 中使请求绑定的数据在全球范围内可用?
- c - 有没有办法读取C中括号内用逗号分隔的2个整数,忽略空格
- python - 如何使用python检测列表中的不同元素并从中创建新列表(查找)
- c++ - 无法取消引用指针
- python - 使用 pandas 读取 CSV 文件时无法正确对齐行和列
- three.js - 如何在 Threejs 中获取更新的缓冲区属性