Comments (9)
Thanks @axelgenus , this is what I have been doing now but I think it breaks the IOC rule and does not mark a clear dependency between Logger and Request which is required, Thanks again for your input.
@ENikS , I think the Delegate Unity syntax should work which is what I was looking for, will test and let you know.
from aspnet-webapi.
@axelgenus could you help with this one?
from aspnet-webapi.
Did you tried using the static property HttpContext.Current from System.Web? It contains the data of the request.
PS; sorry for the later answer but I am quite busy in this period at work.
from aspnet-webapi.
@axelgenus Thanks for the reply, I tried this but not sure of the unity syntax to get this going, something like this should work in web api2 but I am not getting the syntax right, what am I missing?
container.RegisterType<HttpRequestMessage, (HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage)>(); // syntax error
essentially I want to bind a type resolution to a delegate, Any help Please.
from aspnet-webapi.
It looks like you are trying to register instance of HttpRequestMessage
stored in HttpContext.Current.Items["MS_HttpRequestMessage"]
.
If I understand you correctly you should use:
var instance = (HttpRequestMessage)HttpContext.Current.Items["MS_HttpRequestMessage"];
container.RegisterInstance<HttpRequestMessage)>(instance);
from aspnet-webapi.
Thanks @ENikS, I have tried this & does not work, Issue here we are registering a singleinstance to that type which would be null in appstart & even if I register somewhere in httppipeline, HTTPrequestmessage will resolve to the single instance that it got registered to & not to every request instance. Does my explanation make sense, if not I can explain more.
from aspnet-webapi.
I see your point. Unfortunately I do not know much about MVC. Perhaps if you ask on one of the MVC forums or stackoverflow it would be quicker.
from aspnet-webapi.
You could try this:
var Func<HttpRequestMessage> factory = () => (HttpRequestMessage)HttpContext.Current.Items["MS_HttpRequestMessage"];
container.RegisterType<HttpRequestMessage>(new DelegateInjectionFactory(factory));
from aspnet-webapi.
What I meant was that you should NOT use Unity for this since HttpContext.Current is static and unique for each request context.
Taking your example:
public class Logger: ILogger {
private readonly HttpRequest request;
public Logger()
{
this.request = HttpContext.Current.Request;
} }
from aspnet-webapi.
Related Issues (13)
- 未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项 HOT 6
- missing PerRequestLifetimeManager in Unity.AspNet.WebApi HOT 1
- My project is aspnet-webapi, the project needs upgrading to.Net Core, but you only support net47, net46 and net45, can.Net Core Support? HOT 9
- Access appsettings.json file from unity config Dependency HOT 2
- Unity.AspNet.Webapi v5.11.1 global asax question?
- Web API Dependency Injection with Unity error - No public constructor is available for type System.Web.Http.Dispatcher.IHttpControllerActivator HOT 8
- Dependency on Micrsoft.AspNet.WebApi.WebHost not always needed HOT 4
- Intermittent NullReference Exceptions when creating Controllers
- Unity.AspNet.WebApi, Version=5.0.2.17,Unity.Abstractions, Version=2.0.2.67 HOT 2
- Add example usage/getting started in readme file HOT 2
- Add custom filter provider HOT 1
- Why not support netcoreapp2.0 and netstandard2.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnet-webapi.