映射时无法解析类型的服务

问题描述 投票:0回答:1

我在将数据从模型映射到modelDto时遇到了麻烦(在下面的示例中,由于工作场所中的内部代码惯例,其被标记为状态)

但是,当我尝试运行程序并调用端点时,出现以下错误:

无法解析类型'project1.API.Core.Mappers.Interfaces.IMapToNew`2 [Project1.API.Features.Personalisation.Personalisation,Project1.API.Features.Personalisation.Responses.PersonalisationState的服务]',而尝试激活'Project1.API.Features.Personalisation.Services.PersonalisationBogusService'。

考虑一下我如何映射数据的问题,但我无法发现问题。我将发布所有相关的类以进行概述。

public static class ServiceCollectionExtensions
{
public static void GetPersonalisationFeature(this IServiceCollection serviceCollection, IConfiguration config)
{
    config.ThrowIfNull(nameof(config));

    serviceCollection.AddScoped<IPersonalisationService, PersonalisationBogusService>();
}

}

class PersonalisationToPersonalisationState : IMapToNew<Personalisation, PersonalisationState>
{
    public PersonalisationState Map(Personalisation model)
    {
        return new PersonalisationState
        {
            UserType = EnumExtensions.StringToEnum(model.UserType, UserType.Student),
            UserRole = EnumExtensions.StringToEnum(model.UserRole, UserRole.PrimaryGuest),
            BookingPeriod = EnumExtensions.StringToEnum(model.BookingPeriod, BookingPeriod.PreCheckin)
        };
    }
}

public class Personalisation
{
    public string UserType { get; set; }

    public string UserRole { get; set; }

    public string BookingPeriod { get; set; }
}


 public class PersonalisationState
{
    [Description("Get user type based on userID")]
    public UserType UserType { get; set; }
    [Description("Get user role based on userID")]
    public UserRole UserRole { get; set; }
    [Description("Get the booking period of a user based on UserID")]
    public BookingPeriod BookingPeriod { get; set; }
}

public interface IPersonalisationService
{
    Task<IEnumerable<PersonalisationState>> GetPersonalisation(string Id);
}

public class PersonalisationBogusService : IPersonalisationService
{
    private readonly IMapToNew<Personalisation, PersonalisationState> _mapper;


    public PersonalisationBogusService(IMapToNew<Personalisation, PersonalisationState> mapper)
    {
        _mapper = mapper;
    }

    public async Task<IEnumerable<PersonalisationState>> GetPersonalisation(string Id)
    {
        var personalise = GetMockData();
        personalise.Add(GetFakePersonalisedData());
        return personalise
            .Select(b => _mapper.Map(b))

            .Select(b =>
            {
                b.UserType = UserType.Student;
                return b;
            })
            .Select(b =>
            {
                b.UserRole = UserRole.PrimaryGuest;
                return b;
            })
            .Select(b =>
            {
                b.BookingPeriod = BookingPeriod.PreCheckin;
                return b;
            });


    }


    private Personalisation GetFakePersonalisedData()
    {
        return new Personalisation
        {
            UserType = "Student",
            UserRole = "PrimaryGuest",
            BookingPeriod = "01/01/2019"
        };
    }

    public static List<Personalisation> GetMockData()
    {
        return new List<Personalisation> {new Personalisation {UserType = "", UserRole = "", BookingPeriod = ""}};

    }

}

public class PersonalisationController : BaseController
{
    private readonly IPersonalisationService _personalisationService;

    public PersonalisationController(IPersonalisationService personalisationService)
    {
        _personalisationService = personalisationService;
    }


    public async Task<ActionResult> Get([Description("Determines groups a user belongs to via their ID")] string id)
    {
        var result = await _personalisationService.GetPersonalisation(id);
        return Ok(result);
    }

}

并且是的,我还在startup.cs文件中添加了相关代码:

services.GetPersonalisationFeature(_configuration);

c# asp.net-core dependency-injection dto
1个回答
0
投票

目标服务需要IMapToNew<Personalisation, PersonalisationState>,但基于异常,该类型尚未在服务集合中注册。

确保将所有依赖类型都添加到服务集合中。

© www.soinside.com 2019 - 2024. All rights reserved.