0

I have been happily using AutoFaq for a couple of years and take advantage of its ability to easily inject HttpRequestBase and HttpContextBase in the MVC pipeline. This makes mocking and decoupling a lot easier.

I am in the process of changing my data layer to ServiceStack and as part of wiring the default Funq DI mechanism to my different layers I can't figure out how to inject HttpRequestBase and HttpContextBase.

Is there a way to do this? I am looking for the container.Register() analog inside of AppHost.Configure(Func.Container container).

Thanks

t316
  • 1,149
  • 1
  • 15
  • 28

2 Answers2

2

ServiceStack doesn't allow registering runtime dependencies with its IOC, although as ServiceStack Services and Request pipeline only binds to the IRequest interface which can just inject a mocked IRequest directly on the service when its required, e.g:

var service = new MyService {
    Request = new MockHttpRequest()
};

var response = service.Get(new MyRequest { Id = 1 });

The Testing wiki shows other ways of testing ServiceStack services.

mythz
  • 141,670
  • 29
  • 246
  • 390
0

ServiceStack has it's own abstraction of the HttpContext and Request/Response. In v3.x, these are IRequestContext, IHttpRequest, IHttpResponse. This is to be implementation-independent of ASP.NET (console or Mono). It is recommended you use the abstractions instead of trying to use the underlying ASP.NET objects.

In your Service code, you may access them this way:

var httpReq = base.RequestContext.Get<IHttpRequest>();
var httpResp = base.RequestContext.Get<IHttpResponse>();

If you really need the real ASP.NET HttpContext, apparently you should be able to access it at IRequest.OriginalRequest. But if you are trying it the ServiceStack way, "don't do that".

More explanation of the Funq usage in v3 is here: https://github.com/ServiceStackV3/ServiceStackV3/wiki/The-IoC-container

Community
  • 1
  • 1
Raul Nohea Goodness
  • 2,549
  • 23
  • 24