How do I use the windows service and WCF to update some record in database?

79 views Asked by At

I wrote a wcf service and then Host wcf in a windows service Using TCP.and then using the command Installutil WindowsServiceSendMail.exe register it on windows 7.

My problem is that the service does not start when i right click on the service1(on the window services on windows 7) and click start. i read this article and I went forward step by step But The result did not receive.

this is RunProgram() method in wcf project in solution

 [OperationContract]
  void RunProgram();

public void RunProgram()
{
  //code for check database
}

this is code in windows service project in solution

protected override void OnStart(string[] args)
{
    if (myServiceHost != null)
    {
        myServiceHost.Close();
    }
    myServiceHost = new ServiceHost(typeof(Service1));
    myServiceHost.Open();
}

protected override void OnStop()
{
    if (myServiceHost != null)
    {
        myServiceHost.Close();
        myServiceHost = null;
    }
}

and then i add a reference from wcf project to windows service project

and then using the command Installutil WindowsServiceSendMail.exe register it on windows 7

you can download project from here

My problem is that the service does not start when i right click on the service1(on the window services on windows 7) and click start.

1

There are 1 answers

1
mahmood kabi On

I've found the problem. I changed the OnStart method.

protected override void OnStart(string[] args)
{
    if (myServiceHost != null)
    {
        myServiceHost.Close();
    }
    myServiceHost = new ServiceHost(typeof(SendMailService));
    myServiceHost.Open();

    WcfSendMail.SendMailServiceWCF s = new WcfSendMail.SendMailServiceWCF();
    s.RunProgram();
}

and publish windows service project and run the setup.exe in publish folder. Of course using the command Installutil WindowsServiceSendMail.exe register it on windows 7.