Skip to content

Suggestion: Alternative solution for injecting HttpRequestMessage #28

@rexcfnghk

Description

@rexcfnghk

The current implementation of injecting HttpRequestMessage relies on updating the component registry

internal static void UpdateScopeWithHttpRequestMessage(HttpRequestMessage request)
{
    var scope = request.GetDependencyScope();
    var requestScope = scope.GetRequestLifetimeScope();
     if (requestScope == null) return;

     var registry = requestScope.ComponentRegistry;
     var builder = new ContainerBuilder();
     builder.Register(c => request).InstancePerRequest();
     builder.Update(registry);
}

https://github.com/autofac/Autofac.WebApi/blob/develop/src/Autofac.Integration.WebApi/CurrentRequestHandler.cs#L66

However this defeats the best practices in the documentation: the container should be considered immutable.

Also ContainerBuilder.Update is marked as obsolete in this commit autofac/Autofac@8a89e94

Should another approach be considered? Like the one Simple Injector is using, by capturing the HttpRequestMessage in a provider:
https://github.com/simpleinjector/SimpleInjector/blob/master/src/SimpleInjector.Integration.WebApi/SimpleInjectorWebApiExtensions.cs

I will be happy to help with a pull request if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions