Get current owin context in self host mode
Asked Answered
A

2

14

I need to run my application which provides some ASP.NET Web API services on both IIS and .NET CLR self host modes. I developed my ASP.NET Web API services based on OWIN and it is working fine on both hosts. For now I need something like this:

public class OwinContextInfrastructure
    {
        public static IOwinContext Current
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.GetOwinContext();
                }
                else
                {
                    // What should I do here ?
                    return null;
                }
            }
        }
    }

to get current owin context whenever I need in my application.

My code is working fine on IIS, but what should I do in .NET Self Host mode ?

Appendectomy answered 5/10, 2014 at 8:44 Comment(0)
A
0

First, I've to correct my question.

HttpContext.Current is available in applications which are based on ASP.NET and integrated IIS pipeline.But We can't use this class without asp.net anywhere, even on IIS integrated pipeline.

Answer:

1- Anywhere you need IOwinContext, you've to get it, using dependency injection, for example by constructor injection.

2- Configure everything to work based on Owin, SignalR is Owin based only, but use Web Api & owin together, and use nancy for server side views if any. Instead of writting IIS or ASP.NET handlers and modules, develop owin middlewares.

3- Using Autofac.Owin & AutoFac.WebApi & AutoFac.WebApi.Owin & Autofac.SignalR, you can setup dependency injection working across all owin middlewares you've in your application.

4- Autofac will instantiate web api controllers, signalr hubs and owin middlewares, and it will pass IOwinContext instance to classes you want using constructor injection.

My tests are ok on Owin IIS/Helios (without asp.net) , Owin SelfHost and even Owin Test Server.

This approach is similar to asp.net vNext. You can easily migrate your app to asp.net vNext, when it is production ready.

Appendectomy answered 6/2, 2016 at 14:35 Comment(9)
Hi! I need to access too the IOwinContext for a self-hosted SignalR hub...can you show the code you used that worked please? After reading both answers here I am still not sure how to do it...Request is a property of a System.Web.Mvc.Controller which I can't use from my hub :(Waterworks
"Autofac.Owin" starts dependency injection using an Owin Middleware, it will continue to pass all per request objects including IOwinContext to other middlewares like Web API & SignalR and other middlewares including those which are developed by yours, but ASP.NET MVC is not a owin compatible middleware, You can't self host ASP.NET MVC, I get confused by your question /-:Appendectomy
Ok, Autofac is for mocking and testing, I am not in this context. I was hoping you would update your answer with some code, thanks anywayWaterworks
Autofac is a dependency injection library, dependency injection is useful in wide variety of scenarios, including but not limited to mocking and testing.Appendectomy
oh ok sorry, I thought it was like RhinoMock and such, anyway, some code would clearly be more helpful than comments :) cheersWaterworks
NP (: , tell me what do you exactly want, I try to help you somehow.Appendectomy
In fact I just remembered I found an answer to my initial question (#38808714). However I chose to switch from self-hosted to hosted in an MVC web app because of authentication limitationsWaterworks
How do you register IOwinContext in your container?Homologue
@Homologue Autofac-owin itself has a middleware for this. It creates a Autofac scope for every Owin request, and registers IOwinContext for that scope. You can have access to IOwinContext using dependency injection in classes which are registered in PerScope mode (it doesn't work on singleton registrations) Bit framework (a full stack framework to develop api/mobile/web apps) is a good sample of this which configures Owin on top of asp.net core and extends It's dependency injection to all owin/asp.net core components. docs.bit-framework.comAppendectomy
L
16

You can use Request.GetOwinContext() for both web-hosting and self-hosting. GetOwinContext is an extension method for HttpRequestMessage and is defined in the System.Web.Http.Owin.dll assembly.

UPDATE

I have answered your original question, which is how to get OWIN context in both web-hosting and self-hosting. Now, through your additional question in the comment, you have significantly broadened the scope of your question. There is a fundamental problem though. IOwinContext is not a OWIN thing, it is a Katana thing. You cannot expect any framework hosted on OWIN to provide a context in the form of IOwinContext. ASP.NET Web API does but not every framework is supposed to. IOwinContext is an abstraction over OWIN environment dictionary and this dictionary will be available to any OWIN middleware. However, by working on top of a framework, you no longer can access the OWIN environment directly but only through how that specific framework has decided to expose the context.

For Nancy, you have to use NancyContext to get to the Items dictionary and look for the value corresponding to the key "OWIN_REQUEST_ENVIRONMENT". For SignalR, Environment property of IRequest gives you access to OWIN environment. Once you have the OWIN environment, you can create a new OwinContext using the environment.

Limnology answered 6/10, 2014 at 4:26 Comment(2)
Yes, you're right, but what about signal r ? nancy and so on ? I need something globally for all frameworks which are already hosted on owin.Appendectomy
What you said is right, but I found a well designed unified solution.Appendectomy
A
0

First, I've to correct my question.

HttpContext.Current is available in applications which are based on ASP.NET and integrated IIS pipeline.But We can't use this class without asp.net anywhere, even on IIS integrated pipeline.

Answer:

1- Anywhere you need IOwinContext, you've to get it, using dependency injection, for example by constructor injection.

2- Configure everything to work based on Owin, SignalR is Owin based only, but use Web Api & owin together, and use nancy for server side views if any. Instead of writting IIS or ASP.NET handlers and modules, develop owin middlewares.

3- Using Autofac.Owin & AutoFac.WebApi & AutoFac.WebApi.Owin & Autofac.SignalR, you can setup dependency injection working across all owin middlewares you've in your application.

4- Autofac will instantiate web api controllers, signalr hubs and owin middlewares, and it will pass IOwinContext instance to classes you want using constructor injection.

My tests are ok on Owin IIS/Helios (without asp.net) , Owin SelfHost and even Owin Test Server.

This approach is similar to asp.net vNext. You can easily migrate your app to asp.net vNext, when it is production ready.

Appendectomy answered 6/2, 2016 at 14:35 Comment(9)
Hi! I need to access too the IOwinContext for a self-hosted SignalR hub...can you show the code you used that worked please? After reading both answers here I am still not sure how to do it...Request is a property of a System.Web.Mvc.Controller which I can't use from my hub :(Waterworks
"Autofac.Owin" starts dependency injection using an Owin Middleware, it will continue to pass all per request objects including IOwinContext to other middlewares like Web API & SignalR and other middlewares including those which are developed by yours, but ASP.NET MVC is not a owin compatible middleware, You can't self host ASP.NET MVC, I get confused by your question /-:Appendectomy
Ok, Autofac is for mocking and testing, I am not in this context. I was hoping you would update your answer with some code, thanks anywayWaterworks
Autofac is a dependency injection library, dependency injection is useful in wide variety of scenarios, including but not limited to mocking and testing.Appendectomy
oh ok sorry, I thought it was like RhinoMock and such, anyway, some code would clearly be more helpful than comments :) cheersWaterworks
NP (: , tell me what do you exactly want, I try to help you somehow.Appendectomy
In fact I just remembered I found an answer to my initial question (#38808714). However I chose to switch from self-hosted to hosted in an MVC web app because of authentication limitationsWaterworks
How do you register IOwinContext in your container?Homologue
@Homologue Autofac-owin itself has a middleware for this. It creates a Autofac scope for every Owin request, and registers IOwinContext for that scope. You can have access to IOwinContext using dependency injection in classes which are registered in PerScope mode (it doesn't work on singleton registrations) Bit framework (a full stack framework to develop api/mobile/web apps) is a good sample of this which configures Owin on top of asp.net core and extends It's dependency injection to all owin/asp.net core components. docs.bit-framework.comAppendectomy

© 2022 - 2024 — McMap. All rights reserved.