【发布时间】:2020-11-15 21:22:48
【问题描述】:
我有一个包含多个类实现的接口。我已经在 Autofac Container 中注册了它们。我的问题是如何解决特定课程的问题?
界面
public interface IAccountDataStorage
{
Account GetAccount(string accountNumber);
void UpdateAccount(Account account);
}
实现的类
public class BackupAccountDataStore : IAccountDataStorage
{
...
}
public class AccountDataStore : IAccountDataStorage
{
...
}
在容器中注册
这行不通!
builder.RegisterType<AccountDataStore>().As<IAccountDataStorage>().InstancePerRequest();
builder.RegisterType<BackupAccountDataStore>().As<IAccountDataStorage>().InstancePerRequest();
现在我想解析到一个特定的类
// this does not work for me as it will pick itself one of the
// above class.. need help here
var paymentService = buildContainer.Resolve<IPaymentService>();
【问题讨论】:
-
这能回答你的问题吗? Resolving Generic Interface with Autofac
-
另外,您是否阅读过涵盖此内容的Autofac documentation?
-
不,它对我不起作用,因为我需要在 resolve 的决定类中进行验证
-
我按照本教程的注册类型和resolveTyped,总是得到BackupAccountDataStore(第二个)..dotnetfalcon.com/versioning
标签: c# dependency-injection autofac .net-4.8