0
votes

I have ported an existing web api project - which has been run using IIS - to OWIN (selfhosted). I´m using dependency injection (unity) and have implemented a service which needs some information from the current request´s header (i.e. var x = HttpContext.Current.Request.Headers["xxx"]).

Since HttpContext is not available in OWIN (which makes sense) - how can I get the current request? Please keep in mind that I do need this information inside an injected service (not inside a controller and an OWIN middleware module - owincontext).

2
Did you find a solution for this? The answer of getting the headers in the controller does not solve the problem.Dennis Kiesel

2 Answers

2
votes

Your controller should inherit from ApiController, which has a Request property that will be populated for each request. So from within your controller action just use Request.Headers[...].

0
votes

Create a sample class like below

public class HeaderParser
{

    IDictionary<string, object>  _requestContext;
    IDictionary<string, string[]> _headers;
    public HeaderParser(IDictionary<string, object> requestContext)
    {
        _requestContext = requestContext;
        _headers = requestContext["owin.RequestHeaders"] as IDictionary<string, string[]>;
    }
    public string GetEmployeeNoFromHeader()
    {
        if (_headers != null && _headers.ContainsKey("X-EmployeeNo") && _headers["X-EmployeeNo"] != null && _headers["X-EmployeeNo"].Length > 0)
        {
            return _headers["X-EmployeeNo"][0];
        }
        else
        {
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("EMPLOYEE NO NOT AVAILABLE IN REQUEST");
            throw new HttpResponseException(response);
        }
    }
}

In the controller something like below should work

var owincontext = request.GetOwinContext().Environment;

var headerParser= new HeaderParser(owincontext); headerParser.GetEmployeeNoFromHeader()

What we have done is we have implemented interface IHttpControllerActivator.Create like below, so it runs for all controller class, The controller is generated by dependency injection windsor castle

public IHttpController Create(
        HttpRequestMessage request,
        HttpControllerDescriptor controllerDescriptor,
        Type controllerType)
    {
       var owincontext = request.GetOwinContext().Environment;

       var headerParser= new HeaderParser(owincontext);
       var logger = _dpendencyManager.Resolve(typeof(IPOSLogger)) as IPOSLogger;
       var executionContext = new ExecutionContext(logger, owincontext,headerParser.GetEmployeeNoFromHeader());
       var controller =
           (IHttpController)_dpendencyManager.Resolve(controllerType, new { context = executionContext });
       //var controller =
       //  (IHttpController)_dpendencyManager.Resolve(controllerType);
       request.RegisterForDispose(
           new Release(
               () => _dpendencyManager.Release(controller)));

        return controller;
    }