Skip to content

Commit 6c3bd87

Browse files
authored
Merge pull request #50 from Cysharp/v3
V3
2 parents b50f19b + 493a92f commit 6c3bd87

19 files changed

+1081
-701
lines changed

ReadMe.md

Lines changed: 194 additions & 87 deletions
Large diffs are not rendered by default.

sandbox/MultiContainedApp/Program.cs

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using System;
5+
using System.Diagnostics;
56
using System.Threading.Tasks;
67

78
namespace MultiContainedApp
@@ -10,30 +11,110 @@ class Program
1011
{
1112
static async Task Main(string[] args)
1213
{
14+
//args = new string[] { "Bar.Hello3", "2" };
15+
//args = new string[] { "bar", "hello3", "-help" };
16+
args = new string[] { "foo", "echo", "help"};
17+
//args = new string[] { "bar.hello2", "help" };
18+
1319
await Host.CreateDefaultBuilder()
14-
.RunConsoleAppFrameworkAsync(args);
20+
.ConfigureLogging(x => x.SetMinimumLevel(LogLevel.Trace))
21+
.RunConsoleAppFrameworkAsync(args, options: new ConsoleAppOptions
22+
{
23+
GlobalFilters = new ConsoleAppFilter[] { new MyFilter2 { Order = -1 }, new MyFilter() }
24+
});
25+
26+
27+
28+
//await Host.CreateDefaultBuilder()
29+
// .ConfigureLogging(x =>
30+
// {
31+
// x.ClearProviders();
32+
// x.SetMinimumLevel(LogLevel.Trace);
33+
34+
// })
35+
// .RunConsoleAppFrameworkAsync(args);
1536
}
1637
}
1738

39+
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
40+
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
1841
public class Foo : ConsoleAppBase
1942
{
20-
[Command("Echo", "Echo message to the logger")]
21-
public void Echo([Option("msg", "Message to send.")]string msg)
43+
public void Echo(string msg)
2244
{
23-
this.Context.Logger.LogInformation(msg);
45+
Console.WriteLine(msg);
2446
}
2547

26-
public void Sum([Option(0)]int x, [Option(1)]int y)
48+
public void Sum(int x, int y)
2749
{
28-
this.Context.Logger.LogInformation((x + y).ToString());
50+
Console.WriteLine((x + y).ToString());
2951
}
3052
}
3153

3254
public class Bar : ConsoleAppBase
3355
{
3456
public void Hello2()
3557
{
36-
this.Context.Logger.LogInformation("H E L L O");
58+
Console.WriteLine("H E L L O");
59+
}
60+
}
61+
62+
63+
64+
//public class Foo : ConsoleAppBase
65+
//{
66+
// [Command(new[] { "eo", "t" }, "Echo message to the logger")]
67+
// [ConsoleAppFilter(typeof(EchoFilter), Order = 10000)]
68+
// public void Echo([Option("msg", "Message to send.")]string msg)
69+
// {
70+
// // Console.WriteLine(new StackTrace().ToString());
71+
// this.Context.Logger.LogInformation(msg);
72+
// }
73+
74+
// [Command("s")]
75+
// public void Sum([Option(0)]int x, [Option(1)]int y)
76+
// {
77+
// this.Context.Logger.LogInformation((x + y).ToString());
78+
// }
79+
//}
80+
81+
//public class Bar : ConsoleAppBase
82+
//{
83+
// public void Hello2()
84+
// {
85+
// this.Context.Logger.LogInformation("H E L L O");
86+
// }
87+
88+
// public void Hello3([Option(0)]int aaa)
89+
// {
90+
// this.Context.Logger.LogInformation("H E L L O:" + aaa);
91+
// }
92+
//}
93+
94+
public class MyFilter : ConsoleAppFilter
95+
{
96+
public async override ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
97+
{
98+
Console.WriteLine("call second");
99+
await next(context);
100+
}
101+
}
102+
103+
public class MyFilter2 : ConsoleAppFilter
104+
{
105+
public async override ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
106+
{
107+
Console.WriteLine("call first");
108+
await next(context);
109+
}
110+
}
111+
112+
public class EchoFilter : ConsoleAppFilter
113+
{
114+
public async override ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
115+
{
116+
Console.WriteLine("call ec");
117+
await next(context);
37118
}
38119
}
39120
}

sandbox/SingleContainedApp/Program.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public async Task Throw()
160160

161161
public class SimpleTwoArgs : ConsoleAppBase
162162
{
163-
public async ValueTask<int> Hello(string name, int repeat)
163+
public async ValueTask<int> Hello([Option("n")]string name, [Option("r")]int repeat)
164164
{
165165
Context.Logger.LogInformation($"name:{name}");
166166

@@ -173,28 +173,65 @@ public async ValueTask<int> Hello(string name, int repeat)
173173
}
174174
}
175175

176-
class Program
176+
public class Issue46 : ConsoleAppBase, IDisposable
177177
{
178-
static async Task Main(string[] args)
178+
179+
public void Run(string str, bool b = false)
179180
{
180-
//args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
181+
Console.WriteLine("str:" + str + " b:" + b);
182+
}
183+
184+
void IDisposable.Dispose()
185+
{
186+
Console.WriteLine("DISPOSE!");
187+
}
188+
}
181189

182-
args = new[] { "-name", "aaa", "-repeat", "3" };
190+
//class Program
191+
//{
192+
// static async Task Main(string[] args)
193+
// {
194+
// //args = new[] { "-array", "10,20,30", "-person", @"{""Age"":10,""Name"":""foo""}" };
195+
196+
// //args = new[] { "--name", "aaa", "--repeat", "3" };
197+
198+
// //args = new[] { "--help" };
199+
// //args = new[] { "encode", "--help" };
200+
// //args = new[] { "-str", "input" };
201+
// //args = new[] { "-str", "input", "-b"};
202+
// //args = new[] { "-str" };
203+
204+
// await Host.CreateDefaultBuilder()
205+
// .ConfigureLogging(logging =>
206+
// {
207+
// logging.SetMinimumLevel(LogLevel.Trace).ReplaceToSimpleConsole();
208+
// })
209+
// .RunConsoleAppFrameworkAsync<Issue46>(args);
210+
// // .RunConsoleAppEngineAsync
211+
// //.ConfigureServices((hostContext, services) =>
212+
// //{
213+
// // // mapping config json to IOption<MyConfig>
214+
// // services.Configure<MyConfig>(hostContext.Configuration);
215+
// //})
216+
// //.RunConsoleAppEngineAsync<StandardArgTest>(args);
217+
// }
218+
//}
219+
220+
public class Program : ConsoleAppBase
221+
{
222+
static async Task Main(string[] args)
223+
{
224+
await Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<MyFirstBatch>(args, new ConsoleAppOptions
225+
{
226+
//StrictOption = true, // default is false.
227+
//ShowDefaultCommand = false, // default is true
228+
});
183229

230+
}
184231

185-
await Host.CreateDefaultBuilder()
186-
.ConfigureLogging(logging =>
187-
{
188-
logging.SetMinimumLevel(LogLevel.Trace).ReplaceToSimpleConsole();
189-
})
190-
.RunConsoleAppFrameworkAsync<SimpleTwoArgs>(args);
191-
// .RunConsoleAppEngineAsync
192-
//.ConfigureServices((hostContext, services) =>
193-
//{
194-
// // mapping config json to IOption<MyConfig>
195-
// services.Configure<MyConfig>(hostContext.Configuration);
196-
//})
197-
//.RunConsoleAppEngineAsync<StandardArgTest>(args);
232+
public void Hello([Option("m", "Message to display.")]string message)
233+
{
234+
Console.WriteLine(message);
198235
}
199236
}
200237
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using ConsoleAppFramework;
2+
using Microsoft.Extensions.Logging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace SingleContainedApp
10+
{
11+
public class LogRunningTimeFilter : ConsoleAppFilter
12+
{
13+
public override async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
14+
{
15+
context.Logger.LogInformation("Call method at " + context.Timestamp.ToLocalTime()); // LocalTime for human readable time
16+
try
17+
{
18+
await next(context);
19+
context.Logger.LogInformation("Call method Completed successfully, Elapsed:" + (DateTimeOffset.UtcNow - context.Timestamp));
20+
}
21+
catch
22+
{
23+
context.Logger.LogInformation("Call method Completed Failed, Elapsed:" + (DateTimeOffset.UtcNow - context.Timestamp));
24+
throw;
25+
}
26+
}
27+
}
28+
29+
public class MutexFilter : ConsoleAppFilter
30+
{
31+
public override async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
32+
{
33+
using (var mutex = new Mutex(false, context.MethodInfo.Name))
34+
{
35+
if (!mutex.WaitOne(0, false))
36+
{
37+
throw new Exception($"already running {context.MethodInfo.Name} in another process.");
38+
}
39+
40+
try
41+
{
42+
await next(context);
43+
}
44+
finally
45+
{
46+
mutex.ReleaseMutex();
47+
}
48+
}
49+
}
50+
}
51+
}

src/ConsoleAppFramework.WebHosting/ConsoleAppFrameworkHostingExtensions.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ namespace ConsoleAppFramework // .WebHosting
1515
{
1616
public static class ConsoleAppFrameworkHostingExtensions
1717
{
18-
public static IWebHostBuilder PrepareConsoleAppFrameworkMiddleware(this IWebHostBuilder builder, IConsoleAppInterceptor? interceptor = null)
18+
public static IWebHostBuilder PrepareConsoleAppFrameworkMiddleware(this IWebHostBuilder builder, ConsoleAppOptions? options = null)
1919
{
2020
var consoleAppTypes = CollectConsoleAppTypes();
2121
var target = new TargetConsoleAppTypeCollection(consoleAppTypes);
2222

2323
return builder
2424
.ConfigureServices(services =>
2525
{
26-
services.AddSingleton<IConsoleAppInterceptor>(interceptor ?? NullConsoleAppInterceptor.Default);
26+
services.AddSingleton<ConsoleAppOptions>(options ?? new ConsoleAppOptions());
2727
services.AddSingleton<TargetConsoleAppTypeCollection>(target);
2828
foreach (var item in target)
2929
{
@@ -32,11 +32,11 @@ public static IWebHostBuilder PrepareConsoleAppFrameworkMiddleware(this IWebHost
3232
});
3333
}
3434

35-
public static async Task RunConsoleAppFrameworkWebHostingAsync(this IHostBuilder builder, string urls, SwaggerOptions? swaggerOptions = null, IConsoleAppInterceptor? interceptor = null)
35+
public static async Task RunConsoleAppFrameworkWebHostingAsync(this IHostBuilder builder, string urls, SwaggerOptions? swaggerOptions = null, ConsoleAppOptions? options = null)
3636
{
3737
var host = builder.ConfigureWebHost(webHost =>
3838
{
39-
webHost.PrepareConsoleAppFrameworkMiddleware(interceptor)
39+
webHost.PrepareConsoleAppFrameworkMiddleware(options)
4040
.ConfigureServices(services =>
4141
{
4242
if (swaggerOptions == null)
@@ -71,27 +71,27 @@ public class DefaultStartup
7171
{
7272
public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime)
7373
{
74-
var interceptor = app.ApplicationServices.GetService<IConsoleAppInterceptor>();
74+
// var interceptor = app.ApplicationServices.GetService<IConsoleAppInterceptor>();
7575
var provider = app.ApplicationServices.GetService<IServiceProvider>();
7676
var logger = app.ApplicationServices.GetService<ILogger<ConsoleAppEngine>>();
7777

78-
lifetime.ApplicationStarted.Register(async () =>
79-
{
80-
try
81-
{
82-
await interceptor.OnEngineBeginAsync(provider, logger);
83-
}
84-
catch { }
85-
});
78+
//lifetime.ApplicationStarted.Register(async () =>
79+
//{
80+
// try
81+
// {
82+
// // await interceptor.OnEngineBeginAsync(provider, logger);
83+
// }
84+
// catch { }
85+
//});
8686

87-
lifetime.ApplicationStopped.Register(async () =>
88-
{
89-
try
90-
{
91-
await interceptor.OnEngineCompleteAsync(provider, logger);
92-
}
93-
catch { }
94-
});
87+
//lifetime.ApplicationStopped.Register(async () =>
88+
//{
89+
// try
90+
// {
91+
// // await interceptor.OnEngineCompleteAsync(provider, logger);
92+
// }
93+
// catch { }
94+
//});
9595

9696
var swaggerOption = app.ApplicationServices.GetService<SwaggerOptions>() ?? new SwaggerOptions("ConsoleAppFramework", "", "/");
9797
app.UseConsoleAppFrameworkSwaggerMiddleware(swaggerOption);

0 commit comments

Comments
 (0)