Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open <MyService> service on computer '.'

I have a website created by C# which is to start a services in a server.

I created a services called MyService using this :

instsrv MyService %systemroot%\system32\srvany.exe

then I uses the following code to call it :

ServiceController service = new ServiceController("MyService");
try
{
    service.Start();
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

but when I access the website and trigger this event, it prompt me

Cannot open <MyService> service on computer '.'

Is it because of the security or permission problem?? Any guide with clear steps is highly appreciated.

like image 640
NewBirth Avatar asked Jun 23 '11 11:06

NewBirth


People also ask

How do I start a server service?

Start a service from Server Explorer Expand the Services node, and then locate the service you want to start. Right-click the name of the service, and then select Start.

How do I enable Windows Server service?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to enable a service and press Enter: sc config "SERVICE-NAME" start=auto In the command, replace "SERVICE-NAME" for the name of the service that you want to enable.


2 Answers

with iis (version 6 and higher), web apps are run using an application pool. EAch application pool can run with a specific user (the identity), which is, by default, NETWORK SERVICE. It's a very bad idea to add this user to local administrators.

Possible ways :

  • Grants the necessary privilege to start/stop the service to this user, setup an dedicated application pool to use this identity and ensure the account have read access to the asp/.net application path.
  • create a dedicated account too. within you application, connect to the service management using a username/password you store securely in the config (crypt the section). I'm not sure if you can specify a user/password with service control, but you can at least, use WMI or Process.Start with a net stop / net start command.
like image 167
Steve B Avatar answered Oct 21 '22 03:10

Steve B


I have actually solve the problem of on top scenario.

  • The service in this case is "MyService" need to set the log on to Local System Account and check Allow service to interact with desktop
  • In the IIS Manager, create a Application Pool for the particular website. And load the website in to it.
  • The particular Application Pool Identitys need to predefined to Local Syatem

This is what is needed in the IIS and services.msc

On the other hand, there will still be errors about the MAC address.
So, we need a which can be generate by machineKey Generator.
The machineKey generated is needed to paste at the tag below.

Then copy the whole block of the quote below and paste it to the Web.config

<pages buffer="false"
enableViewState="false"
enableSessionState="false"
enableViewStateMac="false"
validateRequest="false"
enableEventValidation="false"
viewStateEncryptionMode ="Never"/>

<machineKey 
validationKey="8FCCB821044C5174C551129400F278B5629C64EAC195046DA5FE608EF418913C1139636E13FE  9F8C35AD3C10C394CAC1D9CEBB8B6BDA018165E91136B1AADBE1"
decryptionKey="3F260119F4CC0A0DD8C6B4979C70644A0ED2DD19B3ADB00F85FB7C83634FC544"
validation="SHA1"
decryption="AES"
/>

And the problem will then be solved, maybe the situation you guys faced may be different but I hope it may help someone as I struggle in this quite a long time.

In addition, I uses this code so there will no conflict when start and stop the services created.

if (service.Status == ServiceControllerStatus.Stopped)
{
    try
    {
        service.Start();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}
else
{
    try
    {
        service.Stop();
        service.Start();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

Good Luck and thank you guys for helping.

ref: http://www.csharp-examples.net/restart-windows-service/
http://forums.asp.net/t/906759.aspx/3/10

like image 42
2 revs, 2 users 79% Avatar answered Oct 21 '22 03:10

2 revs, 2 users 79%