.NET Core IServiceCollection 替换不刷新 [英] .NET Core IServiceCollection Replace not refreshing

查看:19
本文介绍了.NET Core IServiceCollection 替换不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Address 对象,它依赖于在构造函数中注入的 IState 对象:

I have an Address object that depends on an IState object inject in the constructor:

public class AddressService : BaseService, IAddressService
{
        private readonly IStateRepository _stateRepository;
        private readonly ICacheClass _cache;
        private readonly ILogger<AddressService> _logger;

        public const string StatesCacheKey = "StateListManagedByStateService";

        public AddressService(IStateRepository stateRepository, 
                              ICacheClass cache, 
                              ILogger<AddressService> logger, 
                              IUserContext userContext) : base(userContext)
        {
            _stateRepository = stateRepository;
            _cache = cache;
            _logger = logger;
        }

        public async Task<IReadOnlyList<T>> GetAllStatesAsync<T>() where T : IState
        {
            var list = await _cache.GetAsync<IReadOnlyList<T>>(StatesCacheKey);

            if (list != null) return list;

            var repoList = _stateRepository.GetAll().Cast<T>().ToList();

            _logger.LogInformation("GetAllStates retrieved from repository, not in cache.");

            await _cache.SetAsync(StatesCacheKey, repoList);

            return repoList;
        }
}

IStateRepository 由 ASP.NET Core Web 项目注入,在 Startup 中使用以下行:

IStateRepository is injected by the ASP.NET Core web project with the folling lines in Startup:

    services.AddTransient<IStateRepository, Local.StateRepository>();
    services.AddTransient<IAddressService, AddressService>();

在我的控制器中,我有两种操作方法.根据调用的那个,我想更改与 DI 关联的 IStateRepository 对象:

In my Controller I have two action methods. Depending on the one called, I want to change the IStateRepository object associated with DI:

    private readonly IAddressService _addressService;


    public HomeController(IConfiguration configuration, ILogger<HomeController> logger, IAddressService addressService) : base(configuration,logger)
            {
                _addressService = addressService;
            }

    public async Task<IActionResult> DistributedCacheAsync()
    {
                    var services = new ServiceCollection();
                    services.Replace(ServiceDescriptor.Transient<IStateRepository, Repositories.Local.StateRepository>());

                    ViewBag.StateList = await _addressService.GetAllStatesAsync<State>();

                    return View();
    }

    public async Task<IActionResult> DapperAsync()
    {
                    var services = new ServiceCollection();
                    services.Replace(ServiceDescriptor.Transient<IStateRepository, Repositories.Dapper.StateRepository>());

                    ViewBag.StateList = await _addressService.GetAllStatesAsync<State>();

                    return View();
}

问题是 _addressService 已经有从 Startup 关联的 Local.StateRepository,并且在 DapperAsync 中使用 Replace 更新它不会对其产生影响.

The problem is that _addressService already has the Local.StateRepository associated from Startup, and updating it in DapperAsync using Replace doesn't have an effect on it.

在上面的示例中,如何在运行时更改 DI 中的 IStateRepository?

How would be able to change IStateRepository in DI during runtime in the example above?

推荐答案

Microsoft.Extensions.DependencyInjection 不支持在运行时更改服务.

Changing services at runtime is not supported by Microsoft.Extensions.DependencyInjection.

我建议创建一个包装器服务,它可以在运行时根据条件在不同的实现之间进行选择.

I recommend creating a wrapper service that at runtime can choose between different implementations based on a condition.

这篇关于.NET Core IServiceCollection 替换不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆