ASP.NET Core: hosting.json urls not loaded - c#

I am trying to override the server url manually with a hosting.json file, however the url never get's used. I am on .net core 2.0.
hosting.json:
{
"urls": "http://localhost:5000"
}
Program.cs:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}
}

Visual Studio gives you two profiles to run application:
IIS Express, which fairly obviously runs the application using IIS Express and
WebApplication2 (or any given name of your app), the name of the web project, which runs the application using dotnet run using Kastrel instead of IIS.
You can try switching and running application from the second profile (it should pick up hosting.config).
If you want to run app on IIS you can change launchSettings.json that is located under project properties
Also, you can find more details how to configure urls here.

Related

Service Starts, but The Program does not Run

We've created a new hosted service using .NET Core 3.1 and the WorkerService template.
We want to run this service as a "Windows Service".
If I run the program manually using a Powershell shell, it runs fine with no problems.
So I then added it as a windows service. To do this, I followed these steps:
Added the Microsoft.Extensions.Hosting.WindowsServices package
Added the following to CreateHostBuilder:
public static IHostBuilder CreateHostBuilder(Serilog.ILogger logger, string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService() // <-- added this line
.ConfigureAppConfiguration(configurationBuilder =>
{
configurationBuilder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true);
})
Published it to a folder (zipped it and copied to server).
Added it as a service:
PS C:\Windows\system32> sc.exe create MyService binpath= "C:\Program Files\Services\MyService.exe"
5. Opened the Services snap-in and started the service.
The snap-in shows that it has a status of "running".
However, the program does not seem to actually do anything. It is not logging anything, including the most basic "Starting up" log message.
I even added a Seq sink on the off-chance that there was an IO problem preventing it from writing to a file. But nothing.
Can anyone identify what I am doing wrong?
As per comment on issue 36065, when a .NET Core 3.1 worker service runs as a windows service "Directory.GetCurrentDirectory() will point to %WINDIR%\system32". Therefore to get the current directory, you could use some reflection and something like:
public static IHostBuilder CreateHostBuilder(Serilog.ILogger logger, string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService() // <-- added this line
.ConfigureAppConfiguration(configurationBuilder =>
{
configurationBuilder.SetBasePath(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))
.AddJsonFile("appsettings.json", optional: true);
});
Worth noting, according to this comment, it's resolved in .NET 5.0

Disable https dotnet core 3.0 webapi

For dotnet core 2.x I was able to modify the program.cs file to specify ports:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:8080")
.Build();
For dotnet core 3.0, the program.cs file is a little different and no matter what I do, I still get the https option. The default program.cs file for dotnet core webapi has this:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
In the Windows command prompt, I've also tried dotnet new webapi --no-https in the command prompt and publishing and running the .dll it still listens on both http and https. It looks like this option removes https from the launchSettings.json file. I am not using Visual Studio or any IDE, only the Windows Command Prompt. What am I missing?
On the method Configure in Startup.cs, remove the line
app.UseHttpsRedirection();
I was able to completely disable https and close port 5001 in a Blazor and thus dotnet application by removing
app.UseHttpsRedirection();
from Program.cs and either adding
{
...,
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5000"
}
}
}
}
to appsettings.json or by launching the compiled binary with --urls=http://0.0.0.0:5000 as this limits the entpoints to http only.

Application is running inside IIS process but is not configured to use IIS server .Net Core 2.2

I have a .net core 2.2 application configured to run under IIS:
public static void Main(string[] args)
{
ProgramExtensions.ConfigureTypeConverters();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
And also in .csproj configured to run in process:
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
For some reason when running it under IIS Express I'm getting:
InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
What might be the reason of the error?

Host RestAPI inside IIS

I have a rather simple .NET WebApi application which I'm trying to host inside IIS. Followed all of the instructions from MS site about this.
here is the startup method for it.
public static void Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
I configured the Application pool to not use any managed code but I'm getting still this error in logs
Application startup exception: System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
I'm out of any ideas what can be wrong.
Any suggestions?
Using .NET Core 2.2
For InProcess, it uses IISHttpServer.
For your code, you are configuring UseKestrel() which uses out-of-process.
For solution, remove the .UseKestrel() line.
public static void Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
//.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
For more details, refer ASP.NET Core Module.
I want to use UseKestrel().
When I create .net core 2.0 or 2.1 project. I can use UseKestrel()
But If I create .net core 2.2 project I couldn't use UseKestrel()
I solved this problem like that
Create new .net core 2.0 or 2.1 project. Then, you can upgrade .net core project to 2.2. It will work.
But it is not effective solution.

How do I use impersonation in dotnet core 1.1?

I am trying to authenticate my MSSQL connection with Trusted_Connection=True. In order to get that working, I installed NuGet package Impersonate.AspNetCore.Windows (Impersonate), and set it up in my Startup.cs, like so:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseWindowsImpersonation(options => {
options.Enabled = true;
});
}
In my Program.cs I added UseIISIntegration to setup .net core to be hosted in IIS:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
In IIS I created a new Website, changed the ApplicationPool Basic Settings to No managed Code and Classic pipeline mode. In the Advanced Settings I set the Identity under Process Model to an existing Windows User account:
When I use Trusted_Connection=True in my connection string, the application fails to authenticate my connection to the SQL server:
"ConnectionStrings": {
"DashboardDatabase": "Server=<SOME_SERVER>;Database=<SOME_DATABASE>;Trusted_Connection=True"
}
(where and are replaced by the actual names of the server and database, of course).
My question is: Am I forgetting something here?
I had exactly the same setup as you and I got it working. Did you add forwardWindowsAuthToken="true" to your web.config file? You might have to create the web.config file in the root folder of your application if you haven't yet. See the package repo
FYI I also have Integrated Security=true; in my connection string, not sure if that is needed.

Categories