Reuse model binders (#22391)
* Reuse model binders Reuse the same model binders for CancellationTokens and services, rather than allocating a new one for every request with such a parameter.
This commit is contained in:
parent
3f83cebeb0
commit
675bceca57
|
|
@ -11,6 +11,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
|
|||
/// </summary>
|
||||
public class CancellationTokenModelBinderProvider : IModelBinderProvider
|
||||
{
|
||||
// CancellationTokenModelBinder does not have any state. Re-use the same instance for binding.
|
||||
|
||||
private readonly CancellationTokenModelBinder _modelBinder = new CancellationTokenModelBinder();
|
||||
|
||||
/// <inheritdoc />
|
||||
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
|
|
@ -21,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
|
|||
|
||||
if (context.Metadata.ModelType == typeof(CancellationToken))
|
||||
{
|
||||
return new CancellationTokenModelBinder();
|
||||
return _modelBinder;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
|
|||
/// </summary>
|
||||
public class ServicesModelBinderProvider : IModelBinderProvider
|
||||
{
|
||||
// ServicesModelBinder does not have any state. Re-use the same instance for binding.
|
||||
|
||||
private readonly ServicesModelBinder _modelBinder = new ServicesModelBinder();
|
||||
|
||||
/// <inheritdoc />
|
||||
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
|
|
@ -21,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
|
|||
if (context.BindingInfo.BindingSource != null &&
|
||||
context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Services))
|
||||
{
|
||||
return new ServicesModelBinder();
|
||||
return _modelBinder;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Reference in New Issue