我可以将 AutoMapper 与 Blazor 一起使用吗?

Sán*_*ani 2 automapper blazor

我可以将 AutoMapper 8.0.1 与 Blazor 服务器应用程序一起使用吗?我已经尝试过,但我的代码总是遇到错误:

缺少类型映射配置或映射不受支持。映射类型:对象 -> 对象 System.Object -> System.Object

我已将映射器添加到启动文件中:

services.AddAutoMapper(typeof(Startup));
Run Code Online (Sandbox Code Playgroud)

我已经创建了个人资料:

public class MyProfile : Profile
{
    public MyProfile()
    {
        CreateMap<District, DistrictModel>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用它:

[Inject]
protected IMapper Mapper { get; set; }
District district = DistrictService.FindDistrictById(districtId);
DistrictModel model = Mapper.Map<DistrictModel>(district);
Run Code Online (Sandbox Code Playgroud)

AssertConfigurationIsValid 方法给出:

Cannot find any profiles with the name 'MyProfile'. (Parameter 'profileName')
Run Code Online (Sandbox Code Playgroud)

Bri*_*ker 5

Startup.cs

var mapperConfiguration = new MapperConfiguration(configuration =>
{
    configuration.AddProfile(new MyProfile());
});

var mapper = mapperConfiguration.CreateMapper();

services.AddSingleton(mapper);
Run Code Online (Sandbox Code Playgroud)