[英]Request.ServerVariables throws NullReferenceException
我有一個MVC3應用程序,它突然給了我一些奇怪的行為。 首先是一些背景(盡管我會嘗試盡可能簡短)。
在我的控制器操作中,我有這個代碼:
public ActionResult Grid(ApplicationViewModel search = null)
{
return this.ListView(
this.Find<Entity>(),
this.CreateViewModel,
mixins: new Dictionary<string, Func<EntityViewModel, object>>()
{
{ "Icon", vm => Url.Content("~\Images\entityType.png") },
{ "Link", vm => Url.Action("Details", vm.ControllerId) }
});
}
EntityViewModel CreateViewModel(Entity entity);
// Inherited base class methods
protected IQueryable<T> Find<T>(); // Find T entities in DB
protected ListViewResult<TModel, TViewModel> ListView<TModel, TViewModel>(
IQueriable<TModel> entityQuery,
Func<TModel, TViewModel> materializeViewModel,
IDictionary<string, Func<TViewModel, object>> mixins);
此控制器操作隱藏了大量復雜的邏輯,因為ListViewResult
是專門為格式化JSON列表而設計的自定義結果ActionResult
。
public class ListViewResult<TModel, TViewModel> :
ActionResult
{
public IQueryable<TModel> ViewData { get; set; }
public Func<TModel, TViewModel> Materialize { get; set; }
public Dictionary<string, Func<TViewModel, object>> Mixins { get; private set; }
...
public override void ExecuteResult(ControllerContext context)
{
// Perform sorting / paging / formatting on IQueryable
...
var viewModels = this.ViewData.Select(this.Materialize);
try
{
// another custom ActionResult for formatting JSON responses
new JsonNetResult()
{
Data = viewModels.ToArray(),
SerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new MixinContractResolver()
{
Mixins = this.Mixins
}
}
}.ExecuteResult(context);
}
catch (Exception e)
{
context.HttpContext.Response.StatusCode = 500;
context.HttpContext.Response.StatusDescription = e.Message;
}
}
private class MixinContractResolver :
CamelCasePropertyNamesContractResolver
{
public Dictionary<string, Func<TViewModel, object>> Mixins { get; set; }
private List<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
List<JsonProperty> props = // get base properties
foreach (var pair in this.Mixins)
{
props.Add(new JsonProperty()
{
Ignored = false,
NullValueHandling = NullValueHandling.Include,
Readable = true,
PropertyName = Inflector.Camelize(pair.Key),
PropertyType = typeof(object),
ValueProvider = new DelegateValueProvider<TViewModel, object>(pair.Value),
Writable = false,
});
}
}
}
private class DelegateValueProvider<T, R> :
Newtonsoft.Json.Serialization.IValueProvider
{
private readonly Func<T, R> func;
public DelegateValueProvider(Func<T, R> func)
{
this.func = func;
}
public object GetValue(object target)
{
return (R)this.func((T)target);
}
public void SetValue(object target, object value)
{
throw new NotSupportedException();
}
}
}
現在,似乎偶爾會在行vm => Url.Content(...)
和vm => Url.Action(...)
拋出NullReferenceException
。 這並不總是發生,但如果我刷新幾次,我可以可靠地重現它。
更新
在源代碼中挖掘了一段時間之后,我相信我已經發現了有問題的代碼。 問題似乎與調用ServerVariables.Get("IIS_WasUrlRewritten")
的UrlRewriterHelper.WasThisRequestRewritten
方法有關。 似乎該方法使用名為_request
的私有字段,此字段在代碼中為null
(由於某種原因)。 我最好的猜測是,在返回操作和執行結果之間的某個時間, ServerVariables
集合要么丟失它對_request
的內部引用 - 或者 - 正在重新創建ServerVariables
集合而沒有對_request
的有效引用。
我也意識到這可能是IIS Express的一個問題。 在Visual Studio開發服務器下運行時,我無法復制該問題。 我已經更新了標簽,以反映出最可能的原因。
我剛剛在IIS Express中遇到了同樣的問題,發現這是因為我已經將URL重寫擴展安裝到IIS然后將其卸載,但它在IIS Express的applicationhost.config文件中留下了一些配置。 (C:\\用戶\\\\文檔\\ IISExpress \\配置\\對ApplicationHost.config)
我只是注釋掉了該文件中的相關行,殺死了IIS Express並重新啟動了,我沒有再看到這個問題。
我注釋掉的兩行在下面的代碼段中進行了評論。
<configuration>
...
<system.webServer>
...
<globalModules>
<!--<add name="RewriteModule" image="%IIS_BIN%\rewrite.dll" />-->
...
</globalModules>
</system.webServer>
<location path="" overrideMode="Allow">
<system.webServer>
<modules>
<!--<add name="RewriteModule" />-->
...
</modules>
</system.webServer>
</location>
</configuration>
在視覺工作室中檢查您尚未檢查以在調試時顯示所有異常。 由於您的Debug-> exceptions設置,這可能只是您看到的內部處理異常。
檢查是否取消選中Just My Code,如果現在在debug-> exceptions窗口中顯示兩行復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.