I have made a simple .NET 6 Web API. This works fine when running from Visual Studio (by just pressing F5). However, when running my application as a self-contained service I get a 404 for calls that are supposed to succeed. How can I make sure my self-contained service starts behaving the same?
Here is my Program.json
using Microsoft.Extensions.Hosting.WindowsServices;
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
builder.Services.AddRazorPages();
builder.Host.UseWindowsService();
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();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
//app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
await app.RunAsync();
Here is my csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<UserSecretsId>XXXXX-78E75397-5A01-4397-9481-E423B4BF54C2</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
</ItemGroup>
</Project>
This is how I test:
- Open the solution file and press F5.
- Run
dotnet publish --no-build -c Release
executed from the folder with the csproj in it. This generates an exe in sideXXXXX\bin\Release\net6.0\win-x64
so I run the exe from this folder by going to this folder from the command prompt and then run the exe.
In both situations, I test using curl -I http://localhost:5000/swagger/index.html
.
This returns a 404 when doing option 2 (with dotnet publish
). When running from visual studio, I just get a 200.
How to make sure the result of dotnet publish
behaves the same as my application behaves when running from Visual Studio?