2

I searched foe the solution but could not get it. Here is the code for windows service.

 protected override void OnStart(string[] args)
    {
        Debugger.Launch();
        try {
           AsynchronousSocketListener.StartListening();



            // Log an event to indicate successful start.

            EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);

        }
        catch(Exception ex)
        {
            // Log the exception.
            EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);


        }
    }

Here is the class AsynchronousSocketListner

 static string constr = "Integrated Security=SSPI;Persist Security Info=False;Data Source=WIN-OTVR1M4I567;Initial Catalog=CresijCam";

    //string test = constr;

    // Thread signal.  
    public static  ManualResetEvent allDone = new ManualResetEvent(false);

    private  AsynchronousSocketListener()
    {

    }
    public static void StartListening()
    {
        // Establish the local endpoint for the socket.  
        // The DNS name of the computer  

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1200);

        // Create a TCP/IP socket.  
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.  
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(200);

            while (true)
            {
                // Set the event to nonsignaled state.  
                 allDone.Reset();

                // Start an asynchronous socket to listen for connections.  

                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing.  
                 allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            string me = e.Message;

        }

    }

I am getting different Error messages everytime:

A timeout (30000 milliseconds) was reached while waiting for a transaction response from the TCPService service.

Service cannot be started. The service process could not connect to the service controller

I dont know from where is the error that I am getting is coming. I know one thing that service is not run yet. and It is in this method startListening(). I debugged using Debugger.launch(). But I am not getting to a specific line . I also think this is related to TCP somewhere but nothing for sure.

The same code is in working state for console Project. I dont know what other code to put here. But please let me know if needed further detail.

Usha phulwani
  • 184
  • 4
  • 22
  • "allDone.WaitOne();" - Why are you using async if you block anyway. If you don't want things to go async, use blocking APIs ... But that's exactly the problem here: You are blocking the OnStart Method. StartListening should be on a separate Thread, completely. – Fildor Sep 04 '18 at 07:13
  • Please don't edit questions like this after you've already received answers. It was only by looking in the edit history that I could make sense of the fact that there was no threading code in the original, by which I can make sense of TheGeneral's answer. I'd suggest rolling back to version 1, and then possible editing to *add* what you've updated your code to be, but notice that your current title and most of the narrative *don't* match the code. – Damien_The_Unbeliever Sep 04 '18 at 11:45
  • @Damien_The_Unbeliever... I will get it to initial posts... – Usha phulwani Sep 04 '18 at 13:03
  • Other reasons for the error discussed in https://stackoverflow.com/questions/4635231/the-service-process-could-not-connect-to-the-service-controller – Michael Freidgeim Aug 16 '19 at 23:34

1 Answers1

1

This simple answer is your AsynchronousSocketListener is not Asynchronous or threaded or anything of the sort. Essentially your service Start is timing out, and will never hit

EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);

Because it is essentially blocking forever


This error says it all

A timeout (30000 milliseconds) was reached while waiting for a transaction response from the TCPService service.


OnStart should only start the work. This typically means spawning a new thread to do the actual work. In short, it is expected that OnStart completes promptly.

You will need to refactor your code to run your AsynchronousSocketListener in a new thread or task

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I Will try this and let you know – Usha phulwani Sep 04 '18 at 07:46
  • I have added the code for task start. But then also not working. May be I am doing it wrong. Please review. The updation is in the problem above – Usha phulwani Sep 04 '18 at 10:44
  • @Ushaphulwani are you still getting the same error? – TheGeneral Sep 04 '18 at 10:45
  • I am not getting any error... The process is not throwing any exception at all. But no task is done. I tried to pause the debug process in between after waiting for some time and it said next step to be done is **ServiceBase.Run(ServicesToRun)**. That only means that service is not started yet... Am I right? – Usha phulwani Sep 04 '18 at 10:52
  • @Ushaphulwani no, maybe you should test this in a console app first, also maybe you should put in some logging and see where you are getting up to. i mean its hard to know whats going on from here. – TheGeneral Sep 04 '18 at 10:54
  • There is no information in eventlog also for anything..its like for Eventlog nothing's happened at all. – Usha phulwani Sep 04 '18 at 10:54
  • console app is working very fine.. there was no issue at all.. Then only I shifted to service. – Usha phulwani Sep 04 '18 at 10:55
  • @Ushaphulwani yeah no event log seems suspicious, as there should be obvious logging there – TheGeneral Sep 04 '18 at 10:55
  • Let me try something else as I understand you can not help me without any strong clue... – Usha phulwani Sep 04 '18 at 10:56
  • After adding task()... console application is also exited without any error – Usha phulwani Sep 04 '18 at 11:04
  • @Ushaphulwani put a readline after the tasks starts in the console app, so the app doesnt exit – TheGeneral Sep 04 '18 at 11:07
  • I added the read()... The debugger went into StartListening() but It got out after Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);.. It didnt went ahead and didnt waited for another read to close. – Usha phulwani Sep 04 '18 at 11:14
  • I called the method directly just to check if code has any error.. But without any thread or task its working in console application... Is the problem because of ManualResetEvent thread? – Usha phulwani Sep 04 '18 at 11:25
  • @Ushaphulwani maybe, its hard to tell, i dont have time to test this at the moment, but tommorrow ill have a look and create a simple project to test this out for you – TheGeneral Sep 04 '18 at 11:41
  • thank you so much.... I will wait for you test results and I will try from my side also... – Usha phulwani Sep 04 '18 at 13:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179423/discussion-between-usha-phulwani-and-thegeneral). – Usha phulwani Sep 05 '18 at 03:22