C# windows service servicebase
These parameters let us choose where the Events from our logs will be stored. Last, but not least, we are initializing this host as a Windows Service.
This last step is as easy as calling the UseWindowsService method, which configures the host to use the logging properties we just set, as well as setting up automatic logging for the Windows Service lifetime events. Since this new code will listen to new files in the directory we specify, we need a way to filter the file extensions we want to allow processing.
We could hard-code this, but lets simply add a parameter to our CommandLineOptions class, which will easily handle all the parsing for us:. See how easy it is to support our list of extensions? Since these are the most commons ones, we support png , jpg , and jpeg , and all the classes we use to load our image handle these extensions, so we are safe with them.
Now that you understand the structure of our Program. This is the class that will run our WinML code. The ExecuteAsync method is going to be called automatically for us, so we need a mechanism to keep it from returning, to keep our process from ending.
There are many ways to achieve that, specially since we need to return a Task. This should be at the very end of our ExecuteAsync method.
The stoppingToken is passed down to our ExecuteAsync method, and it is an instance of CancellationToken which will be cancelled when our service is requested to stop. Remember that this code is headless no user interface at all , so we need a way to know what our code is executing, and that is done through logs.
At the start of our method, we should load our WinML model, pretty much the same way we were doing before:. This creates a FileSystemWatcher instance that will hook up to some Windows events for us, and automatically raise the Created event whenever there is a new file created on the folder we are watching.
Notice that we are adding Filters to it, which are mapping to the extension filters coming from our new command line argument. We are waiting milliseconds 1 second , before we process the file. The FileSystemWatcher is attached to the Windows events, and is so fast that it will raise the Created event even before the file is closed by whichever process is still creating it.
We could have created a retry logic if we caught this specific exception but, again, this solution is a simplification, and will keep our code simple for this sample.
The ProcessFileAsync method expects a full path to an image file and a confidence float, just like we had before.
Notice that the folderName argument passed in our MoveFileToFolder method is the label with highest confidence, returned by our WinML classification model for that one specific image.
Therefore, we are moving that file inside a folder with its category name. The whole process is so fast that if you copy and paste an image file into the folder we are watching, it barely stays inside the folder, almost instantaneously being moved to a newly or existing folder. The next step is to 1 deploy this somewhere, 2 register our Windows Service, and 3 start it! The deployment step is very straight forward.
This will let you select where you want to publish it, and since this is a simple. NET5 console application, we can choose between a variety of places. Lets select a simple Folder.
On the next step, just select Folder again and click Next. On the last step, we need to select a path.
Click on Finish to create the publish profile, and then on Publish to build our code and deploy it! You will see that all the files that we need are properly deployed in that folder, so we can now configure our service! Another way to publish your project is using the dotnet publish command. Windows Services are managed through a tool called Service Controller , a. The original generated Main is like the one below. ServiceBase[] ServicesToRun;. Run ServicesToRun ;. To debug without installing, add the code as follow, mainly if!
You will be able to step through the code when you run the Windows Service without installing. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email.
This site uses Akismet to reduce spam. Learn how your comment data is processed. Chanmingman's Blog. Skip to content. Step 1. The result look like this. The Wizard adds WebService1. The default code of WebService1. ComponentModel System. Configuration System. Next Recommended Reading. Net Core 6. Create A. Understanding Thread Starvation in. NET Core Applications. Exploring Numeric Streams In Java.
What Is The Metaverse.