1

I am attempting to call a Web API POST action (.Net Core) from a WinForms app (.Net Framework 4.7) via Flurl. However, when I try to debug the exception Flurl throws, my request doesn't even enter the controller itself.

Trying to debug the problem line by line didn't help. I suppose because since Web API and WinForms are two different projects in the same solution, I can't enter the debugging from the form to controller?

   at Flurl.Http.FlurlRequest.<HandleExceptionAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Flurl.Http.FlurlRequest.<SendAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Flurl.Http.HttpResponseMessageExtensions.<ReceiveJson>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at BTM.WinUI.APIService.<Insert>d__12`1.MoveNext() in C:\Users\User\Documents\Visual Studio 2019\Projects\APITest\APITest.WinUI\APIService.cs:line 61

Basically I instantiate an object of a class I made and call my API service:

var entity = new UserRegistrationModel
{
        Username = "test",
        Email = "test@test.com",
        Password = "test",
        PasswordConfirmation = "test",
        Roles = new List<int>()
};
entity.Roles.Add(1);
entity.Roles.Add(2);

await _service.Insert<User>(entity);

This is what my call looks like:

public async Task<T> Insert<T>(object request)
{
        string url = "https://localhost:44365/api/users";
        return await url.PostJsonAsync(request).ReceiveJson<T>();
}

My controller should handle it by adding a new user to the database and returning an User object, but the method's code gets never executed.

[HttpPost]
public User Post(UserRegistrationModel request)
{
        return _service.Register(request);
}
Bowser
  • 87
  • 1
  • 7
  • You're getting a `FlurlHttpException` I assume. See if you can catch that exception and inspect the details - it should be more telling than the stack trace. If the API returned a non-200 response, for example, an exception would be thrown. – Todd Menier May 15 '19 at 23:23
  • 1
    @ToddMenier Yes, it is a `FlurlHttpException`. By inspecting it, I managed to get the stack trace above. What the exception Message says is that is simply failed to do the POST request to my `https://localhost:44365/api/users`. – Bowser May 16 '19 at 10:47
  • You should see all information about this bug in Exception. What code is in `StatusCode`? This bug seems to do nothing with FlUrl but with client-server communication. – Karel Kral May 16 '19 at 14:23

1 Answers1

2

It could be that Flurl is unable to send a POST request to your localhost due to HTTPS. Try disabling SSL in your ASP NET Core Web API project.

TheDoomDestroyer
  • 2,434
  • 4
  • 24
  • 45