kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

For Web Api integration, add support for injecting the current request #447

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It would be useful to allow a component to have the current HttpRequestMessage 
automatically available as a dependency. 

One place this can be used is for link generation.

public class OrderController : ApiController {
  public OrderController(IOrderViewModelAdapter adapter) {
  }
}

public class OrderViewModelAdapter {
  private UrlHelper _helper;

  public OrderViewModelAdapter(HttpRequestMessage request) {
    _helper = new UrlHelper(request);
  }

  public OrderVM Adapt(Order order) {
    //logic to add links (hypermedia) using _helper.Link()
  }

}

Original issue reported on code.google.com by glenn.bl...@gmail.com on 22 Jun 2013 at 8:06

GoogleCodeExporter commented 8 years ago
Here's a gist that shows how I can get injecting the request now. It works but 
it's ugly.

https://gist.github.com/glennblock/906f68420d6bd12d826c

Original comment by glenn.bl...@gmail.com on 22 Jun 2013 at 8:29

GoogleCodeExporter commented 8 years ago
Any reason for the wrapper type in the Gist? Your original example has the 
HttpRequestMessage being directly injected.

Original comment by alex.meyergleaves on 25 Jun 2013 at 1:24

GoogleCodeExporter commented 8 years ago
Don't worry, I see now. One is what you would like, and the other is what you 
had to do. :)

That is way harder than it should be. The dependency scopes really needed some 
more context provided to them Web API.

Original comment by alex.meyergleaves on 25 Jun 2013 at 1:53

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 60d369499963.

Original comment by alex.meyergleaves on 25 Jun 2013 at 2:50

GoogleCodeExporter commented 8 years ago
This one is now ready for the next release. Just call the new 
RegisterHttpRequestMessage method on the builder and provide the 
HttpConfiguration instance.

builder.RegisterHttpRequestMessage(config);

You can then resolve HttpRequestMessage in your dependencies.

Original comment by alex.meyergleaves on 25 Jun 2013 at 2:52

GoogleCodeExporter commented 8 years ago
Just tested, it works perfectly. Thanks Alex!

Original comment by glenn.bl...@gmail.com on 27 Aug 2013 at 11:27

GoogleCodeExporter commented 8 years ago
i used RegisterHttpRequestMessage(config)
why i get a exception when Resolve<HttpRequestMessage>

Original comment by czcz1024@gmail.com on 25 Dec 2013 at 9:38

GoogleCodeExporter commented 8 years ago
The RegisterHttpRequestMessage adds a DelegatingHandler to the Web API pipeline 
that updates the container with the HttpRequestMessage instance of the 
executing request. You will not be able to resolve the HttpRequestMessage 
outside of a request. When unit testing you can create your own 
HttpRequestMessage and pass that in directly or add it to the container if 
required.

Original comment by alex.meyergleaves on 28 Dec 2013 at 3:32

GoogleCodeExporter commented 8 years ago
alex,thanks for answer.
i realy resolve inside a request ,but there something more.
in a request,i call an other class,and in this class ,i use common service 
locator.
so i config both web api and common service locator;i want fix the 
lifetimescope in the delegate ServiceLocator.SetLocatorProvider,so i need 
return a new 
AutofacServiceLocator(request.GetDependencyScope().GetRequestLifetimeScope());so
 i need the request, how can i resolve it

Original comment by czcz1024@gmail.com on 9 Jan 2014 at 6:37

GoogleCodeExporter commented 8 years ago
I don't think this is possible. There just isn't a way to create a static 
reference to the current request. Your best bet would be to remove the service 
locator and wire everything up properly.

Original comment by alex.meyergleaves on 19 Jan 2014 at 2:40