Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to run a windows service exe path in asp.net code behind with c#? I am getting service not found issue.

Following is my code

C#
private void StartService(string serviceName, int timeoutMilliseconds)
    {
        Configuration objConfigFile;
        //getting an instance of the configuration file
        objConfigFile = WebConfigurationManager.OpenWebConfiguration(
                               HttpContext.Current.Request.ApplicationPath);

        //getting an instance of the "identity" section of the cofiguration file
        IdentitySection objIdentitySection =
          (IdentitySection)objConfigFile.GetSection("system.web/identity");
        //Checking the domain,username and password credentails by Alex on Jan 8,2014
        if (objIdentitySection != null)
        {
            string username = objIdentitySection.UserName;
            string password = objIdentitySection.Password;
            bool impersonate = objIdentitySection.Impersonate;
            //Configuration currentConfiguration = objIdentitySection.C.CurrentConfiguration;

          
            if (impersonate == true)
            {
              
                ServiceController service = new ServiceController(@"D:\C3sps  Project\COMMON\Source\Untested\C3psps\C3PSPS_SOD\bin\Debug\C3PSPS_SOD.exe");
                
               // ServiceController service = new ServiceController("C3PSPS_Service");
                
                try
                {

                    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                   
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    return;
                    if (ddlServices.SelectedItem.Value == "EOD")
                    {
                        EOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "EODSalaryAdvance")
                    {
                          SA_EOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "SOD")
                    {
                        SOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "SODCBI")
                    {
                        SOD_CBI objSOD_CBI = new SOD_CBI();
                        objSOD_CBI.SaveSODCBIServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "SODSalaryAdvance")
                    {
                        SA_SOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "SOM")
                    {
                       // EOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "DebitBalance")
                    {
                       // EOD.SaveServiceStatus(1);
                    }
                    if (ddlServices.SelectedItem.Value == "EmailService")
                    {
                        C3PSPSBLL.EmailService.SaveServiceStatus(1);
                    }
                   
                }


                catch (Exception ex)
                {
                    throw (ex);
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please Provide Correct Credentials');", true);
            }
        }


    }
Posted
Updated 9-Jan-14 2:32am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jan-14 13:46pm    
First of all, will you explain how it possibly can make any sense? Do you want to start a service with ASP.NET application? Why?!
—SA
Member 10488324 10-Jan-14 1:27am    
Its a client requirement.
Maria Norbert 20-May-14 2:44am    
I need to run a two Functions(Stored Procedures) at a Same time.,

is this good to run with Windows Services??

Is any other alternative means please let me know.???

I tried Threading, but i dont know how to proceed with that., pls do the needful.

for ur reference
http://www.codeproject.com/Answers/774079/How-to-Fill-a-GridView-by-Threading-with-two-datas#answer1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900