我在ASP.NET MVC 5应用程序中使用AutoMapper 6.2.0 .
当我通过控制器调用我的视图时,它显示所有正确的事情 . 但是,当我刷新该视图时,Visual Studio显示错误:
System.InvalidOperationException:'Mapper已初始化 . 您必须为每个应用程序域/进程调用一次Initialize .
我只在一个控制器中使用AutoMapper . 尚未在任何地方进行任何配置,也未在任何其他服务或控制器中使用AutoMapper .
我的控制器:
public class StudentsController : Controller
{
private DataContext db = new DataContext();
// GET: Students
public ActionResult Index([Form] QueryOptions queryOptions)
{
var students = db.Students.Include(s => s.Father);
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Student, StudentViewModel>();
});
return View(new ResulList<StudentViewModel> {
QueryOptions = queryOptions,
Model = AutoMapper.Mapper.Map<List<Student>,List<StudentViewModel>>(students.ToList())
});
}
// Other Methods are deleted for ease...
控制器内出错:
我的Model类:
public class Student
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string CNIC { get; set; }
public string FormNo { get; set; }
public string PreviousEducaton { get; set; }
public string DOB { get; set; }
public int AdmissionYear { get; set; }
public virtual Father Father { get; set; }
public virtual Sarparast Sarparast { get; set; }
public virtual Zamin Zamin { get; set; }
public virtual ICollection<MulaqatiMehram> MulaqatiMehram { get; set; }
public virtual ICollection<Result> Results { get; set; }
}
我的ViewModel类:
public class StudentViewModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string CNIC { get; set; }
public string FormNo { get; set; }
public string PreviousEducaton { get; set; }
public string DOB { get; set; }
public int AdmissionYear { get; set; }
public virtual FatherViewModel Father { get; set; }
public virtual SarparastViewModel Sarparast { get; set; }
public virtual ZaminViewModel Zamin { get; set; }
}
10 回答
当您刷新视图时,您正在创建
StudentsController
的新实例 - 因此重新初始化Mapper - 导致出现错误消息"Mapper already initialized" .来自Getting Started Guide
设置此方法的一种方法是将所有映射配置放入静态方法中 .
App_Start/AutoMapperConfig.cs :
然后在 Global.asax.cs 中调用此方法
现在,您可以(重新)在控制器操作中使用它 .
如果您希望/需要在单元测试场景中坚持使用静态实现,请注意您可以在调用initialize之前调用
AutoMapper.Mapper.Reset()
. 请注意,这不应该在文档中提到的 生产环境 代码中使用 .资料来源:http://docs.automapper.org/en/stable/Configuration.html#resetting-static-mapping-configuration
我之前使用过这种方法,直到版本6.1.1
从版本6.2开始,这不再起作用了 . 要正确使用Automapper,请创建一个新的Mapper,我们就像这样:
万一你真的需要"re-initialize"
AutoMapper
你应该switch to the instance based API以避免System.InvalidOperationException
:Mapper already initialized. You must call Initialize once per application domain/process.
例如,当您为
xUnit
测试创建TestServer
时,您可以将fixure
类构造函数中的ServiceCollectionExtensions.UseStaticRegistration
设置为false
来制作技巧:您可以使用automapper作为 Static API 和 Instance API ,已经初始化的Mapper是静态API中的常见问题,您可以使用mapper.Reset()初始化mapper,但这根本不是答案 .
只需尝试使用实例API
对于单元测试,您可以将Mapper.Reset()添加到单元测试类中
Automapper 8.0.0 version
你可以简单地使用
Mapper.Reset()
.例:
如果你在UnitTest中使用Mapper而你的测试多于一个,你可以使用
Mapper.Reset()
如果您正在使用MsTest,则可以使用AssemblyInitialize属性,以便仅为该程序集(此处为测试程序集)配置映射一次 . 这通常被添加到控制器单元测试的基类中 .
我希望这个答案有所帮助