Skip to content

Commit

Permalink
Merge pull request #1281 from alistairjevans/docs-links-and-improvements
Browse files Browse the repository at this point in the history
In-progress links in exception messages (plus misc exception msg improvements)
  • Loading branch information
alistairjevans authored Oct 12, 2022
2 parents 0362f7e + 4fe918e commit 96c9139
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,24 @@ private string GetBindingFailureMessage(BoundConstructor[] constructorBindings)
reasons.Append(invalid.Description);
}

return string.Format(
CultureInfo.CurrentCulture,
ReflectionActivatorResources.NoConstructorsBindable,
ConstructorFinder,
_implementationType,
reasons);
if (ConstructorFinder is DefaultConstructorFinder)
{
// Simplify the text for the common default finder case (to make the message easier to understand).
return string.Format(
CultureInfo.CurrentCulture,
ReflectionActivatorResources.NoConstructorsBindableDefaultBinder,
_implementationType,
reasons);
}
else
{
return string.Format(
CultureInfo.CurrentCulture,
ReflectionActivatorResources.NoConstructorsBindable,
ConstructorFinder,
_implementationType,
reasons);
}
}

private void InjectProperties(object instance, IComponentContext context)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@
<data name="NoConstructorsBindable" xml:space="preserve">
<value>None of the constructors found with '{0}' on type '{1}' can be invoked with the available services and parameters:{2}</value>
</data>
</root>
<data name="NoConstructorsBindableDefaultBinder" xml:space="preserve">
<value>None of the constructors found on type '{0}' can be invoked with the available services and parameters:{1}

See https://autofac.rtfd.io/help/no-constructors-bindable for more info.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Reflection;
using Autofac.Core;
using Autofac.Core.Activators.Reflection;
using Autofac.Test.Scenarios.ConstructorSelection;
Expand Down Expand Up @@ -180,7 +181,7 @@ public void NonPublicConstructorsIgnored()
var dx = Assert.Throws<DependencyResolutionException>(() =>
invoker(Factory.CreateEmptyContainer(), Factory.NoParameters));

Assert.Contains(typeof(DefaultConstructorFinder).Name, dx.Message);
Assert.Contains(typeof(InternalDefaultConstructor).Name, dx.Message);
}

[Fact]
Expand Down Expand Up @@ -338,6 +339,20 @@ public void ConstructorSelectorCannotReturnInvalidBinding()
Assert.Throws<InvalidOperationException>(() => invoker(container, Factory.NoParameters));
}

[Fact]
public void CustomBinderNameIncludedInErrorMessage()
{
var target = Factory.CreateReflectionActivator(typeof(InternalDefaultConstructor), new SimpleConstructorFinder());

// Constructor finding happens at pipeline construction; not when the pipeline is invoked.
var invoker = target.GetPipelineInvoker(Factory.CreateEmptyComponentRegistry());

var dx = Assert.Throws<DependencyResolutionException>(() =>
invoker(Factory.CreateEmptyContainer(), Factory.NoParameters));

Assert.Contains(typeof(SimpleConstructorFinder).Name, dx.Message);
}

private class MisbehavingConstructorSelector : IConstructorSelector
{
public BoundConstructor SelectConstructorBinding(BoundConstructor[] constructorBindings, IEnumerable<Parameter> parameters)
Expand All @@ -346,6 +361,11 @@ public BoundConstructor SelectConstructorBinding(BoundConstructor[] constructorB
}
}

private class SimpleConstructorFinder : IConstructorFinder
{
public ConstructorInfo[] FindConstructors(Type targetType) => targetType.GetDeclaredPublicConstructors();
}

public class AcceptsIntParameter
{
public AcceptsIntParameter(int i)
Expand Down
10 changes: 10 additions & 0 deletions test/Autofac.Test/Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public static ReflectionActivator CreateReflectionActivator(Type implementation,
properties);
}

public static ReflectionActivator CreateReflectionActivator(Type implementation, IConstructorFinder customFinder)
{
return new ReflectionActivator(
implementation,
customFinder,
new MostParametersConstructorSelector(),
NoParameters,
NoProperties);
}

public static ReflectionActivator CreateReflectionActivator(Type implementation, IConstructorSelector customSelector)
{
return new ReflectionActivator(
Expand Down

0 comments on commit 96c9139

Please sign in to comment.