title: How to configure AutoMapper 9.0 in IIS hosted WCF application
body: <p>I want to use AutoMapper 9.0 in a WCF project containing several services that will be hosted in IIS. I've only found one other related SO question but its dealing with a 10 year old version of AutoMapper and is not asking the same question. Its answer is similar to the top hits on Google which suggest using a <code>ServiceBehavior</code> but that doesn't seem applicable when I want multiple services to use the same mapper. The defense rests. </p>
  
  <p>In a web project, you might create a static <code>MapperConfiguration</code> in the <code>Global.asax</code> when the application starts, but WCF doesn't have a <code>Global.asax</code>. It looks like there are a few options for executing initialization code in WCF:</p>
  
  <ol>
  <li>Include an <code>AppInitialize()</code> method in the App_Code folder. This will be dynamically compiled at runtime and people have complained that it can have missing reference issues in IIS so I'm not confident <code>AutoMapper</code> or its dependencies will be found once deployed to IIS.</li>
  <li>Create a custom <code>ServiceHost</code>. This seems like it would execute once when the application starts, but also looks like it ignores the web.config configuration, which I don't want.</li>
  <li>Use the <a href="https://docs.microsoft.com/en-us/dotnet/framework/wcf/configuring-wcf-services-in-code" rel="nofollow noreferrer"><code>Configure</code> method</a> per service. This has the same drawback as #2 and also I become concerned with thread safety (as in the <code>ServiceBehavior</code> approach) since two services could try to initialize the <code>MapperConfiguration</code> at once.</li>
  </ol>
  
  <p>I considered just creating a class with a static property that would create a static <code>MapperConfiguration</code> or <code>IMapper</code> instance if it was not already created, but as in #3, I'm worried this may not be thread safe. Maybe if I did something like this?</p>
  
  <pre><code>public static class MapperConfig
  {
      private static IMapper _modelMapper;
      private static readonly object _mapperLocker = new object();
  
      public static IMapper ModelMapper
      {
          get
          {
              lock(_mapperLocker)
              {
                  if (_modelMapper == null)
                  {
                      var config = new MapperConfiguration(cfg =&gt; cfg.AddProfile(new MappingProfile1()));
                      _modelMapper = config.CreateMapper();
                  }
              }
  
              return _modelMapper;
          }
      }
  }
  </code></pre>
  
  <p>Where two services may call <code>ModelMapper</code> simultaneously. Another downside of this is the first request to any service will have to wait for the mapping to compile, but I'm not sure I can get away from that. I definitely don't want it compiling the mappings per call and would prefer not to even have to do it per service. Can you advise on the thread safety of <code>MapperConfiguration</code> and the best way to use it in IIS-hosted WCF?</p>