2016-01-01 100 views
1

我正在使用.NET 3.5和一个简单的http请求处理程序。现在,在每个http请求中,我的处理程序会打开一个与3个远程服务器的tcp连接,以便从它们接收一些信息。然后关闭套接字并将服务器状态写回Context.Response。不同请求中的共享对象

但是,我宁愿有一个单独的对象,每隔5分钟通过tcp连接到远程服务器,获取信息并保留它。所以每个请求上的HttpRequest只要求这个对象提供信息就快得多。

所以我的问题在这里是,如何保持一个共享的全局对象在内存中所有可也“唤醒”的做那些TCP连接连时候没有HTTP请求来了,有对象入店到HTTP请求处理程序。

+1

你可以有针对运行Windows服务,并将数据写入到文件中某处ASP应用程序可以读取它。 –

+0

正在写入文件时请求到达的情况如何?另外,我不确定这是否是最好的方法? –

+0

然后稍等片刻,直到文件可读。 –

回答

0

一个服务可能是这个矫枉过正。

您可以在您的应用程序启动中创建一个全局对象,并让它创建一个后台线程,每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... 
    }