title: ASP.NET API failing to publish on azure
body: <p>I'm trying to publish an C# API to azure, I followed all the steps in <a href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-azure-api-management-using-vs?view=aspnetcore-3.1" rel="noreferrer">this documentation</a> here but for some reason when I publish the application the build succeeds and crashes when this below line gets executed</p>
  <pre><code>Generating swagger file to 'C:\Users\user\Projs\API\bin\Release\netcoreapp3.1\swagger.json'.
  </code></pre>
  <p>and the only error log that gets printed is</p>
  <pre><code>Failed to generate swagger file. Error dotnet swagger tofile --serializeasv2 --output &quot;C:\Users\user\Downloads\Projs\API\bin\Release\netcoreapp3.1\swagger.json&quot; &quot;C:\Users\user\Downloads\Projs\API\bin\Release\netcoreapp3.1\API.dll&quot; v1
  
  Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
  File name: 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  
  
  
  
  Be sure that the Startup.cs for your application is calling AddSwaggerGen from within ConfigureServices in order to generate swagger file. Visit https://go.microsoft.com/fwlink/?LinkId=2131205&amp;CLCID=0x409 for more information.
  
  </code></pre>
  <p>This is my startup.cs</p>
  <pre><code> public void ConfigureServices(IServiceCollection services)
          {
  
              services.AddSwaggerGen(c =&gt;
              {
                  c.SwaggerDoc(
                      &quot;v1&quot;,
                      new Microsoft.OpenApi.Models.OpenApiInfo { Title = &quot;My API&quot;, Version = &quot;v1&quot; });
              });
  
          }
  
  
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              app.UseAuthentication();
              app.UseSwagger(c =&gt;
              {
                  c.SerializeAsV2 = true;
              });
  
              app.UseSwaggerUI(c =&gt;
              {
                  c.SwaggerEndpoint(&quot;v1/swagger.json&quot;, &quot;MyAPI V1&quot;);
              });
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
          }
  </code></pre>
  <p>This is my <code>.csproj</code> file</p>
  <pre><code>&lt;Project Sdk=&quot;Microsoft.NET.Sdk.Web&quot;&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp3.1&lt;/TargetFramework&gt;
        &lt;UseNETCoreGenerator&gt;true&lt;/UseNETCoreGenerator&gt;
        &lt;UserSecretsId&gt;d844b2a8-00b0-473d-8f97-9dca9f2899bd&lt;/UserSecretsId&gt;
    &lt;/PropertyGroup&gt;
  
      &lt;PropertyGroup Condition=&quot;'$(TargetFramework)' == 'net45'&quot;&gt;
          &lt;AutoGenerateBindingRedirects&gt;false&lt;/AutoGenerateBindingRedirects&gt;
      &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Folder Include=&quot;Model\&quot; /&gt;
      &lt;Folder Include=&quot;Context\&quot; /&gt;
      &lt;Folder Include=&quot;Contracts\ServiceResponse\&quot; /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNet.SignalR&quot; Version=&quot;2.4.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Authentication.JwtBearer&quot; Version=&quot;3.1.13&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNetCore.Mvc.NewtonsoftJson&quot; Version=&quot;3.1.13&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;3.1.11&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;3.1.11&quot;&gt;
        &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
        &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Relational&quot; Version=&quot;3.1.11&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;3.1.10&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer.Design&quot; Version=&quot;1.1.6&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Tools&quot; Version=&quot;3.1.11&quot;&gt;
        &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
        &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Newtonsoft.Json&quot; Version=&quot;13.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;AutoMapper&quot; Version=&quot;10.1.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Pomelo.EntityFrameworkCore.MySql&quot; Version=&quot;3.2.4&quot; /&gt;
      &lt;PackageReference Include=&quot;Swashbuckle.AspNetCore&quot; Version=&quot;5.0.0&quot; /&gt;
      &lt;PackageReference Include=&quot;System.IdentityModel.Tokens.Jwt&quot; Version=&quot;6.8.0&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Runtime&quot; Version=&quot;4.3.1&quot; /&gt;
      
    &lt;/ItemGroup&gt;
  &lt;/Project&gt;
  
  </code></pre>
  <p>I have tried solutions from these links but none of them worked</p>
  <p><a href="https://stackoverflow.com/questions/42755274/visual-studio-2017-could-not-load-file-or-assembly-system-runtime-version-4/42755305">Visual Studio 2017 - Could not load file or assembly &#39;System.Runtime, Version=4.1.0.0&#39; or one of its dependencies</a></p>
  <p><a href="https://github.com/Azure/azure-functions-vs-build-sdk/issues/160" rel="noreferrer">https://github.com/Azure/azure-functions-vs-build-sdk/issues/160</a></p>
  <p><a href="https://stackoverflow.com/questions/60450266/could-not-load-file-or-assembly-system-runtime-version-4-2-1-0-culture-neutra">Could not load file or assembly &#39;System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&#39;</a></p>
  <p>Is there a way to fix this?</p>
  <p>Update:
  I tried a solution that was told by a user in the thread to add binding redirects, I added a <code>web.config</code> and a <code>app.config</code> and added the bindinds but it still fails to publish</p>
  <p>Update 2:
  I couldn't find a solution so I installed .net 5.0 and it worked</p>
  <p><a href="https://i.stack.imgur.com/abvCf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/abvCf.png" alt="enter image description here" /></a></p>