如何在ASP.NET 5中使用基于IAppBuilder的Owin中间件 [英] How to use IAppBuilder-based Owin Middleware in ASP.NET 5
问题描述
ASP.NET 5(aspnet vnext)像Katana一样是基于OWIN的,但是具有不同的抽象.显然IAppBuilder
已被IApplicationBuilder
代替.许多中间件库依赖于IAppBuilder
,并且尚未进行更新以支持ASP.NET 5
ASP.NET 5 (aspnet vnext) is OWIN based like Katana was, but has different abstractions. Notably IAppBuilder
has been replaced by IApplicationBuilder
. Many middleware libraries depend on IAppBuilder
and have not been updated to support ASP.NET 5
如何在APS.NET 5中间件中使用此OWIN中间件.两者都是基于OWIN的,所以应该有可能.
How can I use this OWIN middleware in APS.NET 5 middleware. Both are OWIN based so it should be possible.
Microsoft.AspNet.Builder.OwinExtensions
确实提供了UseOwin
方法,但是它基于低级OWIN签名,因此不能与需要IAppBuilder
的方法一起使用.
Microsoft.AspNet.Builder.OwinExtensions
does provide a UseOwin
method, but it is based on the low-level OWIN signatures so cannot be used with methods expecting IAppBuilder
.
推荐答案
您现在可以使用 AspNet.Hosting.Katana.Extensions
程序包.
you can now use the AspNet.Hosting.Katana.Extensions
package for that.
这里是一个稍有不同的版本,使用的是AppBuilder.DefaultApp
:
Here's a slightly different version, that uses AppBuilder.DefaultApp
:
public static IApplicationBuilder UseOwinAppBuilder(this IApplicationBuilder app, Action<IAppBuilder> configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
return app.UseOwin(setup => setup(next =>
{
var builder = new AppBuilder();
var lifetime = (IApplicationLifetime) app.ApplicationServices.GetService(typeof(IApplicationLifetime));
var properties = new AppProperties(builder.Properties);
properties.AppName = app.ApplicationServices.GetApplicationUniqueIdentifier();
properties.OnAppDisposing = lifetime.ApplicationStopping;
properties.DefaultApp = next;
configuration(builder);
return builder.Build<Func<IDictionary<string, object>, Task>>();
}));
}
请注意,引用Microsoft.Owin
会使您的应用与dnxcore50
(Core CLR)不兼容.
Note that referencing Microsoft.Owin
makes your app incompatible with dnxcore50
(Core CLR).
这篇关于如何在ASP.NET 5中使用基于IAppBuilder的Owin中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!