gpt4 book ai didi

c# - 任务不包含内容的定义

转载 作者:太空狗 更新时间:2023-10-30 01:29:54 24 4
gpt4 key购买 nike

如何克服下面的错误?我按照下面的教程进行操作,但我得到的红线表示不包含 Content 的定义。 https://www.youtube.com/watch?v=IVvJX4CoLUY

private void UploadFile_Cliked(object sender, EventArgs e)
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
var httpClient = new HttpClient();

var uploadServiceBaseAddress = "http://192.168.137.1/pic/";
var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = httpResponseMessage.Content.ReadAsStringAsync();
}

enter image description here

'Task' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?)

这是错误,但有解决办法吗?

我已经添加了这个 Microsoft.AspNet.WebApi.Client 包,但仍然失败

最佳答案

使事件处理程序异步,然后等待返回任务派生结果的函数,因为您在调用 ReadAsStringAsync 时会遇到同样的问题

private HttpClient httpClient = new HttpClient();

private async void UploadFile_Cliked(object sender, EventArgs e) {
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");

var uploadServiceBaseAddress = "http://192.168.137.1/pic/";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
}

请注意,事件处理程序是允许 async void 的规则的异常(exception)

引用 Async/Await - Best Practices in Asynchronous Programming

关于c# - 任务不包含内容的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com