Asp.net core 在ASPNET核心DI中注册实例
我正在将Unity中的一些代码转换为使用ASPNET Core的内置DI,我遇到了以下需要注册的场景(的一个实例)。我该怎么做呢Asp.net core 在ASPNET核心DI中注册实例,asp.net-core,Asp.net Core,我正在将Unity中的一些代码转换为使用ASPNET Core的内置DI,我遇到了以下需要注册的场景(的一个实例)。我该怎么做呢 var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile<AutoMapperBootstrapper.ServicesProfile>(); cfg.AddProfile<AutoMapperBootstrapper.W
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperBootstrapper.ServicesProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebApiProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebProfile>();
});
// register mapper config
var mapper = mapperConfig.CreateMapper();
但是我不知道如何在core中实现它。您可以使用IServiceCollection的工厂模式
services.AddSingleton(serviceProvider => {
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperBootstrapper.ServicesProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebApiProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebProfile>();
});
return mapperConfig.CreateMapper();
});
services.AddSingleton(serviceProvider=>{
var mapperConfig=新的MapperConfiguration(cfg=>
{
AddProfile();
AddProfile();
AddProfile();
});
返回mapperConfig.CreateMapper();
});
或者只是
services.AddSingleton(映射器)代码>注册表类型如何?您能解释一下吗?
services.AddSingleton(serviceProvider => {
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperBootstrapper.ServicesProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebApiProfile>();
cfg.AddProfile<AutoMapperBootstrapper.WebProfile>();
});
return mapperConfig.CreateMapper();
});