Throw an exception if configurationType to CreateConfigureDelegate is abstract/has no parameterless ctor
Addresses #5431
This commit is contained in:
parent
07a2f1de06
commit
576c0e6a65
|
|
@ -23,6 +23,12 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
throw new ArgumentNullException(nameof(configurationType));
|
||||
}
|
||||
|
||||
if (!HasParameterlessConstructor(configurationType.GetTypeInfo()))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
Resources.FormatMiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType(configurationType, nameof(configurationType)));
|
||||
}
|
||||
|
||||
var instance = Activator.CreateInstance(configurationType);
|
||||
var configureDelegateBuilder = GetConfigureDelegateBuilder(configurationType);
|
||||
return configureDelegateBuilder.Build(instance);
|
||||
|
|
@ -66,6 +72,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
return methodInfo;
|
||||
}
|
||||
|
||||
private static bool HasParameterlessConstructor(TypeInfo modelTypeInfo)
|
||||
{
|
||||
return !modelTypeInfo.IsAbstract && modelTypeInfo.GetConstructor(Type.EmptyTypes) != null;
|
||||
}
|
||||
|
||||
private class ConfigureBuilder
|
||||
{
|
||||
public ConfigureBuilder(MethodInfo configure)
|
||||
|
|
|
|||
|
|
@ -1306,6 +1306,22 @@ namespace Microsoft.AspNetCore.Mvc.Core
|
|||
return string.Format(CultureInfo.CurrentCulture, GetString("MiddlewareFilter_ServiceResolutionFail"), p0, p1, p2, p3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unable to create an instance of type '{0}'. The type specified in {1} must not be abstract and must have a parameterless constructor.
|
||||
/// </summary>
|
||||
internal static string MiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType
|
||||
{
|
||||
get { return GetString("MiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unable to create an instance of type '{0}'. The type specified in {1} must not be abstract and must have a parameterless constructor.
|
||||
/// </summary>
|
||||
internal static string FormatMiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType(object p0, object p1)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("MiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType"), p0, p1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An {0} cannot be created without a valid instance of {1}.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -383,4 +383,8 @@
|
|||
<value>A duplicate entry for library reference {0} was found. Please check that all package references in all projects use the same casing for the same package references.</value>
|
||||
<comment>{0} is the dependency name.</comment>
|
||||
</data>
|
||||
<data name="MiddlewareFilterConfigurationProvider_CreateConfigureDelegate_CannotCreateType" xml:space="preserve">
|
||||
<value>Unable to create an instance of type '{0}'. The type specified in {1} must not be abstract and must have a parameterless constructor.</value>
|
||||
<comment>0 is the type to configure. 1 is the name of the parameter, configurationType.</comment>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -13,6 +13,22 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
{
|
||||
public class MiddlewareFilterConfigurationProviderTest
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(typeof(AbstractType))]
|
||||
[InlineData(typeof(NoParameterlessConstructor))]
|
||||
[InlineData(typeof(IDisposable))]
|
||||
public void CreateConfigureDelegate_ThrowsIfTypeCannotBeInstantiated(Type configurationType)
|
||||
{
|
||||
// Arrange
|
||||
var provider = new MiddlewareFilterConfigurationProvider();
|
||||
|
||||
// Act
|
||||
var exception = Assert.Throws<InvalidOperationException>(() => provider.CreateConfigureDelegate(configurationType));
|
||||
|
||||
// Assert
|
||||
Assert.Equal($"Unable to create an instance of type '{configurationType}'. The type specified in configurationType must not be abstract and must have a parameterless constructor.", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidConfigure_DoesNotThrow()
|
||||
{
|
||||
|
|
@ -174,5 +190,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class AbstractType
|
||||
{
|
||||
}
|
||||
|
||||
private class NoParameterlessConstructor
|
||||
{
|
||||
public NoParameterlessConstructor(object a)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue