Skip to content

Commit 493a92f

Browse files
committed
v3
1 parent 32a8e37 commit 493a92f

File tree

14 files changed

+564
-279
lines changed

14 files changed

+564
-279
lines changed

ReadMe.md

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

sandbox/MultiContainedApp/Program.cs

Lines changed: 62 additions & 30 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,78 +11,109 @@ class Program
1011
{
1112
static async Task Main(string[] args)
1213
{
13-
// args = new string[] { "Bar.Hello3", "2" };
14+
//args = new string[] { "Bar.Hello3", "2" };
1415
//args = new string[] { "bar", "hello3", "-help" };
15-
args = new string[] { "foo.eo", "-msg", "e" };
16+
args = new string[] { "foo", "echo", "help"};
1617
//args = new string[] { "bar.hello2", "help" };
1718

1819
await Host.CreateDefaultBuilder()
1920
.ConfigureLogging(x => x.SetMinimumLevel(LogLevel.Trace))
20-
.RunConsoleAppFrameworkAsync(args, options: new ConsoleAppFrameworkOptions
21+
.RunConsoleAppFrameworkAsync(args, options: new ConsoleAppOptions
2122
{
22-
GlobalFilters = new IConsoleAppFrameworkFilter[] { new MyFilter2 { Order = -1 }, new MyFilter() }
23+
GlobalFilters = new ConsoleAppFilter[] { new MyFilter2 { Order = -1 }, new MyFilter() }
2324
});
25+
26+
27+
28+
//await Host.CreateDefaultBuilder()
29+
// .ConfigureLogging(x =>
30+
// {
31+
// x.ClearProviders();
32+
// x.SetMinimumLevel(LogLevel.Trace);
33+
34+
// })
35+
// .RunConsoleAppFrameworkAsync(args);
2436
}
2537
}
2638

39+
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
40+
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
2741
public class Foo : ConsoleAppBase
2842
{
29-
[Command(new[] { "eo", "t" }, "Echo message to the logger")]
30-
[ConsoleAppFrameworkFilter(typeof(EchoFilter), Order = 10)]
31-
public void Echo([Option("msg", "Message to send.")]string msg)
43+
public void Echo(string msg)
3244
{
33-
34-
this.Context.Logger.LogInformation(msg);
45+
Console.WriteLine(msg);
3546
}
3647

37-
[Command("s")]
38-
public void Sum([Option(0)]int x, [Option(1)]int y)
48+
public void Sum(int x, int y)
3949
{
40-
this.Context.Logger.LogInformation((x + y).ToString());
50+
Console.WriteLine((x + y).ToString());
4151
}
4252
}
4353

4454
public class Bar : ConsoleAppBase
4555
{
4656
public void Hello2()
4757
{
48-
this.Context.Logger.LogInformation("H E L L O");
49-
}
50-
51-
public void Hello3([Option(0)]int aaa)
52-
{
53-
this.Context.Logger.LogInformation("H E L L O:" + aaa);
58+
Console.WriteLine("H E L L O");
5459
}
5560
}
5661

57-
public class MyFilter : IConsoleAppFrameworkFilter
58-
{
59-
public int Order { get; set; } // order will set from attribute.
6062

61-
public async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
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)
6297
{
6398
Console.WriteLine("call second");
6499
await next(context);
65100
}
66101
}
67102

68-
public class MyFilter2 : IConsoleAppFrameworkFilter
103+
public class MyFilter2 : ConsoleAppFilter
69104
{
70-
public int Order { get; set; } // order will set from attribute.
71-
72-
public async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
105+
public async override ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
73106
{
74107
Console.WriteLine("call first");
75108
await next(context);
76109
}
77110
}
78111

79-
public class EchoFilter : IConsoleAppFrameworkFilter
112+
public class EchoFilter : ConsoleAppFilter
80113
{
81-
public int Order { get; set; }
82-
83-
public async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
114+
public async override ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
84115
{
116+
Console.WriteLine("call ec");
85117
await next(context);
86118
}
87119
}

sandbox/SingleContainedApp/Program.cs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,30 +173,65 @@ public async ValueTask<int> Hello([Option("n")]string name, [Option("r")]int rep
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

184-
//args = new[] { "--help" };
185-
args = new[] { "encode", "--help" };
230+
}
186231

187-
await Host.CreateDefaultBuilder()
188-
.ConfigureLogging(logging =>
189-
{
190-
logging.SetMinimumLevel(LogLevel.Trace).ReplaceToSimpleConsole();
191-
})
192-
.RunConsoleAppFrameworkAsync<OverrideCheck>(args);
193-
// .RunConsoleAppEngineAsync
194-
//.ConfigureServices((hostContext, services) =>
195-
//{
196-
// // mapping config json to IOption<MyConfig>
197-
// services.Configure<MyConfig>(hostContext.Configuration);
198-
//})
199-
//.RunConsoleAppEngineAsync<StandardArgTest>(args);
232+
public void Hello([Option("m", "Message to display.")]string message)
233+
{
234+
Console.WriteLine(message);
200235
}
201236
}
202237
}
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: 3 additions & 3 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, ConsoleAppFrameworkOptions? options = 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<ConsoleAppFrameworkOptions>(options ?? new ConsoleAppFrameworkOptions());
26+
services.AddSingleton<ConsoleAppOptions>(options ?? new ConsoleAppOptions());
2727
services.AddSingleton<TargetConsoleAppTypeCollection>(target);
2828
foreach (var item in target)
2929
{
@@ -32,7 +32,7 @@ public static IWebHostBuilder PrepareConsoleAppFrameworkMiddleware(this IWebHost
3232
});
3333
}
3434

35-
public static async Task RunConsoleAppFrameworkWebHostingAsync(this IHostBuilder builder, string urls, SwaggerOptions? swaggerOptions = null, ConsoleAppFrameworkOptions? options = 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
{

0 commit comments

Comments
 (0)