Skip to content

Commit

Permalink
Added Options.EnableAutoVerification with a value set to false by def…
Browse files Browse the repository at this point in the history
…ault. Fixes #555.
  • Loading branch information
dotnetjunkie committed Oct 1, 2019
1 parent 7a33739 commit 0dfae08
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/SimpleInjector/Container.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,28 @@ private void FlagContainerAsLocked()

this.locked = true;
}

if (this.Options.EnableAutoVerification && !this.IsVerifying && !this.SuccesfullyVerified)
{
try
{
this.Verify();
}
catch (InvalidOperationException ex)
{
// Verify throws an invalid operation exception. Here, we instead want to throw an
// ActivationException, as that would allow the same exception type to be thrown in that
// case, which is often what the user would expect.
throw new ActivationException(
StringResources.EnableAutoVerificationIsEnabled(ex.Message), ex);
}
catch (DiagnosticVerificationException ex)
{
// Same for DiagnosticVerificationExceptions.
throw new ActivationException(
StringResources.EnableAutoVerificationIsEnabled(ex.Message), ex);
}
}
}

private void ThrowContainerDisposedException()
Expand Down
6 changes: 5 additions & 1 deletion src/SimpleInjector/Container.Verification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ private void VerifyInternal(bool suppressLifestyleMismatchVerification)
// the first thread could dispose the verification scope, while the other thread is still using it.
lock (this.isVerifying)
{
// Flag verifying before locking the container, because LockContainer otherwise triggers
// verification again causing a stack overflow.
this.IsVerifying = true;

this.LockContainer();
bool original = this.Options.SuppressLifestyleMismatchVerification;
this.IsVerifying = true;

this.VerificationScope = new ContainerVerificationScope(this);

try
Expand Down
11 changes: 10 additions & 1 deletion src/SimpleInjector/ContainerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,21 @@ internal ContainerOptions(Container container)
/// <summary>
/// Gets or sets a value indicating whether the container should suppress checking for lifestyle
/// mismatches (see: https://simpleinjector.org/dialm) when a component is resolved. The default
/// is false.
/// is false. This setting will have no effect when <see cref="EnableAutoVerification"/> is true.
/// </summary>
/// <value>The value indicating whether the container should suppress checking for lifestyle
/// mismatches.</value>
public bool SuppressLifestyleMismatchVerification { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the container should automatically trigger verification
/// and diagnostics of its configuration when the first service is resolved (e.g. the first call to
/// GetInstance). The behavior is identical to calling <see cref="Container.Verify()">Verify()</see>
/// manually. The default is false.
/// </summary>
/// <value>The value indicating whether the container should automatically trigger verification.</value>
public bool EnableAutoVerification { get; set; }

/// <summary>Gets or sets a value indicating whether.
/// This method is deprecated. Changing its value will have no effect.</summary>
/// <value>The value indicating whether the container will return an empty collection.</value>
Expand Down
9 changes: 6 additions & 3 deletions src/SimpleInjector/InstanceProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public static InstanceProducer FromExpression(
"A property is not appropriate, because get instance could possibly be a heavy operation.")]
public object GetInstance()
{
// We must lock the container, because not locking could lead to race conditions.
this.Container.LockContainer();

this.CheckForCyclicDependencies();

object instance;
Expand Down Expand Up @@ -275,6 +278,9 @@ public object GetInstance()
/// <returns>An Expression.</returns>
public Expression BuildExpression()
{
// We must lock the container, because not locking could lead to race conditions.
this.Container.LockContainer();

this.CheckForCyclicDependencies();

try
Expand Down Expand Up @@ -524,9 +530,6 @@ private void Analyze()

private Expression BuildExpressionInternal()
{
// We must lock the container, because not locking could lead to race conditions.
this.Container.LockContainer();

var expression = this.Registration.BuildExpression();

if (expression == null)
Expand Down
8 changes: 8 additions & 0 deletions src/SimpleInjector/StringResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal static class StringResources
private const string CollectionsRegisterMethodName =
nameof(Container) + "." + nameof(Container.Collection) + "." + nameof(ContainerCollectionRegistrator.Register);

private const string EnableAutoVerificationPropertyName =
nameof(Container) + "." + nameof(Container.Options) + "." + nameof(ContainerOptions.EnableAutoVerification);

// Assembly.Location only exists in .NETStandard1.5 and up, .NET4.0 and PCL, but we only compile
// against .NETStandard1.0 and .NETStandard1.3. We don't want to add an extra build directly, solely
// for the Location property.
Expand Down Expand Up @@ -165,6 +168,11 @@ from error in errors
Environment.NewLine);
}

internal static string EnableAutoVerificationIsEnabled(string innerMessage) =>
innerMessage + $" Verification was triggered because {EnableAutoVerificationPropertyName} was " +
"enabled. To prevent the container from being verified on first resolve, set " +
$"{EnableAutoVerificationPropertyName} to false.";

internal static string ConfigurationInvalidCreatingInstanceFailed(
Type serviceType, Exception exception) =>
Format(
Expand Down

0 comments on commit 0dfae08

Please sign in to comment.