C # Flurl and HttpClient, no response from REST API

I had a problem getting a response from the API with my code, the request does not expire, and it does not give me an answer at all. I made an api endpoint in my own API to return a json string to manually publish json data with the "Firefox Poster" and this works fine. With this, I believe the problem is somewhere in my code.

I got C # WebAPI, which I am developing for use with the Angular interface (this works, this is just for the story). When calling my API, I create an "EnrollmentRequestDto" object

public class EnrollmentRequestDto { /// <summary> /// Context Information for the request. Contains the Ship-To, language code and timezone. /// </summary> [JsonProperty("requestContext")] public RequestContextDto RequestContext { get; set; } /// <summary> /// Unique ID provided by DEP to reseller on provisioning DEP access. This would be the reseller DEP ID if its posted by distributor OBO a reseller, and would be his own depResellerId if a reseller is posting for self. /// </summary> [JsonProperty("depResellerId")] public string DepResellerId { get; set; } /// <summary> /// Unique transaction ID provided by the reseller /// </summary> [JsonProperty("transactionId")] public string TransactionId { get; set; } /// <summary> /// List of orders in the transaction (limit 1000 per transaction) /// </summary> [JsonProperty("orders")] public List<OrderDto> Orders { get; set; } } 

After creating this object, I send to my class RequestHandler and the BulkEnrollRequest method, which atm is written with the HttpClient Flurl extension, which can be found here: github

 public IResponse BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest) { try { var result = _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>(); result.Wait(); return result.Result; } catch (FlurlHttpTimeoutException) { throw new AppleTimeOutException(); } catch (FlurlHttpException ex) { return _errorHandler.DeserializeFlurlException(ex); } } 

I also tried this to make sure that nothing happens in Flurl (this is just for debugging until the moment I want to get an answer):

 public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest) { var json = JsonConvert.SerializeObject(enrollmentRequest); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await httpClient.PostAsync(_bulkEnrollUrl, new StringContent(json, Encoding.UTF8, "application/json")); return new SuccessResponse(); } 

And the code hangs at the waiting point, and nothing happens. I tried to set a breakpoint after BulkEnrollRequest so that it never leaves this method.

Now for the funny part: it worked when I was working on ErrorHandler, and at some point the API just stopped responding. Wireshark shows that the request is being made ... so I'm stuck.

Any help is appreciated.

EDIT

Now it works! I implemented an asynchronous path from the API controller to RequestHandler, and then expected each call. For reference:

 public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest) { try { var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>(); return result; } catch (FlurlHttpTimeoutException) { throw new AppleTimeOutException(); } catch (FlurlHttpException ex) { return _errorHandler.DeserializeFlurlException(ex); } } 
+6
source share
2 answers

This is now fixed. I believe the reason Wireshark traffic looked so weird was because of a deadlock in the code, and a timeout was on my side, which meant that I could never get FIN ACK information. To fix this, I implemented async for all methods, from the controller to my RequestHandler class. For reference:

 public async Task<IResponse> BulkEnrollRequest(EnrollmentRequestDto enrollmentRequest) { try { var result = await _bulkEnrollUrl.PostJsonAsync(enrollmentRequest).ReceiveJson<SuccessResponse>(); return result; } catch (FlurlHttpTimeoutException) { throw new AppleTimeOutException(); } catch (FlurlHttpException ex) { return _errorHandler.DeserializeFlurlException(ex); } } 
+2
source

Try this code

Microsoft.AspNet.WebApi.Client installation package

  using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:9000/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.PostAsJsonAsync("api/products", enrollmentRequest); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsAsync<T>(); } } 
0
source

All Articles