在C#Autofac中,如何从具有多个具体类的单个接口解析类

在C#Autofac中,如何从具有多个具体类的单个接口解析类,c#,dependency-injection,autofac,.net-4.8,C#,Dependency Injection,Autofac,.net 4.8,我有一个多类实现的接口。我已经在Autofac容器中注册了它们。我的问题是如何解决特定类的问题 接口 public interface IAccountDataStorage { Account GetAccount(string accountNumber); void UpdateAccount(Account account); } 实现类 public class BackupAccountDataStore : IAccountDataStorage { .

我有一个多类实现的接口。我已经在Autofac容器中注册了它们。我的问题是如何解决特定类的问题

接口

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();
builder.RegisterType().As().InstancePerRequest();
builder.RegisterType().As().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>(); 
//这对我不起作用,因为它将自己选为
//课余时间。。这里需要帮助吗
var paymentService=buildContainer.Resolve();

我找到了答案

 builder.RegisterType<AccountDataStore>().Keyed("defaultAccountStorage", typeof(IAccountDataStorage));
 builder.RegisterType<BackupAccountDataStore>().Keyed("backupAccountStorage", typeof(IAccountDataStorage));


var a = buildContainer.ResolveKeyed<IAccountDataStorage>("defaultAccountStorage");
var b = buildContainer.ResolveKeyed<IAccountDataStorage>("backupAccountStorage");
builder.RegisterType().Keyed(“defaultAccountStorage”,typeof(IAccountDataStorage));
builder.RegisterType().Keyed(“backupAccountStorage”,typeof(IAccountDataStorage));
var a=buildContainer.ResolveKeyed(“defaultAccountStorage”);
var b=buildContainer.ResolveKeyed(“backupAccountStorage”);

这是否回答了您的问题?另外,您是否阅读了包含此内容的教程?不,它对我不起作用,因为我需要在Resolved上的Decise类中进行验证。我已按照本教程中的register type和ResolveType进行了学习,并始终获得BackupAccountDataStore(第二个)。。