0

I have a service that takes a dependency on HttpContextBase.

Ninject is injecting this for me already as it's set up in the MvcModule to return new HttpContextWrapper(HttpContext.Current) when HttpContextBase is requested

I want to use this service in Application_AuthenticateRequest, so i'm using property injection so that Ninject resolves it for me

When I try and access Request.UserHostAddress on the HttpContextBase I get a Value does not fall within the expected range exception

If I call HttpContext.Current.Request.UserHostAddress directly it works without problems

ExampleService.cs

public class ExampleService : IExampleService {
    HttpContextBase _contextBase;

    public ExampleService(HttpContextBase contextBase) {
        _contextBase = contextBase;
    }

    public void DoSomething() {
        var ip = HttpContext.Current.Request.UserHostAddress;  <== this works
        ip = _contextBase.Request.UserHostAddress;  <== this fails
    }
}

Global.asax

[Inject]
public IExampleService ExampleService { get; set; }

public void Application_AuthenticateRequest() {
    ExampleService.DoSomething();
}

I'm missing something here, but I can't see what

4

1 に答える 1