title: How comes my C++ assembly is seen from a C# console application but not an ASP.NET one?
body: <p>I wrote a C++ dll. It has one function, which returns the double 1.0.</p>
  
  <p>I attempted to use it in a C# console application:</p>
  
  <pre><code>namespace MyNameSpace
  {
  
      class Program
          {
          [DllImport(@"C:\path\to\my\dll\myDll.dll")]
          static extern double SendOne();
  
          static void Main(string[] args)
          {
              double i = SendOne();
              Console.WriteLine(i);
          }
      }
  }
  </code></pre>
  
  <p>This works.</p>
  
  <p>I then attempted to use it in an ASP.NET application, created with an empty MVC application, and then adding one controller and one view :</p>
  
  <p>Controllers/HomeController.cs:</p>
  
  <pre><code>{
      public class HomeController : Controller
      {
          [DllImport(@"C:\path\to\my\dll\myDll.dll")]
          static extern double SendOne();
  
          // GET: Home
          public ActionResult Index()
          {
              double i = SendOne();
              ViewBag["result"] = i;
              return View();
          }
      }
  }
  </code></pre>
  
  <p>Views/Home/Index.cshtml:</p>
  
  <pre><code>@{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt; 
          This is some text and the result : @{ ViewBag["result"]}
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This does not work, and returns a BadImageFormatException.</p>
  
  <p>In all three projects, I manually configured the Platform to be <strong>Debug / x64</strong> (from the default Debug / AnyCPU).</p>
  
  <p>This is the bottom of the stack trace : (First line manually translated from french)</p>
  
  <pre><code>[BadImageFormatException: Impossible charge the file or assembly '[Name of the ASP.NET Project]' or one of its dependencies. Attempt to load a program of incorrect format.]
  System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
  System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36
  System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
  System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
  System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark&amp; stackMark, Boolean forIntrospection) +21
  System.Reflection.Assembly.Load(String assemblyString) +28    System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38
  </code></pre>
  
  <p>Top of the stack trace : HttpException 0x80004005.</p>
  
  <p>Both the ASP.NET Project &amp; the C# Console App are two projects in the same Visual Studio 2017 Solution, that do not reference each other and that I launch separately.</p>
  
  <p>I don't get the differences that exist between an ASP.NET project and a C# Console App that could cause the Native C++ Dll loading to work in the C# Console App and fail in the ASP.NET project.</p>
  
  <p>Would anyone know the issue or have advices on what I can try ? Thanks.</p>