Running ASP NET Core from CommandLine - c#

I have this AspNetCore application that runs ok under VS2019. Listens correctly on 5001 and all.
But when I try to run it from bin\Debug\necoreapp3.1 folder from command line using
dotnet mywebapp.dll
it goes up normally (stating that it is correctly listening on 5000 and 5001 ports with the correct content path root) as if it was executed from VS2019:
info: WorkflowCore.Services.WorkflowHost[0]
Starting background tasks
info: WorkflowCore.Services.BackgroundTasks.RunnablePoller[0]
Polling for runnable workflows
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\work\mywebapp
info: WorkflowCore.Services.BackgroundTasks.RunnablePoller[0]
but when I try to connect to https://localhost:5001 via browser it is unable to connect.
after a while this log appears in the output:
warn: Microsoft.AspNetCore.Server.Kestrel[22]
Heartbeat took longer than "00:00:01" at "09/01/2021 11:02:44 +00:00". This could be caused by thread pool starvation.
Anybody can help me?
Thanks in advance.
P.

Related

Cannot access to dockerized net 6 api

My api project (my-custom-api) is using rabbitmq instance. Since both are running in docker I created custom docker network and run both containers on that same network.
docker create my-network
docker run -d --hostname my-rabbitmq --network my-network --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.11-management
docker run --network my-network --publish 8090:8080 -d my-custom-api
on docker ps -a both containers are up and running and inside docker log for my-custom-api there is no errors or warnings
However when I hit localhost:8090/swagger/index.html I'm getting This site can’t be reached. Using curl localhost:8090 I'm getting url: (52) Empty reply from server
Here's the content of Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /App
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "My-Custom-Api.dll"]
Again, I'm not getting any errors in the docker log when trying to access localhost:8090 but still cannot access api from this address.
What I'm doing wrong here.
Update:
After I changed run command to include environment Development my container logs looks like this
2023-01-22 20:58:55 info: Microsoft.Hosting.Lifetime[14]
2023-01-22 20:58:55 Now listening on: http://[::]:80
2023-01-22 20:58:55 info: Microsoft.Hosting.Lifetime[0]
2023-01-22 20:58:55 Application started. Press Ctrl+C to shut down.
2023-01-22 20:58:55 info: Microsoft.Hosting.Lifetime[0]
2023-01-22 20:58:55 Hosting environment: Development
2023-01-22 20:58:55 info: Microsoft.Hosting.Lifetime[0]
2023-01-22 20:58:55 Content root path: /App/
2023-01-22 20:58:56 info: MassTransit[0]
2023-01-22 20:58:56 Bus started: rabbitmq://my-rabbitmq/
I still cannot open localhost:8090/ with or without swagger.
Swagger is not available by default when your program runs in a container.
In your Program.cs, you should see an if statement that makes it so Swagger is only available when your program runs in a development environment. A container is - by default - not considered development.
To run the program in development mode, you need to set the ASPNETCORE_ENVIRONMENT variable to 'Development'.
Another thing to be aware of is that the asp.net containers set the environment variable ASPNETCORE_URLS to http://+:80 making your app listen on port 80. So you need to map that, rather than port 8080.
You can see that your app is listening on port 80 in the log where it says Now listening on: http://[::]:80.
That means that you end up with a command like this:
docker run --network my-network --publish 8090:80 -d -e ASPNETCORE_ENVIRONMENT=Development my-custom-api
When you do that, Swagger will be available on port 8090.

Swagger Page is 404 Not Found in net-core

swagger is accessible when I run it in the terminal.
enter image description here
enter image description here
swagger 404 when i create the service and start it.
[Unit]
Description=generatesql
After=network-online.target
[Service]
WorkingDirectory=/home/www/tool/generatesql/
ExecStart=/usr/bin/dotnet /home/www/tool/generatesql/DBChange.dll
KillMode=process
Restart=on-failure
RestartSec=10
[Install]
WantedBy=default.target
root#server:/home/www/tool/generatesql# systemctl start generatesql
root#server:/home/www/tool/generatesql# systemctl status generatesql
● generatesql.service - generatesql
Loaded: loaded (/etc/systemd/system/generatesql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-09-28 14:14:34 CST; 6s ago
Main PID: 437150 (dotnet)
Tasks: 26 (limit: 18829)
Memory: 21.7M
CGroup: /system.slice/generatesql.service
└─437150 /usr/bin/dotnet /home/www/tool/generatesql/DBChange.dll
9月 28 14:14:34 server systemd[1]: Started generatesql.
9月 28 14:14:35 server dotnet[437150]: info: Microsoft.Hosting.Lifetime[14]
9月 28 14:14:35 server dotnet[437150]: Now listening on: http://[::]:10001
9月 28 14:14:35 server dotnet[437150]: info: Microsoft.Hosting.Lifetime[0]
9月 28 14:14:35 server dotnet[437150]: Application started. Press Ctrl+C to shut down.
9月 28 14:14:35 server dotnet[437150]: info: Microsoft.Hosting.Lifetime[0]
9月 28 14:14:35 server dotnet[437150]: Hosting environment: Production
9月 28 14:14:35 server dotnet[437150]: info: Microsoft.Hosting.Lifetime[0]
9月 28 14:14:35 server dotnet[437150]: Content root path: /home/www/tool/generatesql/
enter image description here
I don't have anywhere to look at the logs, how can I locate this problem.

.NET Core 3.1 Worker Service - set EnvironmentName on publish

I'm trying to setup a worker service using the asp.net core 3.1 template provided by VS 2019 Enterprise. So far:
1 / Created a new project and ran it: "Hosting Environment: Development", works as expected.
Development
2 / Created a FileSystem publish profile, and added <EnvironmentName>Test</EnvironmentName> to publish the worker to a testing environment for exemple
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<EnvironmentName>Test</EnvironmentName>
</PropertyGroup>
</Project>
When launching the published version, HostingEnvironment is set to "Production", but i defined "Test" in the FolderProfile.pubxml
Production
I've tried dotnet publish /p:EnvironmentName=Test, without success.
I also tried to generate manually a web.config file, containing
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Test" />
The worker service seems to ignore the web.config file.
It works on VS while debugging, having DOTNET_ENVIRONMENT = Test
Test
What's the correct way to publish worker services on different environments ?
My recommended way would be to push the same artifact to all your environments and set the environment variable DOTNET_ENVIRONMENT to your desired value.
SET DOTNET_ENVIRONMENT=test2
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:27:34 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test2
Another way to do it would be to publish a json file alongside your deployment.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1
// using Microsoft.Extensions.Configuration;
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
});
The content of your hostsettings.json could be:
{
"Environment": "test"
}
Now you only need to use the json file that matches your desired target, and copy that next to your DLL.
dotnet WorkerService1.dll
info: WorkerService1.Worker[0]
Worker running at: 03/18/2020 17:06:03 +01:00
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: test
info: Microsoft.Hosting.Lifetime[0]
For any one that stumbles over this question I hade the same issue.
Going through Host.CreateDefaultBuilder source code it shows we can set DOTNET_[Variable] from args passed
so to run your service in Development run the following command
dotnet DLL:
dotent [DllName].dll --ENVIRONMENT=Development
exe:
[ExeName].exe --ENVIRONMENT=Development
Dotnet core source:

How to activate generic host startup message

I've set up a generic host by instantiating a host builder and calling build function on the host and running it. When the console pops up, no start message is shown.
I'm running my code in visual studio 2017. I've tried this on another computer and there I get a startup message. I'm doing everything the same way.
static void Main(string[] args)
{
var hosting = new HostBuilder();
hosting.Build().Run();
}
Console pops up and is empty,
I expect to see something like the following in the console:
Using launch settings from C:\repos\andrewlock\blog-examples\suppress-console-messages\Properties\launchSettings.json...
Hosting environment: Development
Content root path: C:\repos\andrewlock\blog-examples\suppress-console-messages
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Ok so was about to give up before i finally figured it out. Its was trivial but annoying. So i compared the packages from both machines and on the one where it didnt work i had the preview 3.0.0 version of Microsoft.Extensions.Hosting to version 2.0.0 like on my other machine and viola!
Guess moral of the story is not to expect standard behavior from preview releases.

chrome driver : disconnected: unable to connect to renderer

I am working with c# selenium.
I am getting error with chrome driver as :
System.InvalidOperationException : disconnected: unable to connect to renderer
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.32.498550
(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (102)
Now I tried updating the chrome browser version to 2.33 and its working fine on my local machine.
But I am getting this error my server where I am executing this tests on schedule basis.
I am using TFS to deploy tests on the server and to run the tests.
the same project is working on my local machine but I am getting this error on server for the
driver.Manage().Window.Maximize();
line for all the tests.
I check out same project from TFS on my colleagues machine and tried executing there its was working fine.
I tried updating driver to 2.33 even tried downgrading the driver to 2.32 and executed multiple time but its not working.
I am using MS build to build the automation project and deploy it on server as well.
Since it works well on your local machine,but got error when you run the tests on the server.
First make sure your server environment are the same with your local machine such as chrome browser version is 2.33 and the driver is also 2.33.
According to this code driver.Manage().Window.Maximize(); line.
Please make sure that you give enough time for the window to load before you declare this statement.
If you are finding any element to input some data then provide reasonable delay between this and the input statement.
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 2499
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.WebDriverException: disconnected: unable to connect to renderer
(Session info: chrome=62.0.3202.75)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
I have faced the same problem today, It get resolved by using updated chrome driver pack 2.33.
When I am using Python 3, facing the same error, in my situation, I did not close the chrome process after using it. So you should check the chrome process properly closed after using it.

Categories