Line breaks with WCF, JSON, and a non-Windows client

I have a .NET WCF service using WebMessageFormat.Json both its ResponseFormat and RequestFormat . The service runs on a Windows server, the client is an Android tablet.

As it turned out, the lines sent from the client to the server contain LF linebreaks ("\ n") instead of CRLF ("\ r \ n"). Since Android is based on Linux, this is not surprising. However, the data is stored in a Windows database and read by Windows clients, so I need CRLF line breaks.

Is there a more elegant way to solve this problem than manually s = s.Replace("\n", "\r\n"); every row received through WCF? Since WCF has so many options and features, I realized that there might be a hidden AutoTranslateNewlines that I missed ...


Additional Information: My service is declared like this:

 [OperationContract()] [WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "MyService?UserId={myUserId}")] public Reply MyService(String myUserId, Request someRequest) { ... } 

where Request is a custom class with multiple String fields, some of which may contain multiple lines of text.

+4
source share
1 answer

You mentioned that you use

 s = s.Replace("\n","\r\n"); 

But there may be a problem when there are several "\r\n" sequences in your string. This will become "\r\r\n" , which will be the problem. To solve this problem, just do the following.

 s = s.Replace("\r\n", "\n"); s = s.Replace("\r", "\n"); s = s.Replace("\n", "\r\n"); 

which should handle all situations, although perhaps not the most productive. For better performance, perhaps you can use regex.

0
source

Source: https://habr.com/ru/post/1502438/


All Articles