How can i start windows service automatically - c#

i try to develop a windows service. My win service must run if windows 7 stars up. How can i do?

Change the startup type of the service to Automatic.
You should create an installer application for your service (if you haven't already) and you can set this option in there so when its installed it is pre-configured this way. See Walkthrough: Creating a Windows Service Application in the Component Designer

Set the startup type to "Automatic".

You set service startup to "Automatic" in service control manager. Not a programming question, by the way.

Actually make sure your installer sets the startup type on Automatic ;) Add dependencies as needed so that you start after dependent services.

If you want to set this programmatically (i.e. during the development phase) then this is set in the properties for the Installer (StartType), which you will also need.

You could configure the service through the command line using the "SC" utility provided by Windows; it comes with XP and later.
sc create MyServiceName binPath= <path
to service exe file> start= auto
DisplayName= MyServiceDisplayName
sc description MyServiceName "This is
my service's description"
The spaces after arguments (like binPath= ) are important.

Related

Change the Description of my Windows Service

According to this answer it seems that there is no official way to set a version for a Windows Service. However this can be done by inserting it into its Description or DisplayName.
I would like to be able to change that version number without needing to delete and reinstall the service. But I couldn't find a way to set the Description except for in the installation itself.
So, is there a way, and what is it, to change a Service's Description without reinstalling it?
Preferably using .Net. The Service itself is also .Net if that matters.
This can be accomplished using the SC.exe utility with the command:
sc description <ServiceName> "Any Description you like."
This command could be called from a command window opened as administrator or from a .Net application provided that the service has already been created.
Though this is not a pure .NET solution, it can be implemented in .NET, and it is one of the only MS-supported methods of reconfiguring a service. Plus, it doesn't require direct registry manipulation (best avoided if possible).
You can change the description of a Windows service by using the Windows command-line service controller utility, SC.exe.
You can exec the command you need to execute from your .NET code, or call it from a shell or script, such as CMD.exe or PowerShell.
sc.exe config YourServiceName displayName= "Your service description..."
Note:
Detailed information on the SC config command can be found here: MS Docs SC Config man page
YourServiceName is the actual service name of your application, not it's current DisplayName (unless, of course, they're identical)
If your DisplayName is more than one word, it needs to be wrapped in quotes
There must be no space between the word "displayName" and the equals sign
There must be one or more spaces between equals sign and the beginning of your desired service description
If what you need is the version number of the executable for the Windows service, and the executable is a .NET assembly, then retrieve the path to the service executable, and then retrieve the version from that executable/assembly.

windows start service failure on logging as different user?

I've logged into the visual studio as a different user while debugging the project encountered with error as:
"Windows service start failure"
But when I run with my own credentials working fine. I've tried by giving admin permissions to the particular user on the service but the result is same as below.
Can anyone help me on this?.
click here to see the issue
Ensure your service has a 'ServiceProcessInstaller' (right-click designer-surface of your service.cs and select 'Add Installer'), then change the account property for the service process installer to whatever suits your needs best (Network Service, Local System or Local Service). You may also need to specify the startup type of the service installer class.
To install it do the following steps:
Run Visual Studio Command Prompt as administrator.
Change the directory to the output directory of the service (where the executable file is located) - 'cd C:\directory'
Perform the following command 'installutil service_name.exe'
The service should then startup based on the startup type you specified. You may need to start it manually.

Windows Service properties

How to change the the service name for my windows service in Visual Studio 2012. I'm just new to the windows service, and trying to figure out how to build it.
I've googled and read a a lot about Windows services and how to build them. Everywhere they mention:
In the Properties window for Service1.cs [Design] or Service1.vb [Design], set the ServiceName and the (Name) property for Service1 to MyNewService, if it's not already set.
But for me I can't seem to find where to change the servicename. I rightclicked on service1.cs and and went to properties. Here is a screenshot
It doesn't show me the ServiceName.
You're looking at the properties of the CS file, not the service object itself.
DOuble click it to open in the designer, then you will get actual properties of the service, including ServiceName
It depends on how you install the service. If you use sc.exe you will specify name as command parameter in installation. If you use install util, you can specify name in code.
Also look at this

How To Run And Test a Windows Services In VS

I created a windows service in VS:File->New Project->C#->Windows Services
But I don't know how to run and test it.VS says I have to install my windows services to run it.but I Just want to test it and I don't want to install it.Can anybody Help me???
To debug your Windows service, add the following in your Main():
if (!Environment.UserInteractive) {
// We are not in debug mode, startup as service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyServer() };
ServiceBase.Run(ServicesToRun);
} else {
// We are in debug mode, startup as application
MyServer service = new MyServer();
service.StartService();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
And the following method in your class MyServer:
public void StartService() {
this.OnStart(new string[0]);
}
Now hit F5 to debug, as any winform or console application.
When creating a Windows Service I usually put all functionality into a separate class library... for testing I create a "normal app" which makes use of the class library...
AFTER the functionality is tested and the bugs are gone I do build/install the Windows Service... there can be problems specific to Windows Service like permissions etc. - these need to be addressed accordingly (some logging is usually very helpful).
Debugging a Windows Service with VS is a bit different from debugging an application - for details see http://msdn.microsoft.com/en-us/library/7a50syb3.aspx
A windows service is not really a normal executable, it builds as exe but has to be installed and started with Services icon from Control Panel.
Once you got it installed and started you can use Visual Studio Debug menu, Attach to process and attach to the service to debug it, not the simplest and fastest way to debug because if you need to change anything you have to build it again and install it again, start and attach again.
Usually a nice approach is to anyway split and isolate the logic of the service in a class library (probably the service business logic) which can be used also from let's say a console application.
At this point you create a test console application which calls some methods and behaves kind of like the service from the Main method and you test and debug this one.
Once everything has been tested and verified and you are satisfied with the results you copy the code you have put in the Main method of the test console application in the Service class, probably in the OnStart method or similar, details depend on your specific design.
we do this also to debug and test WCF services which are hosted in a test console app during development and in a Windows Service in production.
you should use visual studio command prompt with the following command:
installutil "the path of the exe"
and the go to Services (start-> run-> services.msc) you will find the service. click on start and you're done.
P.S: to debug your service in vs: tools-> attach to process and find your service in the list)
You cannot debug service directly from VS. Need to attach the debugger to Windows Service.
To do it, in VS do following:
From the Debug menu, select Attach To Process…
Near the bottom of the window, select Show processes in all sessions.
Left-click the compiled EXE to be run as the Windows Service in the list. Press Attach.
Result: you can place breakpoints at places in your functions, and you'll be able to debug the service execution.

Installed Win Service not displaying in Service Manager

I have a Windows Service created in c#.
It's relatively simple compared to some of the other ones that I've worked on.
I built a setup project to install it for some testing.
I added the primary output from the service project and all the dependencies were added correctly.
I went to View > Custom Actions and added my Primary output to Install, Commit, Rollback, and Uninstall.
The project built and I right clicked the project and clicked Install.
The installation came back successful, I can view the service on the control panel under Add/Remove programs, but when I go into the Service Manager... nothing...
Can anyone provide some insite or anything else that may cause a successfuly installed service to NOT display on the Service Manager.
Forgive me if this goes without saying, but you haven't mentioned what code you are executing in your custom actions. Your service assembly must have a class which derives from System.Configuration.Install.Installer, and that class must have the [RunInstaller(true)] attribute on it. Within that class, you will need to create an instance of System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller, set the appropriate parameters on those instances, and add them to the Installers collection. The ServiceInstaller and ServiceProcessInstaller MSDN pages have a very basic example, but it should be enough to get you there if this is what is needed.
Make sure you had provided some value in "Display name" property.
Use the following command "sc query <service_name>" from command prompt to see whether your windows service got properly installed. If you are not sure on the service name use the following command "sc query state= all >c:\ServicesList.txt" after executing this command search the ServicesList.txt in your C:\ drive.
If that too doesn't work try looking for the service name in registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
You said you added your primary output to Install, etc. But did you create an Installer derived class to do the actual installing of the windows service? I'm not talking the setup project itself, but in your project there should be an installer class that actually does the service install for you.
I had a post on my blog about creating a framework for easy installable services, it has examples on creating the isntaler class.
http://blackrabbitcoder.net/archive/2010/10/07/c-windows-services-2-of-2-self-installing-windows-service-template.aspx
In my case, solution of the problem was simple, i forgot to add access modifier 'Public' to the class. After adding access modifier, service is visible now in services list.

Categories

Resources