public static IApplicationBuilder UseZKWeb(this IApplicationBuilder app) { // It can't throw any exception otherwise application will get killed return(app.Use((coreContext, next) => Task.Run(() => { var context = new CoreHttpContextWrapper(coreContext); try { // Handle request Application.OnRequest(context); } catch (CoreHttpResponseEndException) { // Success } catch (Exception ex) { // Error if (ex is HttpException && ((HttpException)ex).StatusCode == 404) { // Try next middleware try { next().Wait(); return; } catch (Exception nextEx) { ex = nextEx; } } try { Application.OnError(context, ex); } catch (CoreHttpResponseEndException) { // Handle error success } catch (Exception) { // Handle error failed } } }))); }
/// <summary> /// Use zkweb middleware /// </summary> /// <typeparam name="TApplication">application type</typeparam> /// <param name="app">application builder</param> /// <param name="websiteRootDirectory">website root directory</param> /// <returns></returns> public static IApplicationBuilder UseZKWeb <TApplication>( this IApplicationBuilder app, string websiteRootDirectory) where TApplication : IApplication, new() { // set and initialize application Application.Initialize <TApplication>(websiteRootDirectory); Application.Ioc.RegisterMany <CoreWebsiteStopper>(ReuseType.Singleton); var lifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime)); Application.Ioc.RegisterInstance(lifetime); // set zkweb middleware // it can't throw any exception otherwise application will get killed return(app.Use((coreContext, next) => Task.Run(() => { var context = new CoreHttpContextWrapper(coreContext); try { // Handle request Application.OnRequest(context); } catch (CoreHttpResponseEndException) { // Success } catch (Exception ex) { // Error if (ex is HttpException && ((HttpException)ex).StatusCode == 404) { // Try next middleware try { next().Wait(); return; } catch (Exception nextEx) { ex = nextEx; } } try { Application.OnError(context, ex); } catch (CoreHttpResponseEndException) { // Handle error success } catch (Exception) { // Handle error failed } } }))); }
public static IApplicationBuilder UseZKWeb(this IApplicationBuilder app) { // It can't throw any exception otherwise application will get killed var hostingEnvironment = app.ApplicationServices.GetService <IHostingEnvironment>(); var isDevelopment = hostingEnvironment.IsDevelopment(); return(app.Use((coreContext, next) => Task.Run(() => { var context = new CoreHttpContextWrapper(coreContext); // Get application instance at first for reloading support IApplication instance; try { instance = Application.Instance; } catch (Exception ex) { // Initialize application failed coreContext.Response.StatusCode = 500; coreContext.Response.ContentType = "text/plain;charset=utf-8"; using (var writer = new StreamWriter(coreContext.Response.Body)) { if (isDevelopment) { writer.Write(ex.ToDetailedString()); } else { writer.Write( "Internal error occurs during application initialization, " + "please set ASPNETCORE_ENVIRONMENT to Development to view the error message, " + "or check the logs on server.\r\n"); } writer.Flush(); } return; } try { // Handle request instance.OnRequest(context); } catch (CoreHttpResponseEndException) { // Success } catch (Exception ex) { // Error if (ex is HttpException && ((HttpException)ex).StatusCode == 404) { // Try next middleware try { next().Wait(); return; } catch (Exception nextEx) { ex = nextEx; } } try { instance.OnError(context, ex); } catch (CoreHttpResponseEndException) { // Handle error success } catch (Exception) { // Handle error failed } } }))); }