一個服務可能是這個矯枉過正。
您可以在您的應用程序啓動中創建一個全局對象,並讓它創建一個後臺線程,每5分鐘執行一次查詢。將響應(或響應中處理的內容)放到單獨的類中,爲每個響應創建該類的新實例,並在每次檢索響應時使用System.Threading.Interlocked.Exchange
來替換靜態實例。當你想要查看響應時,只需將靜態實例的引用複製到堆棧引用,並且您將擁有最新的數據。
但請記住,只要沒有一定的時間(空閒時間)請求,ASP.NET就會終止您的應用程序,因此您的應用程序將停止並重新啓動,導致您的全局對象被破壞並重建。
你可能在其他地方看過你不能或不應該在ASP.NET中做背景的東西,但事實並非如此 - 你只需要瞭解它的含義。我在處理超過1000 req/sec峯值(跨多個服務器)的ASP.NET站點上使用與以下示例類似的代碼。
例如,在的global.asax.cs:
public class BackgroundResult
{
public string Response; // for simplicity, just use a public field for this example--for a real implementation, public fields are probably bad
}
class BackgroundQuery
{
private BackgroundResult _result; // interlocked
private readonly Thread _thread;
public BackgroundQuery()
{
_thread = new Thread(new ThreadStart(BackgroundThread));
_thread.IsBackground = true; // allow the application to shut down without errors even while this thread is still running
_thread.Name = "Background Query Thread";
_thread.Start();
// maybe you want to get the first result here immediately?? Otherwise, the first result may not be available for a bit
}
/// <summary>
/// Gets the latest result. Note that the result could change at any time, so do expect to reference this directly and get the same object back every time--for example, if you write code like: if (LatestResult.IsFoo) { LatestResult.Bar }, the object returned to check IsFoo could be different from the one used to get the Bar property.
/// </summary>
public BackgroundResult LatestResult { get { return _result; } }
private void BackgroundThread()
{
try
{
while (true)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://example.com/samplepath?query=query");
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
// get what I need here (just the entire contents as a string for this example)
string result = reader.ReadToEnd();
// put it into the results
BackgroundResult backgroundResult = new BackgroundResult { Response = result };
System.Threading.Interlocked.Exchange(ref _result, backgroundResult);
}
}
}
catch (Exception ex)
{
// the request failed--cath here and notify us somehow, but keep looping
System.Diagnostics.Trace.WriteLine("Exception doing background web request:" + ex.ToString());
}
// wait for five minutes before we query again. Note that this is five minutes between the END of one request and the start of another--if you want 5 minutes between the START of each request, this will need to change a little.
System.Threading.Thread.Sleep(5 * 60 * 1000);
}
}
catch (Exception ex)
{
// we need to get notified of this error here somehow by logging it or something...
System.Diagnostics.Trace.WriteLine("Error in BackgroundQuery.BackgroundThread:" + ex.ToString());
}
}
}
private static BackgroundQuery _BackgroundQuerier; // set only during application startup
protected void Application_Start(object sender, EventArgs e)
{
// other initialization here...
_BackgroundQuerier = new BackgroundQuery();
// get the value here (it may or may not be set quite yet at this point)
BackgroundResult result = _BackgroundQuerier.LatestResult;
// other initialization here...
}
你可以有針對運行Windows服務,並將數據寫入到文件中某處ASP應用程序可以讀取它。 –
正在寫入文件時請求到達的情況如何?另外,我不確定這是否是最好的方法? –
然後稍等片刻,直到文件可讀。 –