2011-12-27 12 views
15

何らかの理由で、特定のボットがサイトにアクセスしたときにUrlHelper.ActionメソッドでURLを生成すると、ヌル例外がSystem.Web.HttpServerVarsCollection.Getから発生します。私はいくつかのデバッグを行い、呼び出しスタックはHttpContextBase.Request.ServerVariablesコレクションから 'HTTP_X_ORIGINAL_URL'を取得しようとしています。UrlHelper.ActionメソッドでURLを生成するときのヌル参照例外

ブラウザから直接同じアドレスにアクセスすると問題ありません。ページはサーバーであり、エラーは記録されません。それはボットが訪問したときにのみ発生するようです。

関連性があるかどうかわかりませんが、サイトはIIS 7.5に移行されたばかりです。 .NET 2.0を統合モードで使用しています。

Reflectorと逆のコードを見ると、Getメソッドでnull例外が直接発生する唯一の場所はthis._request.FetchServerVariablesへの呼び出しです。完全な要求が正しく設定されていないかのように。

誰かがこの問題に直面したか、回避策を発見しましたか?ボットが訪問したときにリクエストが別々に設定されるのはなぜですか?

UPDATE:一部の追加のデバッグでは、HttpServerVarsCollectionが親のHttpRequestオブジェクトとともにDisposedされていることが示されています。今の質問は、要求が完了する前にHttpContext.Currentから返されたRequestオブジェクトをどのように公開できるのでしょうか?

HttpServerVarsCollection.Get方法

public override string Get(string name) 
{ 
    if (!this._populated) 
    { 
     string simpleServerVar = this.GetSimpleServerVar(name); 
     if (simpleServerVar != null) 
     { 
      return simpleServerVar; 
     } 
     this.Populate(); 
    } 
    if (this._iis7workerRequest == null) 
    { 
     return this.GetServerVar(base.BaseGet(name)); 
    } 
    string serverVar = this.GetServerVar(base.BaseGet(name)); 
    if (string.IsNullOrEmpty(serverVar)) 
    { 
     // Only place null reference can happen 
     serverVar = this._request.FetchServerVariable(name); 
    } 
    return serverVar; 
} 

のフルスタックトレース

NullReferenceException: Object reference not set to an instance of an object.] 
    System.Web.HttpServerVarsCollection.Get(String name) +8645730 
    System.Collections.Specialized.NameValueCollection.get_Item(String name) +7 
    System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\PathHelpers.cs:39 
    System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\PathHelpers.cs:21 
    System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\UrlHelper.cs:136 
    System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\UrlHelper.cs:101 
    System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\UrlHelper.cs:51 
    www.CmsExtensions.Document(UrlHelper urlHelper, String path) in C:\Dev\Site\www\Code\CmsExtensions.cs:33 
    www.CmsExtensions.Document(UrlHelper urlHelper, Document document) in C:\Dev\Site\www\Code\CmsExtensions.cs:20 
    www.<>c__DisplayClass17.<Load>b__c(Document d) in C:\Dev\Site\www\Global.asax.cs:251 
    Fringine.Cms.DocumentContentParser.ReplaceDocumentRefs(IResolvingDocumentCache cache, Match match) +258 
    Fringine.Cms.<>c__DisplayClass4.<ParseContent>b__2(Match m) +17 
    System.Text.RegularExpressions.RegexReplacement.Replace(MatchEvaluator evaluator, Regex regex, String input, Int32 count, Int32 startat) +234 
    System.Text.RegularExpressions.Regex.Replace(String input, MatchEvaluator evaluator, Int32 count, Int32 startat) +28 
    System.Text.RegularExpressions.Regex.Replace(String input, MatchEvaluator evaluator) +38 
    System.Text.RegularExpressions.Regex.Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options) +47 
    Fringine.Cms.DocumentContentParser.ParseContent(String content, IResolvingDocumentCache cache) +83 
    Fringine.Cms.ResolvingDocumentCache.<Parse>b__0(String d) +21 
    Fringine.Cms.DocumentCache.GetParsedData(String id, String content, IDocumentService documentService, Func`2 parser) +216 
    Fringine.Cms.ResolvingDocumentCache.Parse(String id, String content) +67 
    Fringine.Cms.CachedDocument.GetSummary() +966 
    Fringine.Cms.CachedDocument.get_Summary() +19 
    ASP.views_document_widget_feeddocumentsummary_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) +841 
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256 
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 
    System.Web.UI.Control.Render(HtmlTextWriter writer) +10 
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 
    System.Web.UI.Page.Render(HtmlTextWriter writer) +29 
    System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) in C:\Dev\Site\MVC\Microsoft\src\SystemWebMvc\Mvc\ViewPage.cs:107 
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266 
+0

'HttpContext.Current.Request'は、要求が完了する前に破棄されたはずです。おそらく '(HttpContext.Current.Request)'またはそれと同等のものを使用しているコードを持っていますか? –

+1

いいえ...それは私が当初考えていた数少ないものの1つでした。私は持っているすべてのコードを見てきましたが、何も明示的な(または暗黙的な使用を通して)要求オブジェクトに対してDisposeを呼び出すことはありません。しかし、私は、クラシックやASP.NET開発サーバーではなく、IIS 7の統合モードでのみ発生するという、より多くのデバッグを行いました。 –

+0

実際には、私はIIS 7.5で確認済みです。o Windows 7と2008 R2。実際にIIs 7.0を黙認していない。 –

答えて

1

私はこの質問は今かなり古いですけど、私は最近、どこに非常に似たような状況で自分自身を発見しましたUrlHelperは私にSystem.Web.HttpServerVarsCollection.Getのnull参照例外を与えていました。

問題は実際にはHTTP_X_ORIGINAL_URLでした。この特定のサーバー変数は、URLリライト2.0モジュール(IIS上)から発生します。

興味深いことに、このモジュールはインストールされましたが使用されていませんでした。しかし、その存在だけで問題が発生しました。それをアンインストールすると、エラーがなくなりました。

URLの書き換えが必要な場合は、他のモジュールがあるか、アプリケーションレベルで行うことができます。

希望に役立ちます。

3

この問題はありましたが、URL書き換えモジュールとは関係ありませんでした。

私のケースでは、私は誤って静的フィールドにUrlHelperのインスタンスをキャッシュし、後の要求で以前の要求から破棄されたインスタンスが発生しました。

+0

これはたぶん私にデバッグの週を救った、ありがとう – AaronHS

1

私はこれに遭遇しましたが、私の場合、UrlHelperの非拡張メソッドの使用法に切り替えることで解決できました。私は別のメソッドを呼び出す拡張メソッドを1つ持っていましたが、UrlHelperを2番目のメソッドに渡しました。

このように壊れていた:

public static string Script(this UrlHelper helper, string fileName) 
{ 
    return Asset(helper, "~/js/", fileName); 
} 

private static string Asset(this UrlHelper helper, string path, string fileName) 
{ 
    return helper.Content(string.Format("{0}/{1}/{2}", 
      path, 
      Version, 
      fileName)); 
} 

このように動作する:

public static string Script(this UrlHelper helper, string fileName) 
{ 
    return Asset(helper, "~/js/", fileName); 
} 

private static string Asset(UrlHelper helper, string path, string fileName) 
{ 
    return helper.Content(string.Format("{0}/{1}/{2}", 
      path, 
      Version, 
      fileName)); 
} 

注資産方法の機能シグネチャ差。

関連する問題