我正在嘗試注冊(cè)一個(gè)具有不同配置的接口,如下所示:private static ContainerBuilder RegisterAzureStorage(this ContainerBuilder containerBuilder, IAzureStorageOptions azureStorageOptions) { containerBuilder.Register(c => new AzureStorageKeyValuePersistenceService( new AzureStorageKeyValuePersistenceConfig( azureStorageOptions.DctAzureBlobConnectionString, azureStorageOptions.DctAzureBlobContainerName))) .As<IStreamableKeyValuePersistenceService>() .Keyed<IStreamableKeyValuePersistenceService>(PersistenceStorageKeys.DctAzureStorage) .SingleInstance(); containerBuilder.Register(c => new AzureStorageKeyValuePersistenceService( new AzureStorageKeyValuePersistenceConfig( azureStorageOptions.NcsAzureBlobConnectionString, azureStorageOptions.NcsAzureBlobContainerName))) .As<IStreamableKeyValuePersistenceService>() .Keyed<IStreamableKeyValuePersistenceService>(PersistenceStorageKeys.NcsAzureStorage) .SingleInstance(); return containerBuilder; }然后在我的構(gòu)造函數(shù)中:public ReportingController( [KeyFilter(PersistenceStorageKeys.DctAzureStorage)] IStreamableKeyValuePersistenceService dctStorage, [KeyFilter(PersistenceStorageKeys.NcsAzureStorage)] IStreamableKeyValuePersistenceService ncsStorage) { _dctStorage = dctStorage; _ncsStorage = ncsStorage; }和用法:await _dctStorage.SaveAsync(........);await _ncsStorage.SaveAsync(........);但是,在執(zhí)行時(shí),它只會(huì)拾取最后一次注冊(cè)并將兩個(gè)對(duì)象保存到同一位置。我可以使用可枚舉方法讓它工作:public ReportingController(IEnumerable<IStreamableKeyValuePersistenceService> storage) { _storage = storage; }然后對(duì)于可枚舉的每個(gè),但如果可能的話我更愿意使用鍵控方法。關(guān)于我所缺少的任何想法?
多次注冊(cè)相同的接口 - Autofac
慕田峪4524236
2022-12-31 10:53:13