ASP.NET API发布到Azure失败

6
我想将一个C# API发布到Azure,我按照这篇文档的所有步骤操作,但出现问题,当我发布应用程序时,编译成功,但执行以下代码时崩溃。
Generating swagger file to 'C:\Users\user\Projs\API\bin\Release\netcoreapp3.1\swagger.json'.

而唯一打印的错误日志是

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=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&CLCID=0x409 for more information.

这是我的startup.cs文件

 public void ConfigureServices(IServiceCollection services)
        {

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

        }


  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseSwagger(c =>
            {
                c.SerializeAsV2 = true;
            });

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

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

这是我的.csproj文件

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
      <UseNETCoreGenerator>true</UseNETCoreGenerator>
      <UserSecretsId>d844b2a8-00b0-473d-8f97-9dca9f2899bd</UserSecretsId>
  </PropertyGroup>

    <PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
        <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
    </PropertyGroup>
  <ItemGroup>
    <Folder Include="Model\" />
    <Folder Include="Context\" />
    <Folder Include="Contracts\ServiceResponse\" />
  </ItemGroup>
  <ItemGroup>
    <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">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <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">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <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" />
    
  </ItemGroup>
</Project>


我尝试过这些链接中的解决方案,但它们都没有起作用。 Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies

https://github.com/Azure/azure-functions-vs-build-sdk/issues/160

无法加载文件或程序集'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

有办法解决吗?

更新: 我尝试了一个用户在帖子中告诉我的解决方案,添加绑定重定向,我添加了一个web.config和一个app.config并添加了绑定,但仍然无法发布

更新2: 我找不到解决方案,所以我安装了.NET 5.0,它可以工作。

enter image description here


你能看到你的 Swagger 端点吗?它似乎与你的 SwashBuckle.AspnetCore 版本有关,但无法确认,也许降级 NuGet 的版本可以帮助解决问题。 - Juanma Feliu
@Juanma Feliu 当我访问https://localhost:44342/swagger/v1/swagger.json时,它可以完美地显示JSON。 - Rasheen Ruwisha
你的Newtonsoft.Json版本也可能会出现问题,因为Pomelo使用的版本比13.0.1低。你能否编辑你的.csproj文件并删除System.Runtime包的引用? - Juanma Feliu
1
@Juanma Feilu 我已经移除了 System.Runtime 并且也删除了 Newtonsoft.Json,因为我没有使用它。但是我仍然遇到相同的错误。 - Rasheen Ruwisha
由于您的项目针对 netcoreapp3.1,建议使用所有版本为 3.. 的框架 nuget 包,而不是 5.*.*。 - Prasath
显示剩余3条评论
1个回答

3
您可以使用最新版本的Swashbuckle.AspNetCore工具。我在Swashbuckle的GitHub页面上找到了官方解决方法:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2006 执行以下步骤:
  1. 在Web API项目中添加global.json文件。
  2. 将以下内容添加到文件中:
  3. { "sdk": { "version": "3.1.406", "rollForward": "latestPatch" } }


1
请注意将SDK版本更改为您的项目正在使用的版本。 - Donald N. Mafa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接