ASP.NET5中如何使用基于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
如何在 APS.NET 5 中间件中使用这个 OWIN 中间件。两者都是基于 OWIN 的,因此应该是可能的。
Microsoft.AspNet.Builder.OwinExtensions
确实提供了 UseOwin
方法,但它基于低级 OWIN 签名,因此不能与期望 IAppBuilder
.
的方法一起使用
框架兼容的经常被引用的参考文献是 an extension method build by Thinktecture for supporting their IdentityServer3 on ASP.NET 5。
该方法特定于 IdentityServer,不支持由稍后在 AspNet 管道中注册的任何中间件处理的请求(它不调用下一个组件)。
这调整了方法以解决这些缺点:
internal static class IApplicationBuilderExtensions
{
public static void UseOwin(
this IApplicationBuilder app,
Action<IAppBuilder> owinConfiguration )
{
app.UseOwin(
addToPipeline =>
{
addToPipeline(
next =>
{
var builder = new AppBuilder();
owinConfiguration( builder );
builder.Run( ctx => next( ctx.Environment ) );
Func<IDictionary<string, object>, Task> appFunc =
(Func<IDictionary<string, object>, Task>)
builder.Build( typeof( Func<IDictionary<string, object>, Task> ) );
return appFunc;
} );
} );
}
}
可以这样使用:
app.UseOwin(
owin =>
{
// Arbitrary IAppBuilder registrations can be placed in this block
// For example, this extension can be provided by
// NWebsec.Owin or Thinktecture.IdentityServer3
owin.UseHsts();
} );
// ASP.NET 5 components, like MVC 6, will still process the request
// (assuming the request was not handled by earlier middleware)
app.UseMvc();
编辑:您现在可以使用 AspNet.Hosting.Katana.Extensions
package。
这是一个略有不同的版本,它使用 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) 不兼容。
ASP.NET 5 (aspnet vnext) 与 Katana 一样基于 OWIN,但具有不同的抽象。值得注意的是 IAppBuilder
已被 IApplicationBuilder
取代。许多中间件库依赖于 IAppBuilder
并且尚未更新以支持 ASP.NET 5
如何在 APS.NET 5 中间件中使用这个 OWIN 中间件。两者都是基于 OWIN 的,因此应该是可能的。
Microsoft.AspNet.Builder.OwinExtensions
确实提供了 UseOwin
方法,但它基于低级 OWIN 签名,因此不能与期望 IAppBuilder
.
框架兼容的经常被引用的参考文献是 an extension method build by Thinktecture for supporting their IdentityServer3 on ASP.NET 5。
该方法特定于 IdentityServer,不支持由稍后在 AspNet 管道中注册的任何中间件处理的请求(它不调用下一个组件)。
这调整了方法以解决这些缺点:
internal static class IApplicationBuilderExtensions
{
public static void UseOwin(
this IApplicationBuilder app,
Action<IAppBuilder> owinConfiguration )
{
app.UseOwin(
addToPipeline =>
{
addToPipeline(
next =>
{
var builder = new AppBuilder();
owinConfiguration( builder );
builder.Run( ctx => next( ctx.Environment ) );
Func<IDictionary<string, object>, Task> appFunc =
(Func<IDictionary<string, object>, Task>)
builder.Build( typeof( Func<IDictionary<string, object>, Task> ) );
return appFunc;
} );
} );
}
}
可以这样使用:
app.UseOwin(
owin =>
{
// Arbitrary IAppBuilder registrations can be placed in this block
// For example, this extension can be provided by
// NWebsec.Owin or Thinktecture.IdentityServer3
owin.UseHsts();
} );
// ASP.NET 5 components, like MVC 6, will still process the request
// (assuming the request was not handled by earlier middleware)
app.UseMvc();
编辑:您现在可以使用 AspNet.Hosting.Katana.Extensions
package。
这是一个略有不同的版本,它使用 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) 不兼容。