Skip to content

Commit

Permalink
Merge pull request #1359 from autofac/feature/exception-msg-links
Browse files Browse the repository at this point in the history
Add documentation links to common exceptions
  • Loading branch information
tillig authored Dec 18, 2022
2 parents 178aa24 + 0faf5c8 commit 0c79d7b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 66 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
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>
60 changes: 31 additions & 29 deletions src/Autofac/Core/Lifetime/MatchingScopeLifetimeResources.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -120,6 +120,8 @@
<data name="MatchingScopeNotFound" xml:space="preserve">
<value>No scope with a tag matching '{0}' is visible from the scope in which the instance was requested.

If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario). Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself.</value>
If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario). Under the web integration always request dependencies from the dependency resolver or the request lifetime scope, never from the container itself.

See https://autofac.rtfd.io/help/no-matching-scope for more info.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -118,6 +118,8 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Message" xml:space="preserve">
<value>The requested service '{0}' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.</value>
<value>The requested service '{0}' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

See https://autofac.rtfd.io/help/service-not-registered for more info.</value>
</data>
</root>
</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 0c79d7b

Please sign in to comment.