获取自主机模式下的电流owin背景 [英] Get current owin context in self host mode

查看:212
本文介绍了获取自主机模式下的电流owin背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行我的应用程序,它提供对IIS和.NET CLR自我主机模式既一些的ASP.NET Web API服务。
我开发了基于OWIN我的ASP.NET Web API服务,它是在两台主机上正常工作。
现在我需要的是这样的:

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;
                }
            }
        }
    }

要获得当前owin情况下,每当我需要在我的应用程序。

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

我的code是工作的罚款IIS上,但我应该怎么做。NET自主机模式?

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

推荐答案

首先,我已经纠正我的问题。

First, I've to correct my question.

HttpContext.Current是在基于ASP.NET和IIS集成无pipeline.But我们asp.net不能使用这个类的任何地方,甚至在IIS集成的管线应用程序中提供。

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.

答:

1 - 你需要的地方IOwinContext,你已经得到它,使用依赖注入,例如通过构造函数注入。

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

2 - 一切都配置基于Owin工作,SignalR是Owin只是根据,而是用网页API&安培; owin在一起,并使用南希因为如果任何服务器端的看法。相反书面方式IIS或ASP.NET处理程序和模块,开发owin中间件。

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使用Autofac.Owin&安培; AutoFac.WebApi&安培; AutoFac.WebApi.Owin&安培; Autofac.SignalR,你可以设置依赖注入横跨你的应用程序已经全部owin中间件的工作。

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将实例网页API控制器,​​signalr集线器和owin中间件,它会通过IOwinContext实例类你想使用构造器注入。

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.

我的测试是确定在Owin IIS /赫利俄斯(不asp.net),Owin SelfHost甚至Owin测试服务器。

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

此方法类似于asp.net vNext。您可以轻松地迁移应用程序到ASP.NET vNext,当它投入生产。

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

这篇关于获取自主机模式下的电流owin背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆