Dependency injection 如何向Microsoft依赖项注入注册Automapper 5.0?

Dependency injection 如何向Microsoft依赖项注入注册Automapper 5.0?,dependency-injection,automapper,Dependency Injection,Automapper,我正在尝试使用Microsoft的内置依赖项注入注册新的Automapper 5.0: public static class ServicesContainerConfigure { public static void Configure(IServiceCollection services) { //This line fails because Mapper has no default constructor services.TryA

我正在尝试使用Microsoft的内置依赖项注入注册新的Automapper 5.0:

public static class ServicesContainerConfigure
{
    public static void Configure(IServiceCollection services)
    {
        //This line fails because Mapper has no default constructor
        services.TryAddScoped<IMapper, Mapper>();

        var profileType = typeof(Profile);
        // Get an instance of each Profile in the executing assembly.
        var profiles = Assembly.GetExecutingAssembly().GetTypes()
            .Where(t => profileType.IsAssignableFrom(t)
                        && t.GetConstructor(Type.EmptyTypes) != null)
            .Select(Activator.CreateInstance)
            .Cast<Profile>();

        // Initialize AutoMapper with each instance of the profiles found.
        var config = new MapperConfiguration(cfg =>
        {
            foreach (var profile in profiles)
            {
                cfg.AddProfile(profile);
            }
        });
        config.CreateMapper();
    }
}
公共静态类服务容器配置
{
公共静态无效配置(IServiceCollection服务)
{
//此行失败,因为映射程序没有默认构造函数
services.TryAddScoped();
var profileType=类型(配置文件);
//获取执行程序集中每个概要文件的实例。
var profiles=Assembly.getExecutionGassembly().GetTypes()
.Where(t=>profileType.IsAssignableFrom(t)
&&t.GetConstructor(Type.EmptyTypes)!=null)
.Select(Activator.CreateInstance)
.Cast();
//使用找到的配置文件的每个实例初始化AutoMapper。
var config=new-MapperConfiguration(cfg=>
{
foreach(配置文件中的var配置文件)
{
cfg.AddProfile(profile);
}
});
config.CreateMapper();
}
}

映射器对象中没有默认构造函数。必须有一种方法可以在不在映射器dll中注册所有注入对象的情况下注册此对象。

方法通常提供一个重载,您可以在其中通过“实现工厂”。但是,似乎
TryAddXXX
没有。如果没有令人信服的理由使用
TryAddXXX
,那么这应该适用于您:

services.AddScoped<IMapper>(_ =>
{
    var profileType = typeof(Profile);
    // Get an instance of each Profile in the executing assembly.
    var profiles = Assembly.GetExecutingAssembly().GetTypes()
                           .Where(t => profileType.IsAssignableFrom(t) && t.GetConstructor(Type.EmptyTypes) != null)
                           .Select(Activator.CreateInstance)
                           .Cast<Profile>();

    // Initialize AutoMapper with each instance of the profiles found.
    var config = new MapperConfiguration(cfg =>
    {
        foreach (var profile in profiles)
        {
            cfg.AddProfile(profile);
        }
    });

    return config.CreateMapper();
});
services.AddScoped(\u=>
{
var profileType=类型(配置文件);
//获取执行程序集中每个概要文件的实例。
var profiles=Assembly.getExecutionGassembly().GetTypes()
.Where(t=>profileType.IsAssignableFrom(t)&&t.GetConstructor(Type.EmptyTypes)!=null)
.Select(Activator.CreateInstance)
.Cast();
//使用找到的配置文件的每个实例初始化AutoMapper。
var config=new-MapperConfiguration(cfg=>
{
foreach(配置文件中的var配置文件)
{
cfg.AddProfile(profile);
}
});
返回config.CreateMapper();
});