Code Monkey home page Code Monkey logo

Comments (9)

vazans avatar vazans commented on June 2, 2024 1

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.

ENikS avatar ENikS commented on June 2, 2024

@axelgenus could you help with this one?

from aspnet-webapi.

axelgenus avatar axelgenus commented on June 2, 2024

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.

vazans avatar vazans commented on June 2, 2024

@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.

ENikS avatar ENikS commented on June 2, 2024

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.

vazans avatar vazans commented on June 2, 2024

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.

ENikS avatar ENikS commented on June 2, 2024

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.

ENikS avatar ENikS commented on June 2, 2024

You could try this:

var Func<HttpRequestMessage> factory = () => (HttpRequestMessage)HttpContext.Current.Items["MS_HttpRequestMessage"];
container.RegisterType<HttpRequestMessage>(new DelegateInjectionFactory(factory));

from aspnet-webapi.

axelgenus avatar axelgenus commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.