ASP.NET 5 (aspnet vnext) está basado en OWIN como Katana, pero tiene diferentes abstracciones. En particular, IAppBuilder ha sido reemplazado por IApplicationBuilder . Muchas bibliotecas de middleware dependen de IAppBuilder y no se han actualizado para admitir ASP.NET 5
¿Cómo puedo usar este middleware OWIN en el middleware APS.NET 5? Ambos están basados en OWIN, por lo que debería ser posible.
Microsoft.AspNet.Builder.OwinExtensions proporciona un método UseOwin , pero se basa en las firmas OWIN de bajo nivel, por lo que no se puede usar con métodos que esperan IAppBuilder .
Editar: ahora puede usar el paquete AspNet.Hosting.Katana.Extensions para eso.
Aquí hay una versión ligeramente diferente, que usa 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>>(); })); } Tenga en cuenta que hacer referencia a Microsoft.Owin hace que su aplicación sea incompatible con dnxcore50 (Core CLR).
La referencia frecuentemente citada de que los marcos son compatibles es un método de extensión creado por Thinktecture para admitir su IdentityServer3 en ASP.NET 5 .
Ese método es específico de IdentityServer y no admite que ningún middleware registrado posteriormente en la canalización de AspNet procese la solicitud (no llama al siguiente componente).
Esto adapta el método para abordar esas deficiencias:
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; } ); } ); } }Se puede utilizar de la siguiente manera:
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();