无法从 system.threding.Tasks .task<byte[]> 转换为 byte[]
cannot convert from system.threding.Tasks .task<byte[]> to byte[]
我是 c# 的新手并且有反应。我正在使用以下方法将图像 url 转换为字节
return Convert.ToBase64String(bytes);
但我收到一条错误消息
cannot convert from System.Threading.Tasks.Task<byte[]>
to byte[]
这是方法:
[HttpGet]
[Route("GetImages")]
public IHttpActionResult GetImages()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
myproxy.BypassProxyOnLocal = false;
//myproxy.UseDefaultCredentials = true;
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = myproxy
};
using (var client = new HttpClient(handler))
{
var bytes =
client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
return Convert.ToBase64String(bytes);
}
}
如何更正此错误?
GetByteArrayAsync
是一个异步方法,return 是一个任务。您需要等待任务才能获取 return 值。为了等待它,动作方法必须是异步的。
[HttpGet]
[Route("GetImages")]
public async Task<IHttpActionResult> GetImages()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
myproxy.BypassProxyOnLocal = false;
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = myproxy
};
using (var client = new HttpClient(handler))
{
var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
return Convert.ToBase64String(bytes);
}
}
由于 GetByteArrayAsync returns 任务,您必须等待任务完成:
var bytes = client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4").Result
或
var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4")
第二种方式通常比第一种好,它不会阻塞线程
我是 c# 的新手并且有反应。我正在使用以下方法将图像 url 转换为字节
return Convert.ToBase64String(bytes);
但我收到一条错误消息
cannot convert from
System.Threading.Tasks.Task<byte[]>
tobyte[]
这是方法:
[HttpGet]
[Route("GetImages")]
public IHttpActionResult GetImages()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
myproxy.BypassProxyOnLocal = false;
//myproxy.UseDefaultCredentials = true;
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = myproxy
};
using (var client = new HttpClient(handler))
{
var bytes =
client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
return Convert.ToBase64String(bytes);
}
}
如何更正此错误?
GetByteArrayAsync
是一个异步方法,return 是一个任务。您需要等待任务才能获取 return 值。为了等待它,动作方法必须是异步的。
[HttpGet]
[Route("GetImages")]
public async Task<IHttpActionResult> GetImages()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
myproxy.BypassProxyOnLocal = false;
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = myproxy
};
using (var client = new HttpClient(handler))
{
var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
return Convert.ToBase64String(bytes);
}
}
由于 GetByteArrayAsync returns 任务,您必须等待任务完成:
var bytes = client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4").Result
或
var bytes = await client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl-coil-
qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926?
alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4")
第二种方式通常比第一种好,它不会阻塞线程