Inject WebAPI UrlHelper into service using Autofac

后端 未结 3 1087
遇见更好的自我
遇见更好的自我 2021-02-14 18:10

I have a service used by a few controllers in my WebAPI project. The service needs to generate URLs, so ideally it would get a UrlHelper via a constructor parameter

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 18:38

    Based on Darrel Miller's comment, I created the following:

    A simple container class to hold a reference to the "current" HttpRequestMessage

    public class CurrentRequest
    {
        public HttpRequestMessage Value { get; set; }
    }
    

    A message handler that will store the current request

    public class CurrentRequestHandler : DelegatingHandler
    {
        protected async override System.Threading.Tasks.Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            var scope = request.GetDependencyScope();
            var currentRequest = (CurrentRequest)scope.GetService(typeof(CurrentRequest));
            currentRequest.Value = request;
            return await base.SendAsync(request, cancellationToken);
        }
    }
    

    In Global.asax, when configuring WebAPI, add the message handler.

    GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new CurrentRequestHandler());
    

    Then, configure the Autofac container to let it construct UrlHelper, getting the current request from the CurrentRequest object.

    var builder = new ContainerBuilder();
    builder.RegisterType().InstancePerApiRequest();
    builder.Register(c => new UrlHelper(c.Resolve().Value));
    builder.RegisterType();
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    ...
    container = builder.Build();
    

    UrlHelper can then be injected into the MyService just like any other dependency.

    Thanks to Darrel for pointing me in the right direction.

提交回复
热议问题