Self hosted Web.Api - accessing HttpRequestMessage or dependency resolver in attribute
<p>I have a Web.Api application currently running under IIS which is being converted to a self hosted application using Owin.</p>

<p>I have a requirement to access the dependency resolver from within an attribute so I can access an instance of a service. (the app is using Unity for DI).</p>

<p>As I can't see a way to inject this service into the attribute (if there is then I'd love to hear about it), I currently grab the resolver from the <code>HttpRequestMessage</code> via <code>HttpContext.Current</code>:-</p>

<pre><code>HttpRequestMessage request = HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage;

var service = request.GetDependencyScope().GetService(typeof(IService)) as IService;
</code></pre>

<p>This works fine with the app running under IIS, but under Owin <code>HttpContext</code> is not present.</p>

<p>I came across <a href="https://github.com/neuecc/OwinRequestScopeContext" rel="nofollow noreferrer">OwinRequestScopeContext</a> which I configure in my app startup:-</p>

<pre><code>public void Configuration(IAppBuilder appBuilder)
{
    appBuilder.UseRequestScopeContext();

    \\...
</code></pre>

<p>And assign a reference to the request object from within a <code>DelegatingHandler</code></p>

<pre><code>protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
    OwinRequestScopeContext.Current.Items["HttpRequestMessage"] = request;

    \\...
</code></pre>

<p>But when I access <code>OwinRequestScopeContext</code> in my attribute, <code>OwinRequestScopeContext.Current.Items</code> is empty.</p>

<p>I seem to be having the same problem as <a href="https://stackoverflow.com/questions/29194836/passing-logical-call-context-from-owin-pipeline-to-webapi-controller">here</a> but the supplied answer doesn't seem to address my issue.</p>

<p>Can anyone tell me what I'm doing wrong with <code>OwinRequestScopeContext</code> and/or what is the simplest way I can gain access to the dependency resolver or HttpRequestMessage from within my attribute?</p>