Skip to content

dotnet-getdocument issues in .NET7 #45025

@Wtiben

Description

@Wtiben

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We're currently on .NET6 and trying to migrate to .NET7. We're making use of <OpenApiGenerateDocumentsOnBuild/> and <OpenApiProjectReference/>. The implementation of GetDocument.Insider has changed in .NET7 (#43701) to start all IHostedService implementations and allow for all DI configuration to be executed. As pointed out by bachratyg this implementation might be handy for some cases, but is also completely undesirable for others. For example, we're using a IHostedService implementation to make a call to a microservice in the same solution at startup, which is now impossible to do. We're also validating the presence of certain configuration entries at startup e.g:
if (string.IsNullOrEmpty(config.GetConnectionString("database"))) { throw new Exception(); }. This now fails because these are defined in our docker-compose.yml file.

I'm not sure if there's a one-size-fits-all solution to this problem but it would at least be nice to have the option to revert back to the previous behaviour.

Expected Behavior

The project builds succesfully and generates an openapi document without invoking a bunch of application code.

Steps To Reproduce

A .csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
		<ImplicitUsings>enable</ImplicitUsings>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
		<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="7.0.0" PrivateAssets="all" />
	</ItemGroup>
</Project>

Example scenarios:

  • Register an IHostedService implementation that executes some logic which is impossible to do at build time. For example making a call to a microservice in the same solution.
  • Validate the presence of a configuration entry which is defined outside of appsettings.json (e.g. user secrets or docker-compose.yml). For example:
var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("Database");

if (string.IsNullOrEmpty(connectionString))
{
    throw new Exception();
}

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

The project now fails to build.

Exceptions (if any)

No response

.NET Version

7

Anything else?

Related:

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapiinvestigate

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions