Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoMapper configuration resets when using ASP.MVC 3.0
    text
    copied!<p>In this scenario I'm using <code>AutoMapper</code> library in ASP.MVC 3.0 to handle mapping between dto and model objects. I tried to place all AutoMapper configuration code in one place, so it has following structure:</p> <pre><code>protected void Application_Start() { ConfigureAutoMapper(); AreaRegistration.RegisterAllAreas(); SetupWindsorContainer(); RegisterRoutes(RouteTable.Routes); } ... private void ConfigureAutoMapper() { Mapper.CreateMap&lt;SampleDto, SampleModel&gt;(); ... } </code></pre> <p>This code is placed in <code>MvcApplication</code> class. With debugger I checked that this code is executed but when I try to perform <code>Mapper.Map&lt;&gt;</code> in Controller code i get exeception saying that mapping hasn't been registered. I had suspected that this is related to how ASP.MVC handles <code>AppDomains</code> but it seems to be the same <code>AppDomain</code> in both methods (registration code and mapping code). ASAIR this code executed properly on ASP.MVC 2.0</p> <p>EDIT 1: According to MS documentation on <code>Application_Start</code>:</p> <blockquote> <p>"Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.</p> <p>You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created."</p> </blockquote> <p>AutoMapper configuration is static class so I think it should work properly when initialized in <code>Application_Start</code> but it doesn't.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload