NewBirth NewBirth - 1 month ago 8
C# Question

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.

Answer

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

These is what needed in the IIS and services.msc

In the other hand, there will still be error 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 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 solve, 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 thanks you guys for helping.

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