.net core、.net8中间件中不能注入Service服务,报错:Cannot resolve scoped service 'XXX' from root provider

中间件中如果想用Service服务,不能用注入,需要这样写:

using (var serviceCope = httpContext.RequestServices.CreateScope())
{
    ISysSettingService? _sysSettingService = serviceCope.ServiceProvider.GetService<ISysSettingService>();
    if (_sysSettingService != null){...}
}

 来源:https://www.cnblogs.com/shipengfei/p/16092063.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章