【问题标题】:ASP.NET API failing to publish on azureASP.NET API 无法在 azure 上发布
【发布时间】:2021-06-26 17:42:31
【问题描述】:

我正在尝试将 C# API 发布到 azure,我在此处执行了 this documentation 中的所有步骤,但由于某种原因,当我发布应用程序时,构建成功并在执行以下行时崩溃

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

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

有没有办法解决这个问题?

更新: 我尝试了一个解决方案,该解决方案由线程中的用户告知添加绑定重定向,我添加了 web.configapp.config 并添加了绑定但它仍然无法发布

更新 2: 我找不到解决方案,所以我安装了 .net 5.0 并且它工作了

【问题讨论】:

  • 你能看到你的招摇端点吗?它似乎与您的 SwashBuckle.AspnetCore 版本有关,但无法确认,也许降级 nuget 版本会有所帮助。
  • @Juanma Feliu 当我转到https://localhost:44342/swagger/v1/swagger.json 时,它完美地显示了 JSON
  • 您的 Newtonsoft.Json 版本也可能会被弄乱,因为 Pomelo 使用的版本低于 13.0.1。你能编辑你的 .csproj 并删除 System.Runtime 包引用吗?
  • @Juanma Feilu 我删除了 System.Runtime 并删除了 Newtonsoft.Json,因为我没有使用它。但我仍然得到同样的错误
  • 请从你的项目中添加一个 nuget 包截图。

标签: c# asp.net .net azure swagger


【解决方案1】:

您可以使用最新版本的 Swashbuckle.AspNetCore。我从 GitHub 上的 Swashbuckle 找到了官方的解决方法: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2006

  1. 将 global.json 文件添加到您的 Web API 项目

  2. 把这个放进去:

    { “SDK”:{ “版本”:“3.1.406”, “前滚”:“最新补丁” } }

【讨论】:

  • 将 sdk 版本更改为您的项目正在使用的版本的注释。
猜你喜欢
  • 2019-09-18
  • 2018-10-26
  • 1970-01-01
  • 2014-01-07
  • 2021-10-21
  • 1970-01-01
  • 2020-05-07
  • 2022-11-21
  • 1970-01-01
相关资源
最近更新 更多