1

I am using .net 6.0 minimal api with the below simple code in c#

app.MapGet("/", () =>
{
    return $"{{\"error\":\"0\", \"message\":\"\", \"rows\":\"0\", \"data\":\"It is working !\"}}";
});

My client side code in c# is as below

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
//
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri($"{(Properties.Settings.Default.use_https ? "https://" : "http://")}{Properties.Settings.Default.server_address}:Properties.Settings.Default.server_port}/");
HttpResponseMessage response = await httpClient.GetAsync("");

the response.Content is

{System.Net.Http.StreamContent}
    Headers: {Content-Type: text/plain; charset=utf-8
}
    IsBuffered: true
    bufferSize: 4096
    bufferedContent: {System.Net.Http.HttpContent.LimitMemoryStream}
    canCalculateLength: false
    content: {System.Net.Http.HttpClientHandler.WebExceptionWrapperStream}
    contentConsumed: true
    contentReadStream: null
    disposed: false
    headers: {Content-Type: text/plain; charset=utf-8
}
    start: 0

With postman I am getting a proper result i.e. {"error":"0", "message":"", "rows":"0", "data":"It is working !"} from get of http://localhost:5400/

Maybe it is something to do with 'content: {System.Net.Http.HttpClientHandler.WebExceptionWrapperStream}'? How do I resolve this?

jps
  • 20,041
  • 15
  • 75
  • 79
Srikanth S
  • 1,717
  • 5
  • 16
  • 21
  • 1
    Does the provided answer help? Also, it looks like you are getting an exception back (`WebExceptionWrapperStream`). Are there any error details in the exception object? Is there an inner exception with more info? Do you have other calls to `GetAsync` that are working and only this one isn't working? – Tawab Wakil Oct 07 '22 at 15:07
  • 2
    Hi Tawab, It was same with both get and put methods. The changes suggested by Mr. Hassan did help me resolve this; But I am still to check if that is compatible with my mobile clients (flutter apps) without major modifications or it breaks. – Srikanth S Oct 07 '22 at 16:13

1 Answers1

1

You are calling it using await GetAsync() method, try to convert your lambda to an async method like this:

app.MapGet("/",async Task<string> () =>
{
    return await Task.FromResult("Your response");
});
Hassan Monjezi
  • 1,060
  • 1
  • 8
  • 24