|
1 | 1 | using Microsoft.AspNetCore.Http; |
2 | 2 | using Microsoft.Extensions.Logging; |
| 3 | +using Microsoft.Extensions.Options; |
3 | 4 | using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.Net; |
|
9 | 10 |
|
10 | 11 | namespace ConsoleAppFramework.WebHosting |
11 | 12 | { |
12 | | - internal class WebHostingInterceptor : IConsoleAppInterceptor |
13 | | - { |
14 | | - readonly IConsoleAppInterceptor innerInterceptor; |
15 | | - |
16 | | - public bool CompleteSuccessfully { get; private set; } |
17 | | - public string? ErrorMessage { get; private set; } |
18 | | - public Exception? Exception { get; private set; } |
19 | | - |
20 | | - public WebHostingInterceptor(IConsoleAppInterceptor innerInterceptor) |
21 | | - { |
22 | | - this.innerInterceptor = innerInterceptor; |
23 | | - } |
24 | | - |
25 | | - public ValueTask OnEngineBeginAsync(IServiceProvider serviceProvider, ILogger<ConsoleAppEngine> logger) |
26 | | - { |
27 | | - return innerInterceptor.OnEngineBeginAsync(serviceProvider, logger); |
28 | | - } |
29 | | - |
30 | | - public ValueTask OnMethodEndAsync(ConsoleAppContext context, string? errorMessageIfFailed, Exception? exceptionIfExists) |
31 | | - { |
32 | | - this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null); |
33 | | - this.ErrorMessage = errorMessageIfFailed; |
34 | | - this.Exception = exceptionIfExists; |
35 | | - return innerInterceptor.OnMethodEndAsync(context, errorMessageIfFailed, exceptionIfExists); |
36 | | - } |
37 | | - |
38 | | - public ValueTask OnMethodBeginAsync(ConsoleAppContext context) |
39 | | - { |
40 | | - return innerInterceptor.OnMethodBeginAsync(context); |
41 | | - } |
42 | | - |
43 | | - public ValueTask OnEngineCompleteAsync(IServiceProvider serviceProvider, ILogger<ConsoleAppEngine> logger) |
44 | | - { |
45 | | - return innerInterceptor.OnEngineCompleteAsync(serviceProvider, logger); |
46 | | - } |
47 | | - } |
| 13 | + // TODO: Add Interceptor. |
| 14 | + //internal class WebHostingInterceptor : IConsoleAppInterceptor |
| 15 | + //{ |
| 16 | + // readonly IConsoleAppInterceptor innerInterceptor; |
| 17 | + |
| 18 | + // public bool CompleteSuccessfully { get; private set; } |
| 19 | + // public string? ErrorMessage { get; private set; } |
| 20 | + // public Exception? Exception { get; private set; } |
| 21 | + |
| 22 | + // public WebHostingInterceptor(IConsoleAppInterceptor innerInterceptor) |
| 23 | + // { |
| 24 | + // this.innerInterceptor = innerInterceptor; |
| 25 | + // } |
| 26 | + |
| 27 | + // public ValueTask OnEngineBeginAsync(IServiceProvider serviceProvider, ILogger<ConsoleAppEngine> logger) |
| 28 | + // { |
| 29 | + // return innerInterceptor.OnEngineBeginAsync(serviceProvider, logger); |
| 30 | + // } |
| 31 | + |
| 32 | + // public ValueTask OnMethodEndAsync(ConsoleAppContext context, string? errorMessageIfFailed, Exception? exceptionIfExists) |
| 33 | + // { |
| 34 | + // this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null); |
| 35 | + // this.ErrorMessage = errorMessageIfFailed; |
| 36 | + // this.Exception = exceptionIfExists; |
| 37 | + // return innerInterceptor.OnMethodEndAsync(context, errorMessageIfFailed, exceptionIfExists); |
| 38 | + // } |
| 39 | + |
| 40 | + // public ValueTask OnMethodBeginAsync(ConsoleAppContext context) |
| 41 | + // { |
| 42 | + // return innerInterceptor.OnMethodBeginAsync(context); |
| 43 | + // } |
| 44 | + |
| 45 | + // public ValueTask OnEngineCompleteAsync(IServiceProvider serviceProvider, ILogger<ConsoleAppEngine> logger) |
| 46 | + // { |
| 47 | + // return innerInterceptor.OnEngineCompleteAsync(serviceProvider, logger); |
| 48 | + // } |
| 49 | + //} |
48 | 50 |
|
49 | 51 | internal class LogCollector : ILogger<ConsoleAppEngine> |
50 | 52 | { |
@@ -93,15 +95,17 @@ public class ConsoleAppFrameworkMiddleware |
93 | 95 | readonly IServiceProvider provider; |
94 | 96 | readonly ILogger<ConsoleAppEngine> logger; |
95 | 97 | readonly IConsoleAppInterceptor interceptor; |
| 98 | + readonly ConsoleAppFrameworkOptions options; |
96 | 99 |
|
97 | 100 | readonly Dictionary<string, MethodInfo> methodLookup; |
98 | 101 |
|
99 | | - public ConsoleAppFrameworkMiddleware(RequestDelegate next, ILogger<ConsoleAppEngine> logger, IConsoleAppInterceptor interceptor, IServiceProvider provider, TargetConsoleAppTypeCollection targetTypes) |
| 102 | + public ConsoleAppFrameworkMiddleware(RequestDelegate next, ILogger<ConsoleAppEngine> logger, IConsoleAppInterceptor interceptor, IServiceProvider provider, TargetConsoleAppTypeCollection targetTypes, IOptionsSnapshot<ConsoleAppFrameworkOptions> options) |
100 | 103 | { |
101 | 104 | this.next = next; |
102 | 105 | this.logger = logger; |
103 | 106 | this.interceptor = interceptor; |
104 | 107 | this.provider = provider; |
| 108 | + this.options = options.Value; |
105 | 109 | this.methodLookup = BuildMethodLookup(targetTypes); |
106 | 110 | } |
107 | 111 |
|
@@ -152,7 +156,7 @@ public async Task Invoke(HttpContext httpContext) |
152 | 156 | var hostingInterceptor = new WebHostingInterceptor(interceptor); |
153 | 157 | var collectLogger = new LogCollector(logger); |
154 | 158 |
|
155 | | - var engine = new ConsoleAppEngine(collectLogger, provider, hostingInterceptor, httpContext.RequestAborted); |
| 159 | + var engine = new ConsoleAppEngine(collectLogger, provider, options, httpContext.RequestAborted); |
156 | 160 | await engine.RunAsync(methodInfo.DeclaringType, methodInfo, args); |
157 | 161 |
|
158 | 162 | // out result |
|
0 commit comments