我有一个 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);
此控制器操作隐藏了大量复杂的逻辑,因为它是专门为格式化 JSON 列表而设计ListViewResult
的自定义结果。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
在线路上抛出avm => Url.Content(...)
和vm => Url.Action(...)
. 这并不总是发生,但如果我刷新几次,我可以可靠地重现它。
更新
在源代码中挖掘了一段时间后,我相信我已经发现了有问题的代码。问题似乎出在UrlRewriterHelper.WasThisRequestRewritten
调用ServerVariables.Get("IIS_WasUrlRewritten")
. 似乎该方法使用了一个名为的私有字段_request
,该字段null
位于代码中(出于某种原因)。我最好的猜测是,在动作返回和结果执行之间的一段时间内,ServerVariables
集合要么失去它对的内部引用_request
——要么——在ServerVariables
没有有效引用的情况下重新创建集合_request
。
我也意识到这可能是 IIS Express 的问题。在 Visual Studio 开发服务器下运行时,我无法复制该问题。我更新了标签以反映最可能的原因。