Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not run IHostedService implementations #2713

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions Swashbuckle.AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Swashbuckle.AspNetCore.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinimalApp", "test\WebSites\MinimalApp\MinimalApp.csproj", "{3D0126CB-5439-483C-B2D5-4B4BE111D15C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalAppWithHostedService", "test\WebSites\MinimalAppWithHostedService\MinimalAppWithHostedService.csproj", "{DD2A4D91-C071-4767-A4BE-7F3772DA134F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -486,6 +488,18 @@ Global
{3D0126CB-5439-483C-B2D5-4B4BE111D15C}.Release|x64.Build.0 = Release|Any CPU
{3D0126CB-5439-483C-B2D5-4B4BE111D15C}.Release|x86.ActiveCfg = Release|Any CPU
{3D0126CB-5439-483C-B2D5-4B4BE111D15C}.Release|x86.Build.0 = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|x64.ActiveCfg = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|x64.Build.0 = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|x86.ActiveCfg = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Debug|x86.Build.0 = Debug|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|Any CPU.Build.0 = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|x64.ActiveCfg = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|x64.Build.0 = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|x86.ActiveCfg = Release|Any CPU
{DD2A4D91-C071-4767-A4BE-7F3772DA134F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -524,6 +538,7 @@ Global
{76692D68-C38C-4A7D-B3DA-DA78A393E266} = {0ADCB223-F375-45AB-8BC4-834EC9C69554}
{66590FBA-5FDD-4AC9-AF91-26ADAB33CCB8} = {0ADCB223-F375-45AB-8BC4-834EC9C69554}
{3D0126CB-5439-483C-B2D5-4B4BE111D15C} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
{DD2A4D91-C071-4767-A4BE-7F3772DA134F} = {DB3F57FC-1472-4F03-B551-43394DA3C5EB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {36FC6A67-247D-4149-8EDD-79FFD1A75F51}
Expand Down
12 changes: 12 additions & 0 deletions src/Swashbuckle.AspNetCore.Cli/HostingApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void ConfigureHostBuilder(object hostBuilder)
{
services.AddSingleton<IServer, NoopServer>();
services.AddSingleton<IHostLifetime, NoopHostLifetime>();

for (var i = services.Count - 1; i >= 0; i--)
{
// exclude all implementations of IHostedService
// except Microsoft.AspNetCore.Hosting.GenericWebHostService because that one will build/configure
// the WebApplication/Middleware pipeline in the case of the GenericWebHostBuilder.
if (typeof(IHostedService).IsAssignableFrom(services[i].ServiceType)
&& services[i].ImplementationType is not { FullName: "Microsoft.AspNetCore.Hosting.GenericWebHostService" })
{
services.RemoveAt(i);
}
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\WebSites\Basic\Basic.csproj" />
<ProjectReference Include="..\WebSites\MinimalAppWithHostedService\MinimalAppWithHostedService.csproj" />
<ProjectReference Include="..\WebSites\MinimalApp\MinimalApp.csproj" />
<ProjectReference Include="..\..\src\Swashbuckle.AspNetCore.Cli\Swashbuckle.AspNetCore.Cli.csproj" />
</ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions test/Swashbuckle.AspNetCore.Cli.Test/ToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ public void Can_Generate_Swagger_Json_ForTopLevelApp()
dir.Delete(true);
}
}

[Fact]
public void Does_Not_Run_Crashing_HostedService()
{
var dir = Directory.CreateDirectory(Path.Join(Path.GetTempPath(), Path.GetRandomFileName()));
try
{
var args = new string[] { "tofile", "--output", $"{dir}/swagger.json", Path.Combine(Directory.GetCurrentDirectory(), "MinimalAppWithHostedService.dll"), "v1" };

Assert.Equal(0, Program.Main(args));

using var document = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir.FullName, "swagger.json")));

// verify one of the endpoints
var paths = document.RootElement.GetProperty("paths");
var path = paths.GetProperty("/ShouldContain");
Assert.True(path.TryGetProperty("get", out _));
}
finally
{
dir.Delete(true);
}
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DotNetSwaggerPath>$([System.IO.Path]::Combine("..", "..", "..", "src", "Swashbuckle.AspNetCore.Cli", "bin", $(Configuration), $(TargetFramework), "dotnet-swagger"))</DotNetSwaggerPath>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.SwaggerGen\Swashbuckle.AspNetCore.SwaggerGen.csproj" />
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.SwaggerUI\Swashbuckle.AspNetCore.SwaggerUI.csproj" />
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.Swagger\Swashbuckle.AspNetCore.Swagger.csproj" />
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.Cli\Swashbuckle.AspNetCore.Cli.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions test/WebSites/MinimalAppWithHostedService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "MinimalApp", Version = "v1" });
});

builder.Services.AddHostedService<HostedService>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MinimalApp v1"));
}

app.MapGet("/ShouldContain", () => "Hello World!");

app.Run();

class HostedService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
throw new Exception("Crash!");
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"MinimalAppWithHostedService": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7128;http://localhost:5201",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}