Is a dependency registered as .InstancePerRequest() in autofac always available in an asynchronous continuation?

Viewed 133

In an Autofac config: I have registered a dependency to be .InstancePerRequest() and have part of my code that is either using the async keyword or .continueWith(). My question is: if the dependency is used in a continuation that is executed asynchronously, will the dependency only be disposed once the async code has been executed or could it happen before?

Specific scenario:

     // in IocConfig.cs
     ContainerBuilder.Register(c => Context.CreateFromToken(                                                                     
       c.Resolve<IUser>().Name,                                                                                                                      
      .As<Context>()
      .InstancePerRequest();

Then in one file I have this code:

    // elsewhere in the code for a specific request..
    client.PostAsync(new Uri(_context.GetSetting("SomeEndpoint")),requestContent)
    .ContinueWith(task =>
        {
            ErrorLog.Write(_context.CreateErrorEntry($"Request: sent {task.Result.StatusCode} result: {task.Result.ToJson()}", LogLevel.Debug));
        });

Will the _context here (which is .InstancePerRequest()) still be available in the .ContinueWith(), or can it be disposed?

1 Answers
Related