How to run bat file as Windows service - c#

I have a bat file in which I have called a simple console application in c#. Whenever I run the bat file it displays the console and run. Can any one tell me how to run bat file as windows service so that to avoid this

I think this is what you're looking for:
Running a C# console application as a Windows service
It's not straightforward due to the fact that console app doesn't automatically qualify as a service, you will have to do some coding.
By the sound of your post though it seems as if you initially wanted to build a service in the first place, in that case, start from scratch and make a Windows service.

Related

Updating and restarting the current running Windows Service in C#

I have a .NET 6 application compiled as a standalone executable that runs as a Windows service. What I want to add is a way to update and restart the service when it detects that a new version is available. The problem is that I can't find a way to actually restart a service with a single command in a way that can be triggered from C#.
The sc.exe can be used to create, modify, start and stop a service. But it does not have a restart command. My original plan was to modify the service and point it to the new binary, and then restart it using sc.exe, but then I noticed that it doesn't actually have a real restart command.
The Powershell cmdlet Restart-Service does what I want, but the C# Powershell integration seems to have some severe issues and bugs when the C# application is compiled as a standalone executable. It simply does not work at all for me.
I'm triggering the update from the running Windows service itself, so I can't just execute a stop command and then try to execute the start command, as my application won't be running anymore at that point.
One option might be to execute both sc.exe stop and sc.exe start in a way that isn't interrupted when the calling process is stopped, but I'm not sure which method would work reliably here. Another idea would be to slightly misuse the auto-restart on failure mechanism for Windows services. But I'm not sure I can trigger that without creating superfluous error messages in the Windows Event logs.
What options do I have to reliably restart a running Windows service from the running C# application itself?

I have a c# a console app that I need to convert to an Azure function

I have a console app which (simplified) takes the response of a sql query and dumps it into a CSV file.
This needs to be run once a day, and I have been asked to convert app to an Azure managed function.
I added an Azure function timer project to my solution, added the dependency/reference to my console app cproj (in the same solution). Then I added a line to the AzureFuntion Run method to execute the ConsoleAapp.Main().
(Intelisense is all happy)
I run it locally and it bombs out with an error that it can’t find the project/method at the path provided (which is correct; I verified.)
This will eventually be deployed to the cloud; but I don't have access to the Azure portal; have never developed for Azure, so I am flying blind in a sense because all the documentation/tutorials start with or exclusively develop in the portal.
Help?
per CrowCoder: Move your code from the console app into a class library or even into the function app. itself.
Changing the console app .cproj to a class library did the trick!

What is the best way to keep a C# Console app running [duplicate]

This question already has answers here:
How to keep a .NET console app running?
(9 answers)
Closed 6 years ago.
I have developed a console app which must run in the background. The app must constantly check a database for new records. If new records are returned they are processed. My app as it currently stands uses while(true) to keep the application running. Is using a while loop the best solution.
A snippet of my code:
static void Main(string[] args)
{
while(true)
{
// Query db for new records
if(record_count > 0)
{
// Process the records
}
Thread.Sleep(500);
}
}
Create as Windows Service Application
What a Windows Service is ?
Enables you to create long-running executable applications that run in their own windows session.
Can be automatically started when the computer boots, can be paused and restarted without any user interaction.
Easily installable by running the command line utility InstallUtil.exe and passing the path to the service's executable file.
Why a Windows Service?
One of the most common requirements of some businesses is long-running scheduled jobs based on some time interval. For example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.
So building a Windows Service could be one of the reliable solutions to do the goal that can do the required job in the behind the scenes without interfering others users on the same computer.
Please refer the link for step by step implementation -
http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/
Your approach to keep your console application continuously running is absolutely fine. Only thing is that you want to keep it running in background silently without user to know anything. Since a console app has a windowed UI (a command console) the user will keep seeing it. Here is what you can do to get rid of command console. Since you already have your console application with you, here is what you need to do :
Go to project properties -> Go to application Tab -> Change the Output type to Windows Application
Phew! Now your same console application will run without a command console and your code will keep running.
Creating a windows service will be too much of a kill for such a simple logic. Also installing a windows service is an extra overhead, though I am not aware of the deployment you are looking forward to for your application in production.
Better option would be to use Windows service that run in background.
Creating a Windows Service Application
Develop and Install a Windows Service in C#
Just google and you can find lots of article and tutorial on Windows Service.
There is no need to make your program running as a services. Services aren't for these small tasks. You should simply create a Windows (GUI, WinForms) application with a hidden window. Or, just don't create a window and just keep the Main running. Main would launch a thread and would itself wait for that thread.
A service has to be installed as a service and not all users would have permission to do that. It is complex task to have service, manage it, have security attributes settled et. al.
The hidden application can simply run in current user account (the service would be running in high-privilege SYSTEM or NETWORK account). Your application won't demand any privilege from current user - it just need SQL database/table read access.
Create a windows service which will check if your console app is running. If console app is down, then windows service will execute it again.
Don't write your console app's code into windows service. Because if it will start automatically at windows start up, then it may not work correctly as your console app.

Windows services does not run my class method

I have a windows service application which would scan the wifi list and save to file. then i will read the file and write to mysql database and close the file. my wifi scanning and db methods in another class within same windows service project which i invoke in service class. I am able to run and install the windows service but i think those methods are not called. I have even tried them in the service class but they still didn't call up in OnStart method. When i run it section mode and debug from visual studio it works fine but when i install it in installutil it only starts the service nothing happens though the service still runs in the background. I've created windows form version of this and that works fine.
I think it's more likely that the class method is called but writing the output file fails. The account that is used to run the service likely has no rights to create and/or write the output file. Check the folder that it should be written to and see if the account that runs your service has proper access rights. You can also use Process Monitor to verify this (set a filter on Path or Process Name).

Calling an application from a server

I have an (console, .NET 3.5, C#) application that I'm trying to call from a Windows Server (2003, with .NET 3.5) from a system (Windows server 2003 with .NET 2.0) via a scheduler (the scheduler app is called 24x7, it's a script based scheduler, in this case, it calls the executable strait up, no parameters, etc).
The issue is when it tries to call it, it sees it as a file and tries to "download" it. I've tried running the app from the command prompt, only to get the same results. I've tried everything I can think of, even writing a launchpad type application and using that to call the app, but I get the same results every time.
Am I missing something? Is there a security flag or something I've overlooked? I've never seen this before.
Edit: Sorry, forgot to mention that the app is being called via full UNC path (\myserver\myfolder\myapp.exe)
hmm.... how exactly are you "calling" this application? via SMB? (i.e. by executing the command \\myserver\sharepath\foo.exe from a command line)
If so, your application doesn't run with the same permissions and you can run into code access issues, but that'll come as an error message, rather than a file download.
The fact that you're getting a file download leads me to believe that you're trying to "run" the console application over http, but I don't have enough information to know for sure, hence my question.

Categories