locked
Running C# Server as Windows Service RRS feed

  • Question

  • Hello,

    I have created a console application for a Socket based C# server. 

    Now, I want this console application to be run as Windows service. I tried to create a Windows Service for that. But Server does not respond to requests from browsers.

    I read things about Windows Service and my understanding up to now is that Windows service is running in different session than Windows application [For example, Browser in my case] and hence can not respond to it.

    To remove this dependency, I tried to redirect the console interactions to internal stream using properties of ProcessStartInfo class. 

    I can see my service running in Windows Task Manager, but it is not responding to my browser requests.

    I am putting my Code snippet for the service here.

    using System.Diagnostics;
    using System.ServiceProcess;
    
    namespace TestWindowsService
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                Library.WriteErrorLog("Test window service is started");
                string path = @"C:\\Users\\NKR8\\Documents\\Visual Studio 2015\\Projects\\ReqtifyExternalServer\\Server\\bin\\Debug\\Server.exe";
                ProcessStartInfo info = new ProcessStartInfo(path);
                info.UseShellExecute = false;
                info.RedirectStandardError = true;
                info.RedirectStandardInput = true;
                info.RedirectStandardOutput = true;
                info.CreateNoWindow = true;
                info.ErrorDialog = false;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                
                System.Diagnostics.Process.Start(info);
            }
    
            protected override void OnStop()
            {            
                Library.WriteErrorLog("Test window service is stopped");
            }
        }
    }
    

    What is wrong in my code?

    Thank you in advance!

    Navnath

    • Moved by Fei Hu Sunday, December 24, 2017 9:16 AM Web technology related
    Wednesday, December 13, 2017 1:20 PM

All replies

  • I can see my service running in Windows Task Manager, but it is not responding to my browser requests.

    And it never will. You need to  be using a WCF service hosted by Windows service with a Web browser solution such as a ASP.NET Web form solution as a client to the WCF service.

    https://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF

    Wednesday, December 13, 2017 3:03 PM
  • Your browser is either executing client-side or server-side code (or maybe a bit of both). If it is running server-side code, e.g. an app running in IIS, then DA924x would be correct - you need a web service instead of a Windows Service or console app.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, December 13, 2017 4:20 PM
  • Your browser is either executing client-side or server-side code (or maybe a bit of both). If it is running server-side code, e.g. an app running in IIS, then DA924x would be correct - you need a web service instead of a Windows Service or console app.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    There is WCF over Named Pipes, MSMQ and TCP/IP that can be hosted by a Windows service where a browser based solution such as JavaScript or ASP.NET  web form solution can communicate with the WCF service hosted by a Windows service.
    Wednesday, December 13, 2017 4:59 PM