Disable https dotnet core 3.0 webapi - c#

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.

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

Static file not served through IIS but serving through localhost - ASP.NET Core 3.1 MVC & C#

I am new to programming, and I was able to solve most of the issues all by myself. But this issue goes over my head.
I am using Visual Studio to run my app locally. When I click on "IIS Express" within VS to run the app, the app is opening in a browser with url http://localhost:1234/ and serving static files.
However when I use IIS to host my app, like when I type myapp.com in my browser, I am seeing website in plain text. And no static files (css, js) are being served.
Not sure what is causing the issue. I googled for long time and read lot of posts, but most of them are asking to check if IIS has static role service turned or suggesting to use app.UseStaticFiles() within startup.cs file. Which in my case is done.
program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
Startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDefaultFiles();
app.UseStaticFiles();
}
Any help is much appreciated.
I didnt know that when we publish it to "files and folder" from Visual studio, any changes that we make in Visual studio will not go through to published folder. Because of this reason "app.UseStaticFiles();" command (that I wrote after publishing) is not passed on to published folder. I had to delete the published folder and republish to make everything work fine.

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.

ASP.NET Core: hosting.json urls not loaded

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.

Categories