Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在表单关闭事件中调用webapi无效_C#_Winforms_Asp.net Core Webapi_Formclosing - Fatal编程技术网

C# 在表单关闭事件中调用webapi无效

C# 在表单关闭事件中调用webapi无效,c#,winforms,asp.net-core-webapi,formclosing,C#,Winforms,Asp.net Core Webapi,Formclosing,试图捕获在Winform应用程序关闭时注销的用户。在客户端窗体中,触发了orm_Closing事件,但HttpClient.PostAsync调用停止,webapi根本没有收到post using (var client = new HttpClient(handler)) { var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "applica

试图捕获在Winform应用程序关闭时注销的用户。在客户端窗体中,触发了
orm_Closing
事件,但
HttpClient.PostAsync
调用停止,webapi根本没有收到post

using (var client = new HttpClient(handler))
        {
            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");

            var response = await client.PostAsync(requestUrl, content);
            response.EnsureSuccessStatusCode();

            var responseAsString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<T>(responseAsString);
        }
使用(var-client=new-HttpClient(handler))
{
var content=newstringcontent(JsonConvert.SerializeObject(数据),Encoding.UTF8,“application/json”);
var response=wait client.PostAsync(requestUrl,content);
response.EnsureSuccessStatusCode();
var responseAsString=await response.Content.ReadAsStringAsync();
返回JsonConvert.DeserializeObject(responseAsString);
}

有什么想法吗?

不要使用
Async/Await
进行该调用。应用程序在后台线程上的请求完成之前结束


让它成为一个同步调用。这样,它将在主UI线程上执行,而主UI线程必须在进程终止之前等待webapi响应。

不要对该调用使用
Async/Await
。应用程序在后台线程上的请求完成之前结束


让它成为一个同步调用。这样,它将在主UI线程上执行,而主UI线程在进程终止之前必须等待webapi响应。

根据经验,任何具有可变属性的事件处理程序(例如
CancelEventArgs.Cancel
)必须同步调用,并且不能使用
await
。在调用私有异步void MainForm2\u FormClosing(对象发送方,FormClosingEventArgs e){await logService.Info(“用户注销”);}时使用await是否可以@beewest从该事件中删除
async/await
。根据经验,任何具有可变属性(例如
CancelEventArgs.Cancel
)的事件处理程序都必须同步调用,并且不能使用
await
。在调用私有async void MainForm2\u FormClosing时使用await是否可以(objectsender,FormClosingEventArgs e){await logService.Info(“用户注销”);}@beewest从该事件中删除
async/await