ASP.NET API failing to publish on azure


I'm trying to publish an C# API to azure, I followed all the steps in this documentation here but for some reason when I publish the application the build succeeds and crashes when this below line gets executed

Generating swagger file to 'C:\Users\user\Projs\API\bin\Release\netcoreapp3.1\swagger.json'.

and the only error log that gets printed is

Failed to generate swagger file. Error dotnet swagger tofile --serializeasv2 --output "C:\Users\user\Downloads\Projs\API\bin\Release\netcoreapp3.1\swagger.json" "C:\Users\user\Downloads\Projs\API\bin\Release\netcoreapp3.1\API.dll" v1

Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'System.Runtime, Version=, 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 for more information.

This is my startup.cs

 public void ConfigureServices(IServiceCollection services)

            services.AddSwaggerGen(c =>
                    new Microsoft.OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" });


  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            if (env.IsDevelopment())



            app.UseSwagger(c =>
                c.SerializeAsV2 = true;

            app.UseSwaggerUI(c =>
                c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");


            app.UseEndpoints(endpoints =>

This is my .csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">


    <PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
    <Folder Include="Model\" />
    <Folder Include="Context\" />
    <Folder Include="Contracts\ServiceResponse\" />
    <PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.13" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.11">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
    <PackageReference Include="System.Runtime" Version="4.3.1" />

I have tried solutions from these links but none of them worked

Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=' or one of its dependencies

Could not load file or assembly 'System.Runtime, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Is there a way to fix this?

Update: I tried a solution that was told by a user in the thread to add binding redirects, I added a web.config and a app.config and added the bindinds but it still fails to publish

Update 2: I couldn't find a solution so I installed .net 5.0 and it worked

enter image description here

asked on Stack Overflow Mar 30, 2021 by Rasheen Ruwisha • edited Mar 31, 2021 by Rasheen Ruwisha

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0