和你一起分析Http Handler從IIS6到IIS7的使用問題_負載集群教程

      編輯Tag賺U幣
      我們這里介紹一下在IIS6下開發的文件到IIS7上以后出現的問題我們該如何處理,我們將一步一步分析并解決問題。下面是一個監控服務器狀態的頁面,內容很簡單,就是顯示一下服務器當前CPU和內存的使用情況。因為想讓它每3秒刷新一次,所以就把功能放在了ashx頁里面,頁面每3秒調用一次。
      $(document).ready(function() {
      loaddata();
      setInterval(loaddata,
      3000);
      });
      function loaddata() {
      $.getJSON(
      "Common/ServerInfoHandler.ashx?format=json&jsoncallback=?", {}, function(json) {
      $(
      "#labServerName").text("服務器名稱:" + json.ServerName);
      $(
      "#labServerIP").text("服務器地址:" + json.ServerIP);
      $(
      "#NumberOfProcessors").text("CPU數量:" + json.CPUNum);
      $(
      "#ProcessorType").text("CPU類型:" + json.CPUType);
      $(
      "#ProcessorLevel").text("CPU級別:" + json.CPULevel);
      $(
      "#MemoryLoad").text("內存使用率:" + json.MemoryLoad);
      $(
      "#TotalPhys").text("物理內存共有:" + json.MemoryTotalPhys);
      $(
      "#AvailPhys").text("可使用的物理內存有:" +
      $(
      "#Date").text("服務器時間:" + json.DateTime);
      $(
      "#cpu").text("CPU平均使用率:" + json.CPULoad);
      });
      }

       

               東西很簡單,寫完以后用VisualStudio直接運行,發現取到并顯示出來的服務器名和地址都是127.0.0.1,所以就想在IIS上部署一下看看。開發的機器是Win7,自帶的是IIS7,跟IIS6差別太大了。好不容易部署好了以后,瀏覽頁面發現沒有任何信息顯示出來,在Visual Studio開調試,發現ashx頁根本沒有被調用。

      下面我們說說解決辦法:

                        就是在IIS7中你的網站的

                     

         里面右上角

                 

        “添加托管處理程序”中加上你的ashx。

         

       這里直接在IIS里面加了,發現還是不好使。下面說說配置:

      代碼
      <system.webServer>
      <handlers>
      <addname="Custom Handler" path="*.sample"verb="*" type="HelloWorldHandler"
      resourceType
      ="Unspecified" />
      </handlers>
      </system.webServer>

        拷過來再說,扔進我的web.config改吧改吧,你要用Synchronous HTTPHandlers  不是建ashx了,而是在App_Code里建一個類,然后繼承、實現IHttpHandler

      代碼
      usingSystem.Web;

      public class HelloWorldHandler : IHttpHandler
      {
      public HelloWorldHandler()
      {
      }
      public voidProcessRequest(HttpContext context)
      {
      HttpRequestRequest
      = context.Request;
      HttpResponse Response
      = context.Response;
      // This handler is called whenever a file ending
      // in .sample is requested. A file with that extension
      // does not need to exist. 
      Response.Write(
      "Hello Handler!");
      }
      public boolIsReusable
      {
      // To enable pooling, return true here.
      // This keeps the handler in memory. 
      get { return false; }
      }
      }

       

      然后呢,配置web.config。這里比較麻煩,有三種配置方式。

      IIS6:

      <configuration>
      <system.web>
      <httpHandlers>
      <add verb="*" path="*.sample"
      type
      ="HelloWorldHandler"/>
      </httpHandlers>
      </system.web>
      </configuration>

        IIS7是分兩種工作模式經典模式和集成模式,所以配置也是分兩種的。

        經典模式是為了與之前的版本兼容,使用ISAPI擴展來調用ASP.NET運行庫,原先運行于IIS6.0下的Web應用程序遷移到IIS7.0中只要將應用程序配置成經典模式,代碼基本不用修改就可以正常運行。

      配置如下

      代碼
      <configuration>
      <system.web>
      <httpHandlers>
      <add verb="*" path="*.sample"
      type
      ="HelloWorldHandler"/>
      </httpHandlers>
      </system.web>
      <system.webServer>
      <handlers>
      <add verb="*" path="*.sample"
      name
      ="HelloWorldHandler"
      type
      ="HelloWorldHandler"
      modules
      ="IsapiModule"/> 
      </handlers>
      </system.webServer>
      </configuration>

        集成模式是一種統一的哀求處理管道,它將ASP.NET請求管道與IIS核心管道組合在一起,這種模式能夠提供更好的性能,能夠實現配置和治理的模塊化,而且增加了使用托管代碼模塊擴展IIS時的靈活性。

      配置

      代碼
      <configuration>
      <system.webServer>
      <handlers>
      <add verb="*" path="*.sample"
      name
      ="HelloWorldHandler"
      type
      ="HelloWorldHandler"/>
      </handlers>
      </system.webServer>
      </configuration>

        關于配置我要解釋幾句,name和type就不用說,verb不知道啥意思,照寫就行;path這個節里面的值是可以指定的,現在這樣*.sample在測試的時候,你就要指向test.Sample。如果你有多個Handler的話就不能這么指定了,最好是指定一個名字比如:

      <add verb="*" path="HelloWorld.sample" name="HelloWorldHandler" type="HelloWorldHandler"/>
      <add verb="*" path="HelloHeaven.sample" name="HelloHeavenHandler" type="HelloHeavenHandler"/>

       

      寫完配置以后就可以測試一下自己的Handler了,
      測試地址如下
      http://localhost/HttpHandler/test.sample

      當然你要是設置了名字就可以用

       

      http://localhost/HttpHandler/ HelloWorld.sample 
      http:
      //localhost/HttpHandler/ HelloHeaven.sample

       

      用的時候也就變成了

      $.getJSON("HandlerHelloWorld.sample?format=json&jsoncallback=?",……
        下面是本文的疑問?用類寫Handler的話那ashx呢?怎么用啊?

        添加新文件的時候一般處理程序(ashx)還是可以選的。

        還有就是iis6和iis7要寫不同的三種配置,有沒有通用的寫法?不用具體非的指定某種IIS。

      來源:網絡搜集//所屬分類:負載集群教程/更新時間:2011-12-08
      相關負載集群教程