Application is running inside IIS process but is not configured to use IIS server .NET Core 3.0 - asp.net-core-3.0

I have migrated our application from .NET Core 2.2 to version 3.0. Actually I created the new application in 3.0 from scratch and then copied source code files. Everything looks great but when I try to run the application within Visual Studio 2019 I get the exception:
Application is running inside IIS process but is not configured to use IIS server
Here is my Program.cs
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseKestrel();
webBuilder.UseIISIntegration();
webBuilder.UseStartup<Startup>();
});
}
The error occurs at the line: CreateHostBuilder(args).Build().Run();
It worked fine in .NET Core 2.2 but it doesn't want to run as 3.0. I cannot find anything else that should be done. Something new in the Startup.cs? I don't know.

I also came across this issue while following the documentation https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio
For your case I have checked and the code below will work, with the call to webBuilder.UseKestrel() removed.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseIISIntegration();
webBuilder.UseStartup<Startup>();
});

I had the same error then to fix my problem I needed to change webBuilder.UseKestrel(); to ConfigureKestrel(serverOptions => {})
My Program.cs before:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseKestrel();
webBuilder.UseStartup<Startup>();
});
My Program.cs fixed:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(serverOptions =>
{
})
.UseStartup<Startup>();
});

In my case I ran wrong profile (IIS Express) in Visual Studio by carelessness.

As #flam3 suggested, you need to select a profile with correct command.
Open Solution Explorer -> Your Project Name -> Properties -> launchSettings.json file.
The value of the key commandName should be 'Project' instead of 'IIS Express'
So select profile with the name Elsa.Samples.UserRegistration.Web(in my case). See below. Or you may try changing IISExpress to Project on line no 19

For Visual Studio 2019 and 2022
Go to API project properties: Debug Tab -> General -> Hit "Open Debug launch profiles UI"
Scroll Down to Hosting Model and Select "Out Of process"
Then, you will be able to run your application with no Issues.

Related

Dotnet 5 to 6 migration - application fails to start - How to use IIS?

I have migrated my web app from dotnet 5 to dotnet 6. Firstly, I updated dotnet version for all projects in solution, and then updated necessary nuget packages accordingly. After this, the application worked fine.
After that, I wanted to improve my Program.cs class, in order to get rid of Startup.cs. In dotnet 5, my program.cs looked like this:
public static int Main(string[] args)
{
.....
.....
var host = CreateHostBuilder(args).Build();
host.Run();
return 0;
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseNLog()
.ConfigureLogging((host, loggingBuilder) =>
{
loggingBuilder.ClearProviders();
if (host.HostingEnvironment.IsDevelopment())
{
loggingBuilder.AddConsole();
loggingBuilder.AddNLog(".\\nlog.config");
}
else
{
loggingBuilder.AddNLog(".\\nlog.config");
}
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseIIS();
webBuilder.UseStartup<Startup>();
});
I updated this to:
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureLogging((host, loggingBuilder) =>
{
loggingBuilder.ClearProviders();
if (host.HostingEnvironment.IsDevelopment())
{
loggingBuilder.AddConsole();
loggingBuilder.AddNLog(".\\nlog.config");
}
else
{
loggingBuilder.AddNLog(".\\nlog.config");
}
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseIIS();
//webBuilder.UseStartup<Startup>(); --> removed this
});
After this change, I get error:
I suspect, I cannot use ConfigureWebHostDefaults anymore. So how can I configure my app to UseIIS?
My hosting model is "InProc", and I cannot change that.
When I tried adding builder.Services.Configure<IISServerOptions>() or builder.WebHost.UseIIS();, but it only changed the error to:
The Event Logs show following two errors:
Application '/LM/W3SVC/1/ROOT/portal' with physical root 'C:\Users\zee\source\repos\Portal\Portal.Web\' has exited from Program.Main with exit code = '0'. Please check the stderr logs for more information.
Application '/LM/W3SVC/1/ROOT/portal' with physical root 'C:\Users\zee\source\repos\Portal\Portal.Web\' failed to load coreclr. Exception message: CLR worker thread exited prematurely
The docs HERE says that WebApplication.CreateBuilder calls Kestrel internally. How this behavior can be changed to use IIS instead?
I have tried few other things, but did not work.
Question:
Can someone point me to the right direction? Why there is no clear guidelines on using IIS? Several answers suggest that I should change to OutOfProc, which I cannot.

Unable to create an object of type 'AppContext'. Using verbose but

I tried to do my first migration and I got this error.
I used:
dotnet ef migrations add initial --project AppProject.Core --verbose
The error:
Finding application service provider in assembly 'AppProject.Core'...
Finding Microsoft.Extensions.Hosting service provider...
-> No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
-> No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'AppContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'AppContext'.
My Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
My Startup
services.AddDbContext<AppContext>(options =>
{
options.UseNpgsql(connection);
});
services.AddIdentity<User, IdentityRole<Guid>>()
.AddEntityFrameworkStores<SysFishingContext>()
.AddDefaultTokenProviders();
I don't get what is the problem. Please someone help me.
Try to specify both options --project and --startup-project
-p|--project <PROJECT>
This is the project that DbContext is sitting in.
-s|--startup-project <PROJECT>
This is the project with Depenedency Injection setup, usually the Web project.
Your migrations add command might look like below:
dotnet ef migrations add initial --startup-project AppProject.Web --project AppProject.Core --verbose

How to use HTTPS with Kestrel server for the .Net Core web api

I have created .net core web api. Now its running as the windows background service and its basically listen to the Http request. But I want to change this application to support HTTPS request. So what I did I have changed Program.cs file.
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000);
options.Listen(IPAddress.Any, 80);
options.Listen(IPAddress.Loopback, 443, listenOptions =>
{
listenOptions.UseHttps("certificate.pfx", "password");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
});
}
But here .ConfigureWebHostDefaults(webBuilder => I am getting following error.
Operator '.' cannot be applied to operand of type 'void' [MHScaleInboundWebAPI]csharp(CS0023)
So I need to fix this error and need to correct above class to support with the https request like this. https://localhost:8080/Customer. I need to change this Program.cs calss. I am using 8080 port number.

Configure Serilog in CreateHostBuilder method using several appsettings files

To cofigure serilog I modified main function, adding all needed config:
public static void Main(string[] args)
{
ConfigLogger();
CreateHostBuilder(args).Build().Run();
}
private static void ConfigLogger()
{
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config).CreateLogger();
}
At this point it works fine. The issue is that I have two different appsettings files, one for production and another for development.
My first thought was using #if DEBUG directive around AddJsonFile line. Then I read I could avoid this directives by making configuration at CreateHostBuilder method, and removing all my ConfigLogger. Modified CreateHostBuilder method:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((ctx, logConfig) => logConfig.ReadFrom.Configuration(ctx.Configuration))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
This should be working but I get an exception with an unclear message, but realted to DependencyInjection as I see in the stacktrace. Exception mesage:
Exception has been thrown by the target of an invocation. (Invalid
URI: The format of the URI could not be determined.)
Application gets closed showing an MessageBox: "There is no process associated with this object".
EDIT:
If I remove all settings related to serilog from appsettings.json file, it loads well. When I have all settings in both files, appsettings.json and appsettings.development.json it tries to read both files and fail, giving me an exception.

Configuring Rebus in a .net core Worker Service (or a Console App)

I have seen that Adding rebus in the ASP.NET Core execution pipeline is very neat using Startup.cs.
I wonder if there is a same neat way to do the same for Worker service or generally a console app.
Most .net core console apps I have seen are very simple demo applications.
Kindly if there is any concrete sample configuration using .net core console application.
Regards
Amour Rashid
One way would be to add Microsoft's Microsoft.Extensions.Hosting package and build your background service as a BackgroundService:
public class MyBackgroundService : BackgroundService
{
readonly IServiceCollection _services = new ServiceCollection();
public BackgroundService()
{
// configure the bus
services.AddResbus(
configure => configure
.Transport(t => t.Use(...))
);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var provider = _services.BuildServiceProvider();
// start the bus
provider.UseRebus();
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
}
}
}
which you then add by going
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<MyBackgroundService>();
});
in your startup.
Thanks Mogens,
Another way is to
var host =CreateHostBuilder(args).Build();
host.UseRebus();
host.Run();
Or in ConfigureServices method
....
var provider=services.CreateServiceProvider();
provider.UseRebus();
It helped me I could create Worker Services using rebus.

Resources