Reputation: 567
UPDATE
My Web API no longer reports 404 Not Found. In debug I discovered a SwaggerGeneratorException error saying
Failed to generate schema for type - [TypeName]...Can't use schemaId...The same schemaId is already used for type [TypeName]
To fix this I replaced
builder.Services.AddSwaggerGen();
with
builder.Services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});
After rebuilding and publishing the project I now get a 500 Internal Server Error. The full error message says:
HTTP Error 500.31 - Failed to load ASP.NET Core runtime Common solutions to this issue: The specified version of Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found. Troubleshooting steps: Check the system event log for error messages Enable logging the application process' stdout messages Attach a debugger to the application process and inspect
APPLICATION LOG
ERROR: Failed to start application '/LM/W3SVC/5/ROOT', ErrorCode '0x8000ffff'.
ERROR: Could not find 'aspnetcorev2_inprocess.dll'. Exception message: It was not possible to find any compatible framework version The framework 'Microsoft.NETCore.App', version '8.0.0' was not found.
You can resolve the problem by installing the specified framework and/or SDK. The specified framework can be found at:
ERROR: Unable to locate application dependencies. Ensure that the versions of Microsoft.NetCore.App and Microsoft.AspNetCore.App targeted by the application are installed.
WARNING: Could not start stdout file redirection to '.\logs\stdout' with application base 'C:\inetpub\wwwroot\SamadhiAU_API'. create_directories: Access is denied.: "C:\inetpub\wwwroot\myAPIFolder.\logs".
ORIGINAL POST
I'm migrating a Web API project from Core 3.1 to .NET 8.0. I created a new Web API project in VS 2022 and ported all the classes across. I deleted all outdated dependencies and added new ones as appropriate.
The project builds successfully and when running in Debug I can use Postman to call the various endpoints and everything works as expected.
I created a new site in IIS, added the appropriate bindings and also installed a proper SSL cert. I've used server name indication because I already have an existing API on 443, albeit with different domain.
I published the project to a local folder, then copied the files to the correct folder in IIS.
When I open postman to test the API, I get 404 Not Found
In debug, calling POST: https://localhost:7297/users/authenticate I get the expected JSON response.
In production, https://mydomain/users/authenticate returns HTML and 404 Not Found.
Program.cs
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using SamadhiAPI;
using SamadhiAPI.API;
using SamadhiAPI.Models;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddCors();
builder.Services.AddControllers();
// learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// configure strongly typed settings objects
var appSettingsSection = builder.Configuration.GetSection("AppSettings");
builder.Services.Configure<AppSettings>(appSettingsSection);
builder.Services.AddScoped<IUserService, UserService>();
// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
builder.Services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
ClockSkew = TimeSpan.Zero
};
});
var app = builder.Build();
app.UseRouting();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
// global cors policy
app.UseCors(x => x
.SetIsOriginAllowed(origin => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
web.config
Auto-generated and remains unchanged.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\SamadhiAPI.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: b7f93657-cfc6-4b8d-91d7-33f7e9e4960b-->
STEPS TAKEN
LOG FILE: /inetpub/logs/log files/W3SVC5
#Software: Microsoft Internet Information Services 10.0
#Version: 1.0
#Date: 2024-05-27 08:04:36
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2024-05-27 08:04:36 xxx.x.xx.xxx GET / - 80 - 199.45.155.43 Mozilla/5.0+(compatible;+CensysInspect/1.1;++https://about.censys.io/) - 500 31 2147549183 615
#Software: Microsoft Internet Information Services 10.0
#Version: 1.0
#Date: 2024-05-27 08:54:12
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status time-taken
2024-05-27 08:54:12 xxx.x.xx.xxx POST /users/authenticate - 443 - 159.196.169.55 PostmanRuntime/7.34.0 - 500 31 2147549183 505
2024-05-27 08:54:31 xxx.x.xx.xxx POST /users/authenticate - 443 - 159.196.169.55 PostmanRuntime/7.34.0 - 500 31 2147549183 9
Upvotes: 1
Views: 6002
Reputation: 1
by default the asp.net core app don't allow swagger discorvery in other environement such as Prod, check that the swagger is added properly in the Env you are working in your program.cs enter image description here
Upvotes: 0
Reputation: 141
Install the hosting bundle.
I had this same concern when trying to access a newly deployed web application on a Windows server. So I installed the .NET Core Hosting bundle using this link https://dotnet.microsoft.com/permalink/dotnetcore-current-windows-runtime-bundle-installer which is an installer for the:
• .NET Core Runtime
• ASP.NET Core Module
Upvotes: 3
Reputation: 567
My original error message was 404 Not Found. In debug I discovered that this error was being caused by a SwaggerGeneratorException error saying "Failed to generate schema for type - [TypeName]...Can't use schemaId...The same schemaId is already used for type [TypeName]"
To fix this error in Program.cs I replaced
builder.Services.AddSwaggerGen();
with
builder.Services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});
After rebuilding and re-publishing the project I got a new error '500 Internal Server Error' saying 'Failed to load ASP.NET Core runtime'. After inspecting the Windows server 'Application' event log I found various error messages including:
This error was fixed by installing the ASP.NET Core Runtime 8.0.5 Windows Hosting Bundle from the following URL: https://dotnet.microsoft.com/en-us/download/dotnet/8.0
A few red herrings along the way included:
I did not need to change my Deployment mode from the default value of 'Framework-dependent' with Target Runtime 'Portable'.
Upvotes: 0