我有一個MVC3應用程序,它突然給我一些奇怪的行爲。首先是一些背景(儘管我會盡量使其儘可能簡短)。Request.ServerVariables拋出NullReferenceException
在我的控制器動作,我有這樣的代碼:
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();
}
}
}
,似乎偶爾NullReferenceException
的在該行vm => Url.Content(...)
和vm => Url.Action(...)
被拋出。這不是總是發生,但如果我刷新幾次,我可以可靠地重現它。
更新
在一會兒的源代碼周圍挖掘後,我相信我已經發現有問題的代碼。這個問題似乎與UrlRewriterHelper.WasThisRequestRewritten
方法,它調用ServerVariables.Get("IIS_WasUrlRewritten")
。看來這種方法使用了一個名爲_request
的專用字段,它是null
(在某些情況下)。我最好的猜測是,在返回的動作和執行結果之間的某個時間段,ServerVariables
集合要麼丟失了它的內部參照_request
- 或 - 正在重新創建集合ServerVariables
,而沒有對_request
的有效參考。
我也意識到這可能是IIS Express的問題。在Visual Studio Development Server下運行時,我無法重現該問題。我更新了標籤以反映最可能的原因。
有趣的問題,+1。 – Brian 2013-02-28 23:09:44
這是從調試還是隻是在探討? – 2013-03-02 03:25:01
調試。我也意識到'_request'被'ServerVarsCollection.Dispose'設置爲null。所以也許控制器被處置掉(仍然不能解釋爲什麼我偶爾會遇到這個錯誤)。 – 2013-03-02 13:28:05