using XSupport.InitInterop in console/windows services app

84 views
Skip to first unread message

liviu....@gmail.com

unread,
Jun 16, 2024, 2:28:52 PMJun 16
to Softone Developers Network
Hello all!
I need to build a console application that runs in windows service mode as well. I am using Topshelf for this. The app runs ok in console mode, but when I start the service it cannot pass the line with XSupport.InitInterop(), where it freezes with no error, so the start process is not finishing. 
Can you please let me know if there is any trick that I should use to make this run in service mode?
Thank you,
Liviu

vezirgiannis sa

unread,
Jun 17, 2024, 7:37:31 AMJun 17
to Softone Developers Network
Hello,

First of all I would strongly suggest using a windows service project and not a third party library (from a quick look on github it hasn't been maintained for years).

That being said, I can't reproduce your findings.

I created a simple console application as https://www.c-sharpcorner.com/article/creating-windows-service-in-net-with-topshelf/ and it seems to be working normally (it inserts a new document on service start).

Maybe your problem is caused by the service privileges ? Maybe check other types and not Local System ?
-----------------------------------------------------------------------------------------------------------------------------------
internal class Program
{
    static void Main(string[] args)
    {
        ConfigureService.Configure();
    }
}

-------------------------------------------------------------------------------------------------------------------------------------
 public class MyService
 {
     XSupport Prg;

     public void Start()
     {
         try
         {
             XSupport.InitInterop(0, @"C:\Soft2\xdll.dll");

             if (Prg == null || Prg.Handle == 0)
                 Prg = XSupport.Login(@"C:\Soft2\web.xco", "***", "***", 1001, 1000, DateTime.Now);

             using (var salDoc = Prg.CreateModule("SALDOC"))
             {
                 using (var finDoc = salDoc.GetTable("FINDOC"))
                 {
                     using (var iteLines = salDoc.GetTable("ITELINES"))
                     {
                         salDoc.InsertData();

                         finDoc.Current["SERIES"] = 7002;

                         finDoc.Current["TRDR"] = 3382;

                         iteLines.Current.Append();

                         iteLines.Current["MTRL"] = 149043;

                         iteLines.Current["QTY1"] = 1d;

                         salDoc.PostData();
                     }
                 }
             }
         }
         catch { }
     }

     public void Stop()
     {

     }
 }

-----------------------------------------------------------------------------------------------

internal static class ConfigureService
{
    internal static void Configure()
    {
        HostFactory.Run(configure =>
        {
            configure.Service<MyService>(service =>
            {
                service.ConstructUsing(s => new MyService());
                service.WhenStarted(s => s.Start());
                service.WhenStopped(s => s.Stop());
            });

            configure.RunAsLocalSystem();
            configure.StartManually();
            configure.EnableShutdown();
            configure.SetServiceName("TestService");
            configure.SetDisplayName("TestService");
        });
    }
}

Best regards,
Zeikos Christos
--

antwn...@gmail.com

unread,
Jun 17, 2024, 8:52:52 AMJun 17
to Softone Developers Network
Hello! 

I've never used Topshelf, but may I recommend alternatives such as 1) winsw/winsw: A wrapper executable...  2) NSSM - the Non-Sucking Service Manager

Hope one of them fixes your problem!

Liviu Buligan

unread,
Jun 18, 2024, 10:29:01 AMJun 18
to so...@googlegroups.com
Hi and thank you very much for your answer.
I adapted your code to my project but I did not have any luck.
I added a debugger to follow the execution during the service start routine.
It hung on the XSupport.InitInterop, not processing the next step nor raising an exception
:(
I'm afraid I had to pass the service and run the command from a Windows scheduler task. Not exactly what I want, but will still do the job.
 
public void Start()
        {
            try
            {
                _ = System.Diagnostics.Debugger.Launch();
                XSupport.InitInterop(0, @"d:\Softone\LastVersion\xdll.dll");
                while (true) {
                    ProcessS1();
                    Thread.Sleep(1000);
                }
               
            }
            catch (Exception ex) {
                String aa = ex.Message;
            }
        }


--
Softone Developers Network group.
To post to this group, send email to so...@googlegroups.com
---
Λάβατε αυτό το μήνυμα επειδή έχετε εγγραφεί στην ομάδα "Softone Developers Network" των Ομάδων Google.
Για να απεγγραφείτε απ' αυτή την ομάδα και να σταματήσετε να λαμβάνετε μηνύματα ηλεκτρονικού ταχυδρομείου απ' αυτή, στείλτε ένα μήνυμα ηλεκτρονικού ταχυδρομείου στη διεύθυνση soft1+un...@googlegroups.com.
Για να κάνετε προβολή αυτής της συζήτησης στον ιστό, επισκεφτείτε τη διεύθυνση https://groups.google.com/d/msgid/soft1/207ccccf-29b0-4a2b-bd79-6eda5b01ddf0n%40googlegroups.com.


--
Liviu Buligan

Liviu Buligan

unread,
Jun 18, 2024, 10:33:33 AMJun 18
to so...@googlegroups.com
Well I was creating also a standard  windows service project  but it looks that I get the same problem. As far as your suggestions... I never used them I. It will be quite time consuming and the deadline is close. I have the alternative to let it run as a console app and schedule it to execute periodically. Still remains a mystery why the same code that produced a functional service some time ago now it presents same problems... :((

--
Softone Developers Network group.
To post to this group, send email to so...@googlegroups.com
---
Λάβατε αυτό το μήνυμα επειδή έχετε εγγραφεί στην ομάδα "Softone Developers Network" των Ομάδων Google.
Για να απεγγραφείτε απ' αυτή την ομάδα και να σταματήσετε να λαμβάνετε μηνύματα ηλεκτρονικού ταχυδρομείου απ' αυτή, στείλτε ένα μήνυμα ηλεκτρονικού ταχυδρομείου στη διεύθυνση soft1+un...@googlegroups.com.
Για να κάνετε προβολή αυτής της συζήτησης στον ιστό, επισκεφτείτε τη διεύθυνση https://groups.google.com/d/msgid/soft1/b0db696c-7200-4a58-a658-8ada79172506n%40googlegroups.com.


--
Liviu Buligan

vezirgiannis sa

unread,
Jun 18, 2024, 1:07:50 PMJun 18
to Softone Developers Network
Glad to see you found a workaround.

But a couple of suggestions on the code you provided for future reference:

You are creating an endless loop in the Start() method that is actually called on the OnStart() method of the service.
This is bad because you are not allowing the method to complete and signal the completion to the OS. This maybe why, like you wrote in your initial post, causes the syptom "the start process is not finishing".
And also you are using the Thread.Sleep() method, tying a worker thread to the loop just doing nothing.

Normally on the OnStart() method you should initialize a timer (system.timers.timer) which is actually indented to be used in a multi-threaded environment,
and implement it's elapsed event to do the work you want.

Now if you, for some reason, don't want to use a timer, the other solution would be to call from your start method an async method using Task.Run without awaiting it (fire and forget thus allowing the completion of the calling method) in which you would create an endless loop and inserting the delay you want with await Task.Delay().

Anyway, wish you luck.

Best regards,
Christos Zeikos

Liviu Buligan

unread,
Jun 19, 2024, 5:26:51 AMJun 19
to so...@googlegroups.com
Thanks for the reply. 
It used to be a timer, then I changed to a loop while trying to see why is not working. Anyway, this is a dead end for me, unfortunately, at least for now. I hope to find some free time to stay longer on the subject with a more relaxed approach.
Your suggestion with the task seems good,, it is to be tried.

Thank you again!
Liviu


Για να κάνετε προβολή αυτής της συζήτησης στον ιστό, επισκεφτείτε τη διεύθυνση https://groups.google.com/d/msgid/soft1/21618e42-f773-4fb9-acad-bd1ce772da8en%40googlegroups.com.


--
Liviu Buligan
Reply all
Reply to author
Forward
0 new messages