Reputation: 115
I'm having problem starting my web api. I am using .net core 3.0 Web Api. I was debugging and testing it locally using iis express without problems. but when I tried to deploy the web api to my linux server I get error messages that look like this:
crit: Microsoft.AspNetCore.Server.Kestrel[0] Unable to start Kestrel. System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase().
and running it on my local machine in debug mode does the same thing. so I created a new profile to start the executable when I debug the application in VS.
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll A path base can only be configured using IApplicationBuilder.UsePathBase().
here's the code in Program.cs and Startup.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel((a, b) => { })
.UseUrls("http://*:5050,https://*:5051")
.UseKestrel()
.UseStartup<Startup>();
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
connectionObject.SetConfiguration(Configuration);
// configure strongly typed settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
// configure jwt authentication
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<IUserData>();
var userId = int.Parse(context.Principal.Identity.Name);
var user = userService.GetById(userId);
if (user == null)
{
// return unauthorized if user no longer exists
context.Fail("Unauthorized");
}
return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
// configure DI for application services
services.AddScoped<IUserData, UD>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
}
here's my launchsettings
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60850",
"sslPort": 44372
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "Executable",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Kestrel": {
"commandName": "Executable",
"executablePath": ".\\WebApi.exe",
"applicationUrl": "http://localhost:5050"
}
}
}
I tried fiddling with
app.UsePathBase("/");
or
app.UsePathBase("http://localhost:5050")
but in the latter case the error message is the value needs to start with /
I have seen other complaining about this issue before, but their solutions did not work for me. why am I getting this at all?
Upvotes: 3
Views: 6619
Reputation: 8642
The UseUrls(...)
method expects the URL's to be separated by a semi-colon ;
, not a comma ,
.
Try changing the line in program.cs
to
.UseUrls("http://*:5050;https://*:5051")
The documentation says (emphasis mine):
The value provided using these approaches can be one or more HTTP and HTTPS endpoints (HTTPS if a default cert is available). Configure the value as a semicolon-separated list (for example, "Urls": "http://localhost:8000;http://localhost:8001").
You can see the complete documentation here
Upvotes: 6