1

I have a .NET Console application that is consuming a SOAP API. It was written originally in .NET 5 on a Windows computer. It now targets .NET 6 and was published on a Red Hat Linux Server.

When I uploaded and ran the code on Red Hat, I ran into some minor issues related to Windows (EventLog, etc). Not a big deal. However, I am now getting two different outputs when it checks the directory for files.

Expected output and current output in Windows during Debug:

info: FarFetch.Controllers.FileHandlerController[0]
      In directory: C:\Users\user\some\dir\test\IN\
info: FarFetch.Controllers.FileHandlerController[0]
      Files found: 0

However, on Red Hat, I receive the following output:

Please provide a valid file type!
info: FarFetch.Controllers.FileHandlerController[0]
      In directory: /usr/some/dir/path/T_IN/
info: FarFetch.Controllers.FileHandlerController[0]
      Files found: 1

As you can see on the very first line of the output above, it is outputting the default of the switch in the foreach loop before it gets the service on the lines before the loop.

It then goes on and executes the foreach loop again after GetRequiredService has returned.

public static async Task Main(string[] args)
{
        var services = new ServiceCollection();
        ConfigureServices(services);

        //create service provider
        var serviceProvider = services.BuildServiceProvider();

        var fileHandler = serviceProvider.GetRequiredService<IFileHandler>();
        var files = fileHandler.GetAllFiles();

        foreach (var file in files)
        {
            switch (fileHandler.GetFileType(file))
            {
                case "CS":
                    await RetrieveStockInfo(serviceProvider, fileHandler, file);
                    break;
                case "GOBD":
                    await RetrieveOrdersByDate(serviceProvider, fileHandler, file);
                    break;
                case "GOH":
                    await RetrieveOrderHeaders(serviceProvider, fileHandler, file);
                    break;
                case "GOR":
                    await RetrieveOrderRows(serviceProvider, fileHandler, file);
                    break;
                case "PKG":
                    await DecidePackaging(serviceProvider, fileHandler, file);
                    break;
                case "RBS":
                    await RecommendedBoxSize(serviceProvider, fileHandler, file);
                    break;
                default:
                    Console.WriteLine("Please provide a valid file type!");
                    break;
            }
        }

    }

Here is the GetAllFiles implementation:

public IEnumerable<string> GetAllFiles()
    {
        if (Directory.Exists(_filePaths.Value.In))
        {
            _files = ProcessDirectory(_filePaths.Value.In);
        }

        _logger.LogInformation($"Files found: {_files.Count()}");

        return _files;
    }

Process directory:

private IEnumerable<string> ProcessDirectory(string targetDirectory)
    {
        return Directory.EnumerateFiles(targetDirectory, "*.RDY");
    }

And the Get File Type:

public string GetFileType(string filepath)
    {
        string[] fileParts = filepath.Split('\\');
        string fileName = fileParts[fileParts.Length - 1];

        return fileName.Split('_')[0];
    }

Any help would be appreciated. Seems like it skips ahead to the foreach loop when it's getting the FileHandler service. Then returns to normal execution after it gets the required service.

Why does it do this on Linux, but not on Windows?

Thanks in advance!

5
  • Hi! Are the paths some\dir\test\IN` vs some/dir/path/T_IN/` expected? Specifically, test/N vs T_IN? What are the actual contents of /usr/some/dir/path/T_IN/? An output from find /usr/some/dir/path/T_IN/ would be great.
    – omajid
    Jun 15, 2022 at 18:24
  • @omajid I changed the paths during the post, but essentially yes. Those are the paths I expect. My concern is with the output from the foreach loop before the code is in that FileHandler class. Does that make sense? Other than that, it seems to be finding the correct directory and finding files afterwards Jun 15, 2022 at 18:33
  • I think it only appears that way. Part of the code is using Console.WriteLine, and part of the code is using logging. AFAIK logging is buffered and can appear later, while Console.WriteLine is displayed immediately. Maybe consider switching everything to logging (or everything to Console.WriteLine) to rule that out as a source of issues?
    – omajid
    Jun 15, 2022 at 18:37
  • @omajid I understand what you're saying, but that line is coming from the switch statement inside a foreach loop. Why does execution on windows wait until GetRequiredService returns to run the foreach, yet it is going to the foreach loop immediately on Linux? Jun 15, 2022 at 18:45
  • No, the code runs the same way on both platforms. It appears that the foreach loop runs earlier because you see the output from the foreach loop earlier. And that's because it's not buffered. Logging is buffered and logs are printed some time after the text is logged. Try switching everything to logging or Console.WriteLine to rule that out as a source of confusion.
    – omajid
    Jun 15, 2022 at 18:49

1 Answer 1

1

Without more context, the first suspect is this line:

    string[] fileParts = filepath.Split('\\');

That says to split the path using \ character. That's the directory sparator on Windows, but doesn't do anything on Linux (the file separator is / on Linux). You probably want Path.DirectorySeparatorChar here:

    string[] fileParts = filepath.Split(Path.DirectorySeparatorChar);

Or, perhaps just use the built in functions to get the file name directly:

    string fileName = Path.GetFileName(filePath);
2
  • I just saw your other comment and looking at this again, I think you may be right. Let me make some changes and retry this. Jun 15, 2022 at 18:55
  • 1
    That was definitely the issue. There were two files in the directory and so it seemed to output the default value first and then output the buffered log. I changed to logging in the Main function and I now see expected output. Thanks! Jun 15, 2022 at 19:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.