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

Max resolve stack depth is now configurable via extension method #1115

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Autofac/Core/Resolving/CircularDependencyDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ namespace Autofac.Core.Resolving
internal class CircularDependencyDetector
{
/// <summary>
/// Catch circular dependencies that are triggered by post-resolve processing (e.g. 'OnActivated').
/// Gets or sets the value that helps catch circular dependencies that are triggered by post-resolve processing (e.g. 'OnActivated').
/// </summary>
[SuppressMessage("SA1306", "SA1306", Justification = "Changed const to static on temporary basis until we can solve circular dependencies without a limit.")]
private static int MaxResolveDepth = 50;
internal static int MaxResolveDepth { get; set; } = DefaultMaxResolveDepth;

internal const int DefaultMaxResolveDepth = 50;

private static string CreateDependencyGraphTo(IComponentRegistration registration, Stack<InstanceLookup> activationStack)
{
Expand All @@ -60,7 +62,7 @@ public static void CheckForCircularDependency(IComponentRegistration registratio
if (registration == null) throw new ArgumentNullException(nameof(registration));

if (callDepth > MaxResolveDepth)
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, CircularDependencyDetectorResources.MaxDepthExceeded, registration));
throw new DependencyResolutionException(string.Format(CultureInfo.CurrentCulture, CircularDependencyDetectorResources.MaxDepthExceeded, callDepth, MaxResolveDepth, registration));

// Checks for circular dependency
foreach (var a in activationStack)
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 @@ -121,6 +121,6 @@
<value>Circular component dependency detected: {0}.</value>
</data>
<data name="MaxDepthExceeded" xml:space="preserve">
<value>Probable circular dependency between factory-scoped components. Chain includes '{0}'</value>
<value>The call stack depth has reached {0}. This exceeded the threshold value {1} (you can set up it with WithMaxResolveStackDepth method). Usually, this means probable circular dependency between factory-scoped components. Chain includes '{2}'</value>
</data>
</root>
53 changes: 53 additions & 0 deletions src/Autofac/OptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// https://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using Autofac.Core.Resolving;

namespace Autofac
{
/// <summary>
/// Adds possibility to configure internal options.
/// </summary>
public static class OptionsExtensions
{
/// <summary>
/// Set the maximum resolve stack depth.
/// </summary>
/// <param name="builder">Container builder.</param>
/// <param name="maxStackResolveDepth">The maximum stack resolve depth.</param>
/// <returns>Container builder.</returns>
public static ContainerBuilder WithMaxResolveStackDepth(this ContainerBuilder builder, int maxStackResolveDepth)
{
if (maxStackResolveDepth < CircularDependencyDetector.DefaultMaxResolveDepth)
{
maxStackResolveDepth = CircularDependencyDetector.DefaultMaxResolveDepth;
}

CircularDependencyDetector.MaxResolveDepth = maxStackResolveDepth;

return builder;
}
}
}