Skip to content

Commit 6654c7f

Browse files
authored
Merge pull request #1362 from autofac/feature/1360
Fix #1360: Unify the "no constructors found" reporting
2 parents 0c79d7b + 81ef9fd commit 6654c7f

File tree

11 files changed

+137
-146
lines changed

11 files changed

+137
-146
lines changed

bench/Autofac.BenchmarkProfiling/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ static void Main(string[] args)
8484

8585
// Workload method is generated differently when BenchmarkDotNet actually runs; we'll need to wrap it in the set of parameters.
8686
// It's way slower than they way they do it, but it should still give us good profiler results.
87-
Action<int> workloadAction = (repeat) =>
87+
void workloadAction(int repeat)
8888
{
8989
while (repeat > 0)
9090
{
9191
selectedCase.Descriptor.WorkloadMethod.Invoke(benchInstance, selectedCase.Parameters.Items.Select(x => x.Value).ToArray());
9292
repeat--;
9393
}
94-
};
94+
}
9595

9696
setupAction.InvokeSingle();
9797

src/Autofac/Core/Activators/Reflection/DefaultConstructorFinder.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@ public ConstructorInfo[] FindConstructors(Type targetType)
4545

4646
private static ConstructorInfo[] GetDefaultPublicConstructors(Type type)
4747
{
48-
var retval = ReflectionCacheSet.Shared.Internal.DefaultPublicConstructors
48+
return ReflectionCacheSet.Shared.Internal.DefaultPublicConstructors
4949
.GetOrAdd(type, t => t.GetDeclaredPublicConstructors());
50-
51-
if (retval.Length == 0)
52-
{
53-
throw new NoConstructorsFoundException(type);
54-
}
55-
56-
return retval;
5750
}
5851
}

src/Autofac/Core/Activators/Reflection/NoConstructorsFoundException.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,82 @@ public class NoConstructorsFoundException : Exception
1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="NoConstructorsFoundException"/> class.
1515
/// </summary>
16-
/// <param name="offendingType">The <see cref="System.Type"/> whose constructor was not found.</param>
17-
public NoConstructorsFoundException(Type offendingType)
18-
: this(offendingType, FormatMessage(offendingType))
16+
/// <param name="offendingType">The <see cref="Type"/> whose constructor was not found.</param>
17+
/// <param name="constructorFinder">The <see cref="IConstructorFinder"/> that was used to scan for the constructors.</param>
18+
public NoConstructorsFoundException(Type offendingType, IConstructorFinder constructorFinder)
19+
: this(offendingType, constructorFinder, FormatMessage(offendingType, constructorFinder))
1920
{
2021
}
2122

2223
/// <summary>
2324
/// Initializes a new instance of the <see cref="NoConstructorsFoundException"/> class.
2425
/// </summary>
25-
/// <param name="offendingType">The <see cref="System.Type"/> whose constructor was not found.</param>
26+
/// <param name="offendingType">The <see cref="Type"/> whose constructor was not found.</param>
27+
/// <param name="constructorFinder">The <see cref="IConstructorFinder"/> that was used to scan for the constructors.</param>
2628
/// <param name="message">Exception message.</param>
27-
public NoConstructorsFoundException(Type offendingType, string message)
29+
public NoConstructorsFoundException(Type offendingType, IConstructorFinder constructorFinder, string message)
2830
: base(message)
2931
{
3032
OffendingType = offendingType ?? throw new ArgumentNullException(nameof(offendingType));
33+
ConstructorFinder = constructorFinder ?? throw new ArgumentNullException(nameof(constructorFinder));
3134
}
3235

3336
/// <summary>
3437
/// Initializes a new instance of the <see cref="NoConstructorsFoundException"/> class.
3538
/// </summary>
36-
/// <param name="offendingType">The <see cref="System.Type"/> whose constructor was not found.</param>
39+
/// <param name="offendingType">The <see cref="Type"/> whose constructor was not found.</param>
40+
/// <param name="constructorFinder">The <see cref="IConstructorFinder"/> that was used to scan for the constructors.</param>
3741
/// <param name="innerException">The inner exception.</param>
38-
public NoConstructorsFoundException(Type offendingType, Exception innerException)
39-
: this(offendingType, FormatMessage(offendingType), innerException)
42+
public NoConstructorsFoundException(Type offendingType, IConstructorFinder constructorFinder, Exception innerException)
43+
: this(offendingType, constructorFinder, FormatMessage(offendingType, constructorFinder), innerException)
4044
{
4145
}
4246

4347
/// <summary>
4448
/// Initializes a new instance of the <see cref="NoConstructorsFoundException"/> class.
4549
/// </summary>
46-
/// <param name="offendingType">The <see cref="System.Type"/> whose constructor was not found.</param>
50+
/// <param name="offendingType">The <see cref="Type"/> whose constructor was not found.</param>
51+
/// <param name="constructorFinder">The <see cref="IConstructorFinder"/> that was used to scan for the constructors.</param>
4752
/// <param name="message">Exception message.</param>
4853
/// <param name="innerException">The inner exception.</param>
49-
public NoConstructorsFoundException(Type offendingType, string message, Exception innerException)
54+
public NoConstructorsFoundException(Type offendingType, IConstructorFinder constructorFinder, string message, Exception innerException)
5055
: base(message, innerException)
5156
{
5257
OffendingType = offendingType ?? throw new ArgumentNullException(nameof(offendingType));
58+
ConstructorFinder = constructorFinder ?? throw new ArgumentNullException(nameof(constructorFinder));
5359
}
5460

61+
/// <summary>
62+
/// Gets the finder used when locating constructors.
63+
/// </summary>
64+
/// <value>
65+
/// An <see cref="IConstructorFinder"/> that was used when scanning the
66+
/// <see cref="OffendingType"/> to find constructors.
67+
/// </value>
68+
public IConstructorFinder ConstructorFinder { get; private set; }
69+
5570
/// <summary>
5671
/// Gets the type without found constructors.
5772
/// </summary>
5873
/// <value>
59-
/// A <see cref="System.Type"/> that was processed by an <see cref="IConstructorFinder"/>
60-
/// or similar mechanism and was determined to have no available constructors.
74+
/// A <see cref="Type"/> that was processed by the
75+
/// <see cref="ConstructorFinder"/> and was determined to have no available
76+
/// constructors.
6177
/// </value>
6278
public Type OffendingType { get; private set; }
6379

64-
private static string FormatMessage(Type offendingType)
80+
private static string FormatMessage(Type offendingType, IConstructorFinder constructorFinder)
6581
{
6682
if (offendingType == null)
6783
{
6884
throw new ArgumentNullException(nameof(offendingType));
6985
}
7086

71-
return string.Format(CultureInfo.CurrentCulture, NoConstructorsFoundExceptionResources.Message, offendingType.FullName);
87+
if (constructorFinder == null)
88+
{
89+
throw new ArgumentNullException(nameof(constructorFinder));
90+
}
91+
92+
return string.Format(CultureInfo.CurrentCulture, NoConstructorsFoundExceptionResources.Message, offendingType.FullName, constructorFinder.GetType().FullName);
7293
}
7394
}

src/Autofac/Core/Activators/Reflection/NoConstructorsFoundExceptionResources.resx

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
3+
<!--
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -118,6 +118,6 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="Message" xml:space="preserve">
121-
<value>No accessible constructors were found for the type '{0}'.</value>
121+
<value>No constructors on type '{0}' can be found with the constructor finder '{1}'.</value>
122122
</data>
123-
</root>
123+
</root>

src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void ConfigurePipeline(IComponentRegistryServices componentRegistryServic
8383

8484
if (availableConstructors.Length == 0)
8585
{
86-
throw new NoConstructorsFoundException(_implementationType, string.Format(CultureInfo.CurrentCulture, ReflectionActivatorResources.NoConstructorsAvailable, _implementationType, ConstructorFinder));
86+
throw new NoConstructorsFoundException(_implementationType, ConstructorFinder);
8787
}
8888

8989
var binders = new ConstructorBinder[availableConstructors.Length];
@@ -131,7 +131,7 @@ private void UseSingleConstructorActivation(IResolvePipelineBuilder pipelineBuil
131131
// This is not going to happen, because there is only 1 constructor, that constructor has no parameters,
132132
// so there are no conditions under which GetConstructorInvoker will return null in this path.
133133
// Throw an error here just in case (and to satisfy nullability checks).
134-
throw new NoConstructorsFoundException(_implementationType, string.Format(CultureInfo.CurrentCulture, ReflectionActivatorResources.NoConstructorsAvailable, _implementationType, ConstructorFinder));
134+
throw new NoConstructorsFoundException(_implementationType, ConstructorFinder);
135135
}
136136

137137
// If there are no arguments to the constructor, bypass all argument binding and pre-bind the constructor.

src/Autofac/Core/Activators/Reflection/ReflectionActivatorResources.resx

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3-
<!--
4-
Microsoft ResX Schema
5-
3+
<!--
4+
Microsoft ResX Schema
5+
66
Version 2.0
7-
8-
The primary goals of this format is to allow a simple XML format
9-
that is mostly human readable. The generation and parsing of the
10-
various data types are done through the TypeConverter classes
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
1111
associated with the data types.
12-
12+
1313
Example:
14-
14+
1515
... ado.net/XML headers & schema ...
1616
<resheader name="resmimetype">text/microsoft-resx</resheader>
1717
<resheader name="version">2.0</resheader>
@@ -26,36 +26,36 @@
2626
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
2727
<comment>This is a comment</comment>
2828
</data>
29-
30-
There are any number of "resheader" rows that contain simple
29+
30+
There are any number of "resheader" rows that contain simple
3131
name/value pairs.
32-
33-
Each data row contains a name, and value. The row also contains a
34-
type or mimetype. Type corresponds to a .NET class that support
35-
text/value conversion through the TypeConverter architecture.
36-
Classes that don't support this are serialized and stored with the
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
3737
mimetype set.
38-
39-
The mimetype is used for serialized objects, and tells the
40-
ResXResourceReader how to depersist the object. This is currently not
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
4141
extensible. For a given mimetype the value must be set accordingly:
42-
43-
Note - application/x-microsoft.net.object.binary.base64 is the format
44-
that the ResXResourceWriter will generate, however the reader can
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
4545
read any of the formats listed below.
46-
46+
4747
mimetype: application/x-microsoft.net.object.binary.base64
48-
value : The object must be serialized with
48+
value : The object must be serialized with
4949
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
5050
: and then encoded with base64 encoding.
51-
51+
5252
mimetype: application/x-microsoft.net.object.soap.base64
53-
value : The object must be serialized with
53+
value : The object must be serialized with
5454
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
5555
: and then encoded with base64 encoding.
5656
5757
mimetype: application/x-microsoft.net.object.bytearray.base64
58-
value : The object must be serialized into a byte array
58+
value : The object must be serialized into a byte array
5959
: using a System.ComponentModel.TypeConverter
6060
: and then encoded with base64 encoding.
6161
-->
@@ -120,9 +120,6 @@
120120
<data name="ConstructorSelectorCannotSelectAnInvalidBinding" xml:space="preserve">
121121
<value>The constructor selector provided by '{0}' selected a binding that cannot be instantiated. Selectors must return a constructor where 'CanInstantiate' is true.</value>
122122
</data>
123-
<data name="NoConstructorsAvailable" xml:space="preserve">
124-
<value>No constructors on type '{0}' can be found with the constructor finder '{1}'.</value>
125-
</data>
126123
<data name="NoConstructorsBindable" xml:space="preserve">
127124
<value>None of the constructors found with '{0}' on type '{1}' can be invoked with the available services and parameters:{2}</value>
128125
</data>

src/Autofac/Features/OpenGenerics/OpenGenericDecoratorRegistrationSource.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
6464
throw new ArgumentNullException(nameof(registrationAccessor));
6565
}
6666

67-
if (OpenGenericServiceBinder.TryBindOpenGenericService(service, _registrationData.Services, _activatorData.ImplementationType, out Type? constructedImplementationType, out Service[]? services))
67+
if (service is not IServiceWithType swt)
68+
{
69+
return Enumerable.Empty<IComponentRegistration>();
70+
}
71+
72+
if (OpenGenericServiceBinder.TryBindOpenGenericTypedService(swt, _registrationData.Services, _activatorData.ImplementationType, out Type? constructedImplementationType, out Service[]? services))
6873
{
69-
var swt = (IServiceWithType)service;
7074
var fromService = _activatorData.FromService.ChangeType(swt.ServiceType);
7175

7276
return registrationAccessor(fromService)

src/Autofac/Features/OpenGenerics/OpenGenericDelegateRegistrationSource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
4747
throw new ArgumentNullException(nameof(registrationAccessor));
4848
}
4949

50-
if (OpenGenericServiceBinder.TryBindOpenGenericDelegate(service, _registrationData.Services, _activatorData.Factory, out var constructedFactory, out Service[]? services))
50+
if (service is not IServiceWithType swt)
51+
{
52+
yield break;
53+
}
54+
55+
if (OpenGenericServiceBinder.TryBindOpenGenericDelegateService(swt, _registrationData.Services, _activatorData.Factory, out var constructedFactory, out Service[]? services))
5156
{
5257
// Pass the pipeline builder from the original registration to the 'CreateRegistration'.
5358
// So the original registration will contain all of the pipeline stages originally added, plus anything we want to add.

src/Autofac/Features/OpenGenerics/OpenGenericRegistrationSource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
5959
throw new ArgumentNullException(nameof(registrationAccessor));
6060
}
6161

62-
if (OpenGenericServiceBinder.TryBindOpenGenericService(service, _registrationData.Services, _activatorData.ImplementationType, out Type? constructedImplementationType, out Service[]? services))
62+
if (service is not IServiceWithType swt)
63+
{
64+
yield break;
65+
}
66+
67+
if (OpenGenericServiceBinder.TryBindOpenGenericTypedService(swt, _registrationData.Services, _activatorData.ImplementationType, out Type? constructedImplementationType, out Service[]? services))
6368
{
6469
// Pass the pipeline builder from the original registration to the 'CreateRegistration'.
6570
// So the original registration will contain all of the pipeline stages originally added, plus anything we want to add.

0 commit comments

Comments
 (0)