ASP.NET 5 (aspnet vnext)像Katana一样基于OWIN,但具有不同的抽象。值得注意的是,IApplicationBuilder
已经取代了IAppBuilder
。许多中间件库依赖于IAppBuilder
,尚未更新为支持ASP.NET 5
如何在APS.NET 5中间件中使用该中间件。两者都是基于OWIN的,所以这应该是可能的。
Microsoft.AspNet.Builder.OwinExtensions
确实提供了UseOwin
方法,但它基于低级OWIN签名,因此不能与需要IAppBuilder
的方法一起使用。
发布于 2015-06-12 04:47:00
编辑:您现在可以使用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)不兼容。
发布于 2015-06-10 04:18:10
框架兼容的最常被引用的参考文献是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();
https://stackoverflow.com/questions/30742028
复制相似问题