Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# HttpClient检查有效映像文件异步_C#_Asynchronous_Asp.net Mvc 2_Httpclient - Fatal编程技术网

C# HttpClient检查有效映像文件异步

C# HttpClient检查有效映像文件异步,c#,asynchronous,asp.net-mvc-2,httpclient,C#,Asynchronous,Asp.net Mvc 2,Httpclient,我是否错误地使用了HttpClient类。我试图测试图像的HTTP状态,但它似乎根本没有执行。我有一个复杂对象的列表,所以我想对所有图像URL运行测试,看看哪些URL被破坏: var client = new HttpClient(); var tasks = ObjectViewModel.Select(a => a.UserUrl).Select(url => client.GetAsync(url).ContinueWith(t => { var respons

我是否错误地使用了HttpClient类。我试图测试图像的HTTP状态,但它似乎根本没有执行。我有一个复杂对象的列表,所以我想对所有图像URL运行测试,看看哪些URL被破坏:

var client = new HttpClient();
var tasks = ObjectViewModel.Select(a => a.UserUrl).Select(url =>   
client.GetAsync(url).ContinueWith(t =>
{
  var response = t.Result;
   if (!response.IsSuccessStatusCode)
   url = "/Content/Images/MissingPic.png";
 }));
我最初是在foreach循环中这样做的:

foreach(var Model in ObjectViewModel)
{
 Model.UserUrl= Model.UserUrl.GetHttpRequest() ? Model.UserUrl: 
"/Content/Images/MissingImage.png";
 //Model.state= Model.state.ValidName();// this line is something seperate
 //Model.property= Model.state.propertyCheck();// this line is something seperate

}

public static bool GetHttpRequest(this string s)
{
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest
                                       .Create(s);
        webRequest.AllowAutoRedirect = false;
        HttpStatusCode responseStatusCode;

        try
        {
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            responseStatusCode = response.StatusCode;
        }
        catch (WebException we)
        {
            responseStatusCode = ((HttpWebResponse)we.Response).StatusCode;
        }
        if (responseStatusCode.ToString() == "OK")
            return true;
        else
            return false;
}
它工作得非常好,但是需要大约5到7秒来完成所有项目,因为它们都是单独运行的,这对于响应UI的请求来说非常长。

Linq(通常)是懒惰的。这意味着Linq语句只表示一个查询。枚举查询(具体化)时会发生工作

您从未具体化您的
任务
查询。为了让它实际运行select语句中的代码,需要通过枚举查询来具体化查询

一种方法是简单地调用
任务。ToList()
Linq(通常)是懒惰的。这意味着Linq语句只表示一个查询。枚举查询(具体化)时会发生工作

您从未具体化您的
任务
查询。为了让它实际运行select语句中的代码,需要通过枚举查询来具体化查询

一种方法是简单地调用tasks.ToList()

考虑在遍历枚举时使用aspallel(),这样会大大加快速度

var UrlToReponseMap = new Dictionary<string, bool>();
ObjectViewModel.AsParallel().ForAll(x =>
{
  UrlToReponseMap[x.UserUrl] = x.UserUrl.GetHttpRequest();
});
var UrlToReponseMap=new Dictionary();
ObjectViewModel.AsParallel().ForAll(x=>
{
UrlToReponseMap[x.UserUrl]=x.UserUrl.GetHttpRequest();
});
考虑在遍历枚举时使用AsParallel(),这样会大大加快速度

var UrlToReponseMap = new Dictionary<string, bool>();
ObjectViewModel.AsParallel().ForAll(x =>
{
  UrlToReponseMap[x.UserUrl] = x.UserUrl.GetHttpRequest();
});
var UrlToReponseMap=new Dictionary();
ObjectViewModel.AsParallel().ForAll(x=>
{
UrlToReponseMap[x.UserUrl]=x.UserUrl.GetHttpRequest();
});

Ok现在看起来它实际运行了,但是断开的URL没有被替换Ok现在看起来它实际运行了,但是断开的URL没有被替换这不是httpclient上的GetAsync正在做的吗?还将AsParallel放置在何处?我使用了Parallel.ForEach,但这确实让我进入了正确的方向httpclient上的GetAsync不就是这么做的吗?还把AsParallel放在哪里?我用了Parallel.ForEach,但这确实让我走上了正确的方向