Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've recently migrated a web app from a Windows Azure App Service to a Linux one.
I'm now running into a problem that I had not anticipated.
Since I'm new to running on Linux I thought I'd ask here.

So basically the web app allows users to upload a text file and the web app then sends the text to a web service running on Windows for further processing.

Here's the gist of it:
C#
string content = await IFormFile.ReadAsStringAsync();

// Some code here, client is an HttpClient.

string json = JsonConvert.SerializeObject(new { content, something, somethingElse });
var httpcontent = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
using HttpResponseMessage response = await client.PostAsync("MyEndpoint", httpcontent).ConfigureAwait(false);

// Later in MyEndpoint...
var lines = content.Split(Environment.NewLine);
I'd expected Split(Environment.NewLine) would return hundreds of lines, but I'm getting only one when I call the endpoint from the Linux web app.
My guess is that Linux sends the new lines as \n while Windows uses \r\n.

I'd had hoped the JSON would serialize to \n and then the service on Windows would deserialize it as \r\n.

I've probably used plenty services running on Linux and I've never had to worry about my new lines.
I don't want to know what my service runs on and adjust my new lines accordingly.
What am I doing wrong?

The web app uses .NET8 and the web service uses .NET6. Both are running in Azure Web Apps.

What I have tried:

Plenty of logging to find the problem.
I know of workarounds, like splitting in the Linux app and then sending the lines to Windows, or splitting on \n instead of Environment.NewLine.
Did some googling, but couldn't find an actual answer to my problem.
Posted
Comments
PIEBALDconsult 28-Feb-24 10:01am    
I wrote my own tokenizer for that. I wouldn't use Split except in very simple cases.

1 solution

Since you're using .NET 6, you could try using the MemoryExtensions.EnumerateLines method[^] method, which should handle both Windows and Linux newlines for you.
C#
List<string> lines = [];
foreach (ReadOnlySpan<char> line in content.AsSpan().EnumerateLines())
{
    lines.Add(line.ToString());
    // Or process the line here without creating a string, to avoid the allocation...
}

Or you could try the String.ReplaceLineEndings method[^], although that would allocate a new string.

Quote:
The list of recognized newline sequences is CR (U+000D), LF (U+000A), CRLF (U+000D U+000A), NEL (U+0085), LS (U+2028), FF (U+000C), and PS (U+2029). This list is given by the Unicode Standard, Sec. 5.8, Recommendation R4 and Table 5-2.
 
Share this answer
 
Comments
Sander Rossel 28-Feb-24 11:41am    
Thanks, this works! Already changed my split to \n or \r\n\, but this solution is cleaner.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900