Autofac System.ArgumentNullException : Value cannot be null. Parameter name: context

Muhammad Ali

I created autofac instance per matching lifetime scope and create child scope if parent scope already exist but i got exception.

Please see the code and stack trace below.

Code

public static class App {
   private static AsyncLocal<ILifetimeScope> _upperScope;

   public static AsyncLocal<int> Number = new AsyncLocal<int>();

   public static ILifetimeScope NewScope(IContainer container) {
        if (_upperScope?.Value != null)
           return _upperScope.Value.BeginLifetimeScope();

        _upperScope = new AsyncLocal<ILifetimeScope> {Value = container.BeginLifetimeScope("test")};
        _upperScope.Value.CurrentScopeEnding += (sender, args) => _upperScope.Value = null;
        return _upperScope.Value;
   }
}

[Fact]
public void Test1() {
     var containerBuilder = new ContainerBuilder();
     containerBuilder.RegisterType<ClassOne>().AsSelf().InstancePerMatchingLifetimeScope("test");
     var container = containerBuilder.Build();

     var tasks = new List<Task>();

     tasks.Add(Task.Run(() => {
         using (var scope = App.NewScope(container)) {
             scope.Resolve<ClassOne>();
         }
     }));

     tasks.Add(Task.Run(() => {
         using (var scope = App.NewScope(container)) {
             scope.Resolve<ClassOne>();
         }
     }));

     Task.WaitAll(tasks.ToArray());
}

Click here for stack trace

Cyril Durand

You get this error because you assigned the AsyncLocal<T> more than once instead of only once. You should instantiate it once and then assign the Value property multiple time, this property will be unique per thread.

ie :

private static AsyncLocal<ILifetimeScope> _upperScope = new AsyncLocal<ILifetimeScope>();

and then

_upperScope.Value = container.BeginLifetimeScope("test");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

System.ArgumentNullException: 'Value cannot be null. Parameter name: provider'

Ninject StandardKernel System.ArgumentNullException: value cannot be null. Parameter name: path1

Azure App Service custom authentication getting System.ArgumentNullException: "Value cannot be null" "Parameter name: provider"

Xamarin Forms OnConfigurationChanged - System.ArgumentNullException: Value cannot be null Parameter name: enumerable

System.ArgumentNullException: 'Value cannot be null. (Parameter 'connectionString')'

Winform: System.ArgumentNullException: 'Value cannot be null. (Parameter 'stream')'

ArgumentNullException: Value cannot be null. Parameter name: constructor

ArgumentNullException : Value cannot be null. Parameter name : viewData

ArgumentNullException: Value cannot be null. Parameter name: entity

Unity throw ArgumentNullException: Value cannot be null. Parameter name: collider

Getting an error in xamarin forms while inserting data: System.ArgumentNullException: 'Value cannot be null. Parameter name: cancel'

IdentityServer4 System.ArgumentNullException: Value cannot be null. Parameter name: type when trying to refresh token

C# ODataQueryOptions with EF6 navigation property filters. System.ArgumentNullException : Value cannot be null. Parameter name: type

ArgumentNullException: Value cannot be null. (Parameter 'items')

System.ArgumentNullException: 'Value cannot be null error

.Net BinaryFormater System.ArgumentNullException: Object Graph cannot be null. Parameter name: graph

CosmosDB System.ArgumentNullException: 'Value cannot be null. (Parameter 'authKeyOrResourceToken')' [SOLVED]

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') asp.net Core Docker-compose

Specflow - TearDown failed for test fixture - System.ArgumentNullException : Value cannot be null. (Parameter 'key')

System.ArgumentNullException: Value cannot be null. (Parameter 'configuration') in ASP.NET Core Redis Implementation

An unhandled exception was thrown by the application. System.ArgumentNullException: Value cannot be null. (Parameter 'uriString')

Error when get connection string: ArgumentNullException: Value cannot be null. Parameter name: connectionString

ContentDispositionHeaderValueIdentityExtensions.IsFileDisposition thows ArgumentNullException: Value cannot be null Parameter name: header

ReportViewer 15.0.0 Renerding: Sys.ArgumentNullException: Value cannot be null. Parameter name: element

System.ArgumentNullException: 'Value cannot be null. Arg_ParamName_Name'

Unhandled Exception: System.ArgumentNullException: Value cannot be null

System.ArgumentNullException: Value cannot be null - Umbraco HTTPContext on save and publish

System.ArgumentNullException : Value cannot be null in unit test - C#

System.ArgumentNullException: Value cannot be null. Paramete