Windows Listening Service

How to write a windows service in C # that listens for tcp connections and processes these connections? The problem is that I want better than "blocking" in the main thread, for example.

while (true);

I can start the listener in another thread, but the main thread needs to be blocked so that the application cannot exit (and the service stops working). Thanks.

+3
source share
3 answers

Why aren't you using the WCF service? - you can host the WCF service in a Windows service ... Then you can use NetTcpBinding (for communication via tcp) so our code will look like

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        internal static ServiceHost myServiceHost = null;

        public Service1()
        {
            InitializeComponent();
        }

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

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

: http://www.pluralsight.com/community/blogs/aaron/archive/2008/12/02/screencast-hosting-wcf-services-in-windows-services.aspx http://msdn.microsoft.com/en-us/library/ms733069.aspx

+5

, , ManualResetEvent, , .

+3

WCF, TcpListner - .

Windows "heartbeat", , . - (, ), . - "".

0

Source: https://habr.com/ru/post/1709024/


All Articles