Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-progress links in exception messages (plus misc exception msg improvements) #1281

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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