Home > Net >  Why is the .NET Console output on Windows different than output on Linux?
Why is the .NET Console output on Windows different than output on Linux?

Time:06-16

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!

CodePudding user response:

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);
  • Related