gpt4 book ai didi

c# - Azure API 部署问题 - Swashbuckle SwaggenGen 错误

转载 作者:行者123 更新时间:2023-12-03 03:27:58 27 4
gpt4 key购买 nike

我在尝试从 VS 2022 将应用服务部署到 Azure 时遇到问题。每次收到此错误时:

Deployment error

我使用 .NET 6 作为我的目标框架,并且我已将 Swashbuckle.AspNetCore 包更新到最新版本。应用服务发布成功,但更新API时出现错误。

在我的项目版本中,没有 Startup.cs,而是 Program.cs,我实现 Swagger 生成的方式是:

builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();

我浏览过类似的帖子,并根据它们我可以补充一点,我也没有获取任何环境变量。

我也尝试过像这样使用 UseSwaggerUI() 但我还是得到了同样的错误:

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});

奇怪的是,部署对我的同事有效,对我也有效,直到上周,当 Program.cs 中的任何内容都没有更改时,它停止了。

我的程序.cs:

var builder = WebApplication.CreateBuilder(args);
var azureKeyVaultUrl = builder.Configuration[AppConfigurationConst.AzureKeyVaultUrl];
var logAnalyticsWorkspaceId = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.LogAnalyticsWorkspaceId);
var logAnaliticsAuthenticationId = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.LogAnaliticsAuthenticationId);
var CrmConnectionString = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.OneCrmConnectionString);

var logger = new LoggerConfiguration()
.WriteTo.AzureAnalytics(logAnalyticsWorkspaceId, logAnaliticsAuthenticationId)
.Enrich.FromLogContext()
.CreateLogger();

var serviceClient = new ServiceClient(CrmConnectionString);


builder.Logging.AddSerilog(logger);

logger.Information("Start ALGOI App Service");

builder.Services.AddControllers().AddNewtonsoftJson();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();
builder.Services.AddSingleton<IWebClientService, WebClientService>();

builder.Services.AddScoped<ICreateSessionService, CreateSessionService>();
builder.Services.AddScoped<ICreateFolderService, CreateFolderService>();
builder.Services.AddScoped<IDeleteFolderService, DeleteFolderService>();

builder.Services.AddScoped<IVerifyFolderService, VerifyFolderService>();
builder.Services.AddScoped<IUndoFolderService, UndoFolderService>();

builder.Services.AddSingleton(serviceClient);
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();


app.Run();

还有我的 .csproj:

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

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>{usersecretsid}</UserSecretsId>
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Connected Services\**" />
<Content Remove="Connected Services\**" />
<EmbeddedResource Remove="Connected Services\**" />
<None Remove="Connected Services\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="JsonSubTypes" Version="1.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Graph" Version="4.34.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.48.1" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0" />
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.16.0" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="RestSharp" Version="106.13.0" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.AzureAnalytics" Version="4.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="TypeSafe.Http.Net.HttpClient" Version="2.2.16" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="someprojectpath" />
<ProjectReference Include="someprojectpath" />
<ProjectReference Include="someprojectpath" />
</ItemGroup>

<ItemGroup>
<Content Update="local.settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>

appsettings.json:

{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "domain",
"TenantId": "tenantid",
"ClientId": "clientid",
"Scopes": "access_as_user",
"CallbackPath": "/signin-oidc",
"ClientSecret": "Client secret from app-registration. Check user secrets/azure portal.",
"ClientCertificates": []
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AzureKeyVaultUrl": "keyvaulturl",
"AllowedHosts": "*",
"MicrosoftGraph": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "user.read"
},
"ApplicationInsights": {
"ConnectionString": "appinsightsconnectionstring"
}
}

编辑:我发现错误的原因是这些行:

var azureKeyVaultUrl = builder.Configuration[AppConfigurationConst.AzureKeyVaultUrl];
var logAnalyticsWorkspaceId = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.LogAnalyticsWorkspaceId);
var logAnaliticsAuthenticationId = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.LogAnaliticsAuthenticationId);
var CrmConnectionString = AzureKeyVaultHelper.GetAzureKeyVault(azureKeyVaultUrl, AppConfigurationConst.OneCrmConnectionString);

此后我将它们更改为直接从配置而不是 keyvault 获取值:

var azureKeyVaultUrl = builder.Configuration.GetValue<string>(AppConfigurationConst.AzureKeyVaultUrl);
var logAnalyticsWorkspaceId = builder.Configuration.GetValue<string>(AppConfigurationConst.LogAnalyticsWorkspaceId);
var logAnaliticsAuthenticationId = builder.Configuration.GetValue<string>(AppConfigurationConst.LogAnaliticsAuthenticationId);
var CrmConnectionString = builder.Configuration.GetValue<string>(AppConfigurationConst.OneCrmConnectionString);

在本地运行时,它会正确获取值并将它们分配给变量。但似乎在部署时遇到了问题。在 var serviceClient = new ServiceClient(CrmConnectionString); 行中,当我将变量更改为静态字符串时,它可以正常工作,但是当使用该变量时,会弹出startup.cs错误(仅在部署时)。与使用日志 analitycs 值初始化记录器相同。同样,本地运行没有问题,只是部署崩溃了。

最佳答案

我创建了一个示例 ASP.NET Core 6.0 Web API,并且能够将应用程序部署到 Azure 应用服务,不会出现任何错误。

一般来说,当代码中有任何错误或配置问题时,就会发生此类错误。

In my version of the project there is no Startup.cs but Program.cs

.Net6 Core文件夹结构:

enter image description here

是的,我们需要在Program.cs本身中配置Swagger相关的代码。

检查以下步骤并确保您按照相同的步骤进行部署,没有任何问题。

  • 选择 ASP.NET Core Web API 模板。 enter image description here

  • 选择启用 openAPI 支持enter image description here

我的.csproj文件:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>

我的 Program.cs 文件:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

部署的 Azure 应用服务的输出:

enter image description here

enter image description here

关于c# - Azure API 部署问题 - Swashbuckle SwaggenGen 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75297594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com