Search code examples
c#visual-studioservicetopshelf

C# TopShelf Service runs great with debugging; Incredibly slow without debugging


Edit: This is still an issue but the program is not running slow as a service, it is running slow whenever NOT debugging. Running the diagnostics withotu debugging produced the same slowness. Peak cpu usage was 3%. That being said, any help with why this application would run slower WITHOUT debugging would be incredibly helpful as I google down the answer :)

Using C# and nuget'd topshelf, I have made a filewatcher/mover. There is an instance of filesystemwatcher which performs a long set of logic against a text document (between 100 kb > 8 MB). As it reads this text document (on a UNC network location) it will move the lines to one of 2 output files.

While I run the application through Visual Studios 2015 Community in debug mode or release mode, all the file actions and movement are as instant as they can get. Satisfied, I installed the application as a service on the same local machine. I turn on the service, key the trigger, and it starts moving the text files.... 50 kb every couple seconds.

1 MB file is taking a few minutes to parse through when it took a whopping 10 seconds when run through Visual Studios. The service is logged in as me, the same user in Visual Studio.

Any ideas? I have used topshelf for 3-4 other services, all utilizing filesystemwatcher or System.IO.FILE library (using the file library).

I can post some code if needed, just a bunch of if statements as it iterates through each line of text. The key issue being: works great from VS, not as a service. Why??

P.S. Just noticed that it took >4 minutes to move 1.4 mb's all to another file line by line, but went through all the logic after the move within the same SECOND.

I will post the snippet of topshelf configuration I am using, as it is not much:

    static void Main(string[] args)
    {
        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<Service>(serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(name => new ASNWatcher());
                serviceConfigurator.WhenStarted(tc => tc.Start());
                serviceConfigurator.WhenStopped(tc => tc.Stop());
            });
            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDescription("HighRisk ASN Watcher");
            hostConfigurator.SetDisplayName("HighRiskASNWatcher");
            hostConfigurator.SetServiceName("HighRiskASNWatcher");
        });
    }

(yes, it says local system, I changed it post install to a proper user.)

Adding code that does the reading; very lite.

I am adding the piece of code that does the splitting:

     foreach (string line in File.ReadAllLines(fullPath))
            {
                if (line.StartsWith("ASIAUD"))
                {
                    foreach (var field in fileHelper856AUD.ReadString(line))
                    {
                        if (field.TradeID.TrimEnd() != TradeID && field.TradeID.TrimEnd() != CatalogTradeID)
                        {
                            logger.Info("Found thisTrade ID:  " + field.TradeID.TrimEnd());
                            thisorder = true;
                        }
                        else
                        {
                            logger.Info("Found thatTrade ID:  " + field.TradeID.TrimEnd());
                            thisorder = false;
                        }
                    }
                }
                if (thisorder )
                {
                    try
                    {

                        File.AppendAllText(newfile1, line + Environment.NewLine);
                    }
                    catch (Exception e)
                    {
                        //write to log,  also send an email alerting IT.
                    }
                }
                else
                {
                    File.AppendAllText(newfile2, line + Environment.NewLine);
                }
            }
            logger.Info("File Split Complete.  Deleting Original File...");
            File.Delete(fullPath);

Solution

  • I was able to find some hints and get in the right direction through some other Stack articles once I discovered it wasn't the service piece that was hurting it, just running it without the profiler in general.

    Why does my program run way faster when I enable profiling?

    The reason is because when you run your application within Visual Studio, the debugger is attached to it. When you run it using the profiler, the debugger is not attached.

    If you press F5 to run your program, even with the Release build, the debugger is still attached.

    If you try running the .exe by itself, or running the program through the IDE with "Debug > Start Without Debugging" (or just press Ctrl+F5) the application should run as fast as it does with the profiler.