title: Best practices for using NSwag 'AspNetCoreToSwaggerGenerator'
body: <p>I've been using <a href="https://github.com/RSuter/NSwag" rel="nofollow noreferrer">NSwag</a> for a while to generate an Angular TS client to my WebAPI and I love it.</p>
  <p>I'm finally moving away from NSwagStudio to using MSBuild targets to automatically generate my json specification.</p>
  <p>I see that there is a new way to generate specs called <code>AspNetCoreToSwaggerGenerator</code>, which uses the <a href="https://andrewlock.net/introduction-to-the-apiexplorer-in-asp-net-core/" rel="nofollow noreferrer">ASP.NET Core API Explorer</a> service.</p>
  <p>The <a href="https://github.com/RSuter/NSwag/wiki/AspNetCoreToSwaggerGenerator" rel="nofollow noreferrer">docs</a> specify:</p>
  <blockquote>
  <p>This generator uses the <a href="https://andrewlock.net/introduction-to-the-apiexplorer-in-asp-net-core/" rel="nofollow noreferrer">ASP.NET Core API Explorer</a> service to generate specs and will eventually supersede the reflection based generator <a href="https://github.com/RSuter/NSwag/wiki/WebApiToSwaggerGenerator" rel="nofollow noreferrer">WebApiToSwaggerGenerator</a>. It is recommended to use this generator for new projects and also start migrating existing projects.</p>
  </blockquote>
  <p>So I thought great I'll use that. However I suddenly got some errors about missing paths, that turned out to be from my startup.cs file. I thought that was weird until i realized that it was actually spinning up an instance of my Startup class and running the code - makes sense if it's using the API Explorer.</p>
  <p>However this strikes me as not only potentially very dangerous (if I did some weird coding in the Startup), but also potentially slower if it's running through a lot of startup code.</p>
  <p>I switched to using the older <code>WebApiToSwaggerGenerator</code> like this :</p>
  <pre><code>  &lt;Target Name=&quot;NSwag&quot; AfterTargets=&quot;Build&quot;&gt;
      &lt;Copy SourceFiles=&quot;@(Reference)&quot; DestinationFolder=&quot;$(OutDir)References&quot; /&gt;
      &lt;Exec Command=&quot;$(NSwagExe_Core21) webapi2swagger /assembly:$(OutDir)RR.API.DLL /output:rrapi.json&quot; /&gt;
      &lt;RemoveDir Directories=&quot;$(OutDir)References&quot; /&gt;
    &lt;/Target&gt;
  </code></pre>
  <p>This generates me a <code>rrapi.json</code> file that I can then run in my Angular build to actually create the client. It seems to be quite fast and works well.</p>
  <p>So I'm left wonder what are the best practices, and benefits of the newer <code>AspNetCoreToSwaggerGenerator</code>. Specifically :</p>
  <ul>
  <li>How to prevent unnecessary code from running from my Startup.cs file</li>
  <li>What are the actual benefits? Is this to enable more advanced metadata via the API Explorer service - or some future proofing for Swagger 3?</li>
  <li>What are the benchmarks of using either for a typical project.</li>
  </ul>
  <p>I basically want a static .json file generated everytime I build my WebAPI client. I don't right now see any reason not to use the older generator.</p>
  <hr />
  <p>Update: this is what I ended up using (aspnetcore2swagger). I don't remember the exact details of everything going on here, but this has been working well. It creates an intermediate rr.api.json file (I have all my API stuff in RR.API project).</p>
  <pre><code> &lt;!-- https://github.com/RSuter/NSwag/issues/1159#issuecomment-363886104 --&gt;
   &lt;Target Name=&quot;NSwag&quot; AfterTargets=&quot;Build&quot;&gt;
  
     &lt;Copy SourceFiles=&quot;@(ReferencePath)&quot; DestinationFolder=&quot;$(OutDir)References&quot; /&gt;
     &lt;Exec Command=&quot;$(NSwagExe_Core31) aspnetcore2swagger  /documentName:a /assembly:$(ProjectDir)bin\$(Configuration)\netcoreapp3.1\RR.API.dll /output:$(SpaRoot_APIRoot)rr.api.json&quot; /&gt;
  
     &lt;!-- SAVE https://localhost:5001/swagger/a/swagger.json --&gt;
     &lt;!-- TO http://localhost:54712/src/app/shared/api/rr.api.json --&gt;
     &lt;Exec Command=&quot;$(NSwagExe_Core31) run $(SpaRoot_APIRoot)rr.api.nswag /variables:SpaRoot_APIRoot=$(SpaRoot_APIRoot)&quot; /&gt;
     &lt;RemoveDir Directories=&quot;$(OutDir)References&quot; /&gt;
  
  &lt;/Target&gt;
  </code></pre>
  <p>I have <code>SpaRoot_APIRoot</code> defined as:</p>
  <pre><code>&lt;SpaRoot_APIRoot&gt;..\MyAngularProject\src\app\shared\api\&lt;/SpaRoot_APIRoot&gt;
  </code></pre>