简体   繁体   中英

How do I get rid of the SecurityException error when trying to use Automapper in ASP.NET MVC?

I'm developing an ASP.NET MVC application with NHibernate and I'm trying to use Automapper to hide the Domain objects from the DTO objects sent to the view:

  1. ClassLibrary with my Domain (for NHibernate) and DTO objects
  2. Class library to make a SessionFactory and Factories in my Project

I downloaded AutoMapper to transform Domain objects in DTO and added the code to do this in Application_Start .

When I run the app in VisualStudio (by pressing F5) it works fine and my DTOs are mapped into the view.

When I publish this in IIS, I get a security exception:

Mapper.CreateMap<Category, CategoryDto>(); 
Mapper.CreateMap<Product, ProductDto>();

System.Security.SecurityException: Failed request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

How do I get Automapper to work without the security exception?

Right click on the AutoMapper.dll > select properties > Unblock.

Then restart IIS

you need to run your IIS in full trust in order to have reflection permission, look here reflection permission

or here set trust level

you need this because Automapper uses reflection to emit code o_O

Unblock the dll (as mentioned by Lee, Right click on the AutoMapper.dll > select properties > Unblock.), and then just recycle the site's app pool. Need not restart IIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM