Flurl引发异常(MoveNext())

时间:2019-05-15 14:25:45

标签: winforms http-post asp.net-core-webapi flurl

我试图通过Flurl从WinForms应用程序(.Net Framework 4.7)调用Web API POST操作(.Net Core)。但是,当我尝试调试Flurl引发的异常时,我的请求甚至都没有进入控制器本身。

尝试逐行调试问题没有帮助。我想是因为Web API和WinForms是同一解决方案中的两个不同项目,所以我无法从表单输入调试信息到控制器?

   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

基本上,我实例化一个我创建的类的对象并调用我的API服务:

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);

这就是我的通话:

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

我的控制器应该通过向数据库添加新用户并返回User对象来处理它,但是该方法的代码永远不会执行。

[HttpPost]
public User Post(UserRegistrationModel request)
{
        return _service.Register(request);
}

1 个答案:

答案 0 :(得分:1)

可能是Flurl由于HTTPS无法向您的本地主机发送POST请求。尝试在ASP NET Core Web API项目中禁用SSL。