【问题标题】:System.AggregateException: 'Some services are not able to be constructed error in .net6System.AggregateException: \'某些服务无法在.net6 中构造错误
【发布时间】:2022-08-20 14:59:30
【问题描述】:
//this is UserService file   

 using System.Security.Claims;
    
    namespace _HALKA.Services
    {
        public class UserService : IUserService
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
        
    
            public UserService(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
    
            public string GetMyName()
            {
                var result = string.Empty;
                if (_httpContextAccessor.HttpContext != null)
                {
                    result = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.Name);
                }
                return result;
            }
        }
        }

//this is IUserService file
    namespace _HALKA.Services
    {
        public interface IUserService
        {
            string GetMyName();
         
        }
    }

//And this is program.cs file

builder.Services.AddScoped<IUserService, UserService>();

我从 \"var app = builder.Build();\" 收到此错误:

System.AggregateException:\'无法构造某些服务(验证服务描述符时出错\'ServiceType:_HALKA.Services.IUserService Lifetime:Scoped ImplementationType:_HALKA.Services.UserService\':无法解析服务类型\ 'Microsoft.AspNetCore.Http.IHttpContextAccessor\' 尝试激活 \'_HALKA.Services.UserService\'。)\'

  • 什么以及如何解决IUserService

标签: asp.net asp.net-web-api


【解决方案1】:

Programm.cs 文件中添加以下代码

builder.Services.AddHttpContextAccessor();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 2020-11-19
    • 2021-03-14
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2020-06-04
    • 2021-07-07
    相关资源
    最近更新 更多