问题描述
我针对一个Web服务,接受一个字符串参数。
按计划我要火了大约100个电话,为从我的数据库100个值的Web服务。
为了优化过程中,我相信我需要做的WebRequest异步调用。
我已经遇到了code 示例下面对各种博客等,但无法弄清楚如何去适应它作为我的要求。
我该如何收官RegisterAsyncTask foreach循环内,通过URI进行解析类向WebRequest.Create()这里面BeginAsyncOperation?
I'm targeting a web service that takes a single string parameter.On a schedule I want to fire off approximately 100 calls to that web service for 100 values from my database.To optimise the process I believe I need to do the WebRequest calls asynchronously.I've come across the code example below on a variety of blogs etc. but can't figure out how to adapt it for my requirement.How can I wrap up the RegisterAsyncTask inside a foreach loop, parsing through the uri for the WebRequest.Create() that's inside BeginAsyncOperation?
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public partial class AsyncPageTask : System.Web.UI.Page
{
private WebRequest _request;
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
RegisterAsyncTask(task);
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
AsyncCallback cb, object state)
{
_request = WebRequest.Create("http://msdn.microsoft.com");
return _request.BeginGetResponse(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
string text;
using (WebResponse response = _request.EndGetResponse(ar))
{
using (StreamReader reader =
new StreamReader(response.GetResponseStream()))
{
text = reader.ReadToEnd();
}
}
Output.Text = text;
}
void TimeoutAsyncOperation(IAsyncResult ar)
{
Output.Text = "Data temporarily unavailable";
}
}
我的本意是写响应字符串返回到数据库中。鸭preciate这是一个额外的问题,但是,没有任何理由不包括EndAsyncOperation方法中插入方法调用?
My intention is to write the response string back into a database. Appreciate this is an additional question but, is there any reason not to include the insert method call within the EndAsyncOperation method ?
这Q&A对我的主要问题,但其第四个参数提示?
This Q&A hints towards my main question but which 4th argument?
推荐答案
首先,你需要添加到您的Web.config文件(默认仅有2):
First you need to add this to your Web.config file (default is just 2):
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
<system.net>
<configuration>
然后你需要异步指令添加到您的网页:&LT;%@页面异步=真正的%GT;
protected void Page_Load(object sender, EventArgs e)
{
//...first get UriStringArray from db, and then:
foreach(string uri in UriStringArray)
{
var task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
uri,
true; //run in parallel
);
RegisterAsyncTask(task);
}
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
var request = (HttpWebRequest)WebRequest.Create((string)state);
return request.BeginGetResponse(cb, request);
}
void EndAsyncOperation(IAsyncResult ar)
{
string text;
var request = (HttpWebRequest)ar.State;
using(WebResponse response = request.EndGetResponse(ar))
{
using(StreamReader reader = new StreamReader(response.GetResponseStream()))
text = reader.ReadToEnd();
}
//yes, you can insert in db here, even as a new PageAsyncTask - but then must call ExecuteRegisteredAsyncTasks() manually...
}
这篇关于如何使用PageAsyncTask与WebRequest的多个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!