Consider the following sample:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace WebApiApp.Controllers
{
public class TheModelFields
{
public int Id { get; set; }
}
[ModelBinder(typeof(TheModelBinder))]
public class TheModel
{
public PropertyInfo PropInfo { get; set; }
public PropertyInfo FieldPropInfo;
public object BoxedPropInfo { get; set; }
}
enum TestMode
{
PropInfo,
FieldPropInfo,
BoxedPropInfo
}
public class TheModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.HttpContext.Request.Query.TryGetValue("testMode", out var modeStr) && Enum.TryParse(typeof(TestMode), modeStr, true, out var mode))
{
var model = new TheModel();
var propInfo = typeof(TheModelFields).GetProperty("Id");
switch (mode)
{
case TestMode.PropInfo:
model.PropInfo = propInfo;
break;
case TestMode.FieldPropInfo:
model.FieldPropInfo = propInfo;
break;
case TestMode.BoxedPropInfo:
model.BoxedPropInfo = propInfo;
break;
}
bindingContext.Result = ModelBindingResult.Success(model);
Timer.Stopwatch.Restart();
return Task.CompletedTask;
}
else
{
bindingContext.Result = ModelBindingResult.Failed();
return Task.CompletedTask;
}
}
}
public static class Timer
{
public static Stopwatch Stopwatch = new Stopwatch();
}
[ApiController]
public class TestController : ControllerBase
{
[HttpGet("test")]
public IActionResult Test([FromQuery]TheModel model)
{
Timer.Stopwatch.Stop();
if (model is null)
return BadRequest("pass testMode=PropInfo|FieldPropInfo|BoxedPropInfo for test");
else
return Ok($"Time: {Timer.Stopwatch.ElapsedMilliseconds}");
}
}
}
The class TheModel
has a custom ModelBinder named TheModelBinder
.
In this test, TheModelBinder
decides what property/field to set based on the value of query string parameter named testMode
.
Using a static Stopwatch, I started measuring the time between the end of model binding and the beginning of the action. Here are the approximate results:
If testMode == PropInfo
then TheModelBinder
sets value to a property of type PropertyInfo
named PropInfo
.
(This is pretty slow, around 800-1000 ms)
If testMode == FieldPropInfo
then TheModelBinder
sets value to a field of type PropertyInfo
named PropInfoField
.
(This one takes 0ms)
If testMode == BoxedPropInfo
then TheModelBinder
sets value to a property of type object named BoxedPropInfo
.
(This one takes 0ms too)
Now the question is: Why the first testMode (setting the PropInfo
property) increases the execution time (after the model bound successfully) up to 800-1000 milliseconds?
Tested on asp.net core 2.1 and 2.2 preview2
To test this yourself, you can do dotnet new webapi
and paste the content of sample to a new file. If you run the app on port 5000
, you can test the execution time using these URLs:
http://localhost:5000/test?testMode=propInfo
http://localhost:5000/test?testMode=propInfoField
http://localhost:5000/test?testMode=boxedPropInfo