33using System . Linq ;
44using System . Threading ;
55using System . Threading . Tasks ;
6- using Microsoft . AspNetCore . Hosting . Internal ;
76using Microsoft . AspNetCore . Hosting . Server . Features ;
8- using Microsoft . AspNetCore . Server . Kestrel ;
97using Microsoft . Extensions . Logging ;
108using Microsoft . Extensions . Options ;
119using static KestrelPureOwin . Constants ;
1210
1311namespace KestrelPureOwin
1412{
13+ using Microsoft . AspNetCore . Hosting . Internal ;
14+ using Microsoft . AspNetCore . Server . Kestrel . Core ;
15+ using Microsoft . AspNetCore . Server . Kestrel . Transport . Libuv ;
1516 using AppFunc = Func < IDictionary < string , object > , Task > ;
1617
1718 using MidFunc = Func <
@@ -48,38 +49,24 @@ public KestrelOwinServer(KestrelServerOptions options)
4849 }
4950
5051 var wrappedOptions = Options . Create ( options ) ;
51-
52- var lifetime = new ApplicationLifetime ( ) ;
53-
52+
5453 var loggerFactory = new LoggerFactory ( ) ;
54+ loggerFactory . AddProvider ( new ConsoleLogProvider ( ) ) ;
5555
56- Server = new KestrelServer ( wrappedOptions , lifetime , loggerFactory ) ;
56+ var transport = new LibuvTransportFactory (
57+ Options . Create ( new LibuvTransportOptions ( ) ) ,
58+ new ApplicationLifetime ( loggerFactory . CreateLogger < ApplicationLifetime > ( ) ) ,
59+ loggerFactory ) ;
60+
61+ Server = new KestrelServer ( wrappedOptions , transport , loggerFactory ) ;
5762 }
5863
5964 private static KestrelServerOptions GetOptions ( IDictionary < string , object > properties )
6065 => new KestrelServerOptions ( ) ;
6166
6267 public KestrelServer Server { get ; }
6368
64- public void Run ( string url , Action < BuildFunc > configure )
65- {
66- var done = new ManualResetEventSlim ( false ) ;
67-
68- Console . CancelKeyPress += ( s , e ) =>
69- {
70- Console . WriteLine ( "Application is shutting down..." ) ;
71-
72- done . Set ( ) ;
73-
74- e . Cancel = true ;
75- } ;
76-
77- Start ( url , configure ) ;
78-
79- done . Wait ( ) ;
80- }
81-
82- public void Start ( string url , Action < BuildFunc > configure )
69+ public Task Start ( string url , Action < BuildFunc > configure , CancellationToken cancellationToken )
8370 {
8471 var application = ConfigureApplication ( configure ) ;
8572
@@ -97,7 +84,7 @@ public void Start(string url, Action<BuildFunc> configure)
9784 Console . WriteLine ( $ "Now listening on: { address } ") ;
9885 }
9986
100- Server . Start ( application ) ;
87+ return Server . StartAsync ( application , cancellationToken ) ;
10188 }
10289
10390 public void Dispose ( ) => Server . Dispose ( ) ;
@@ -121,5 +108,32 @@ private static OwinApplication BuildApplication(IEnumerable<MidFunc> middleware)
121108
122109 return new OwinApplication ( app ) ;
123110 }
111+
112+ private class ConsoleLogger : ILogger
113+ {
114+ public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception exception , Func < TState , Exception , string > formatter )
115+ => Console . WriteLine ( formatter ( state , exception ) ) ;
116+
117+ public bool IsEnabled ( LogLevel logLevel ) => true ;
118+
119+ public IDisposable BeginScope < TState > ( TState state ) => new NoOpScope ( ) ;
120+
121+ private class NoOpScope : IDisposable
122+ {
123+ public void Dispose ( )
124+ {
125+ }
126+ }
127+ }
128+
129+ private class ConsoleLogProvider : ILoggerProvider
130+ {
131+ public void Dispose ( )
132+ {
133+
134+ }
135+
136+ public ILogger CreateLogger ( string categoryName ) => new ConsoleLogger ( ) ;
137+ }
124138 }
125139}
0 commit comments