.net Core windows service to stdout - .net-core

First of all I don't understand windows services. Coming from a linux background it feels like very much of a black box.
We have a .net core 6 application that we want to run as either an executable or as a service. It uses the
WebApplication.CreateBuilder(args);
function. It also logs a lot to stdout (I know... we should be using a logger)
It works fine when we run by clicking the executable. But when we register it as a New-Service using powershell it will start and run for a few seconds and then stop.
We have done this with the older .net code and we put a special if block to detect if the application is running as a service or in the console.
Im not quite clear how the windows service knows that the application is not running. Do we have to return something from the application to show it started successfully.
Is there a way to see stdout when it run as a service.

Related

Batch File: Wait until multiple dotnet run commands are finished before launching browser shortcut

I have a batch file that executes:
start cmd /k dotnet run -p projectName
For two separate projects and then launches a shortcut link to the angular project in browser. The only (semi) problem is that the shortcut launches before the dotnet run commands have finished and so the browser believes the site cannot be reached until the dotnet run commands are complete.
I'm trying to not launch the shortcut link until both projects have finished executing the dotnet run command so the "This site can't be reached" dialog never shows up. I don't personally mind it, but it's rather confusing to anyone thinking the application should just open without the need for a reload.
EDIT:
The projects I'm running are a .NET Core Web API & .NET Core (Angular) Web App. The angular app get's compiled and served via dotnet run, but both projects need to remain running in order to listen to their specific ports. And the shortcut link executed at the end of the batch file is just to open the browser at localhost:AngularPort#. I understand a desktop app would have probably been the way to go from the start but for v2, the plan is to host in azure and allow customers to access the site as well instead of just being an internal app.
I found a solution on npm with a package called 'wait-on' which in my case will wait until an http(s) request on that url returns a response with a status code in the 200 range before completing. There's a whole other assortment of use-cases with this package but I only needed it for that.
Here's a link to the package if anyone else runs into this problem:
https://www.npmjs.com/package/wait-on
Edit:
Honestly this was only a problem because the laptop I needed to put the application on didn't have the best CPU stats, so it got hung up trying to serve a local api and angular application simultaneously.

log4net console appender logs viewing in .net core windows service application

Have anybody used console log4net appender in .net core based windows service application?
I am using .net core 2.1.6 console application which is hosted inside windows service. I have a file appender which is righting to file. wanted a console appender log view real time at console as well.
A Windows Service has no visible console, it runs on an invisible desktop. See Console.WriteLine() inside a Windows Service?.
If you want to view your log in real-time, then open the log with a viewer that supports that, for example tail -f on Git Bash, or through Notepad++ with the tail button (eye icon). Note that the latter tends to hang of the file is being written to a lot.

Run GUI application as part of BuildMaster deploy

I need to run a GUI application as part of BuildMaster deploy. Currently I added a PowerShell action that launches that application. The problem is the process is started but there is no GUI. I guess it is caused by the fact BuildMaster agent (that executes the PowerShell script) is a Windows Service. Is there any way to resolve it?
A Windows Service cannot launch applications with a GUI (as of Windows Vista / Server 2008 and newer at least), as they run in session 0 and will never be seen by an actual user. You might be able to hack around this by setting the "Interact With Desktop" flag, but that isn't really recommended at this point.
Is it possible to re-architect the application that has to be run into something that doesn't present a GUI and instead accepts command-line arguments?

Shell.Invoke doesn't launch the powershell script in deployed asp.net web application

I wrote an asp.net web application, which launches a powershell script. The program ran without any issue when I debugged it in my development environment. After publishing and deploying the asp.net web application (by the way the target server is the same machine localhost), by clicking the button for launching the powershell script, nothing happened. It means the powershell script was not launched. I attached the debugger to the deployed asp.net and saw that the Shell.Invoke() was hit as before and the command was also correct. However Shell.Invoke() returned immediately and the powershell script was not launched. In my environment the powershell execution policy is RemoteSigned.
Since the program in the development environment works perfectly and in the deployed case also hits the same code, I don't think it has to do with the code but more with security, certification,...???
Has anyone experienced the same? If yes, please advise me what I should do.
I appreciate your help in advance.
Thx
Might have something to do with the application pool identity not having the right permissions:
You might want to run your PowerShell invocation from a system service with explicit permissions though. That will take a steeper curve though.

Why would installation method affect .NET service?

I have built a WCF service that is hosted in a Windows Service following this article: http://msdn.microsoft.com/en-us/library/ms733069.aspx. Part of what the code in the service does is join a multicast group and listen for data that is broadcast to the group. Then it processes it. I have found that when I install the service manually using InstallUtil it works fine. To install it manually I do the following:
Build the MyService project in Visual Studio.
Right click on the Visual Studio Command Prompt and choose Run As Administrator
Navigate to the folder that has the MyService.exe file
Run the InstallUtil command as follows: installutil.exe MyService.exe
The service installs in Windows fine and then I start it. Then I run my ASP.NET application which is the client for the service and it runs fine. The service receives and processes the data just fine.
However I am trying to use Advanced Installer to build an MSI or EXE that will install the service and the ASP.NET application all at once so it doesn't have to be done manually. I am able to successfully create the Advanced Installer project that does this and it actually installs both the ASP.NET application and the Windows Service just fine and it starts my Windows Service too. However the really strange thing is that when I run the application I find that my service code can not receive any multicast data. It seems to block on that line of code and I never get any data. Does anyone know why this would happen? I have tried using an EXE and using "Run As Administrator" when I do the Advanced Installer installation. Here is the code from my service.
_groupAddress = IPAddress.Parse(_myIPAddress);
_listener = new UdpClient(_myPort);
_groupEP = new IPEndPoint(_groupAddress, _myPort);
_listener.JoinMulticastGroup(_groupAddress);
byte[] _bytes = _listener.Receive(ref _groupEP);
It seems to block on that last line of code and it never receives any data. This only happens when I install using Advanced Installer. When I install manually it works fine.
A service is configured to run under the identity of a user. Is this different when you install with the different methods?
Do you use the same port number in both cases, if not it could be the firewall.
99% sure that you have checked it, but check that the service is running after it is installed using advanced installer.
Check the event log for problems with the service.
It may be that your Windows Service is not running with sufficient credentials to perform this action. To test this, I'd recommend trying to change the user account being used for the service to see if that makes any difference.
To do this, go to the services applet (start, run, type services.msc). Find your service, right-click, properties, "Log On" tab, choose "This Account" and select an administrator user account that the service can run under.
I initially thought as the guys said this was a problem with the user credentials. But since you said in both cases the service is installed under the LocalSystem the problem seems to be elsewhere.
I recommend you first check the system "Events Viewer" for any messages regarding your message failing to start, maybe there you can find more information about the failure case.
If you can't find more detailed there I suggest a little bit of reverse engineering, to see what InstallUtil does and Advanced Installer doesn't, or the other way around. Advanced Installer comes along with the Repackager tool. You can use this tool to capture the system changes performed when running "InstallUtil" by providing a dummy executable to the Repackager when it is asking for the setup package, for example Notepad.
When the Repackager launches Notepad, leave it open and run your install command for the service, after the service finished installing, close Notepad and leave the repackager do its job. Then analyze the new project that it generates to see what resources has captured, like files, registry, services, etc...
You can also capture the install package create by Advanced Installer to see if the services installation from it creates less or more registration info for your service.

Resources