CommandAttribute did not work?
itn3000 opened this issue · comments
Yusuke Ito commented
Environment
- dotnet core sdk 3.0pre6
- MicroBatchFramework: 1.2.0
Steps to reproduce
- create console project(and set TargetFramework to netcoreapp2.1)
- write following source to
Program.cs
- do
dotnet run -- test -arg1 aaaa
using System;
using System.Threading.Tasks;
using MicroBatchFramework;
namespace microbatchframeworktest
{
class MyBatch : BatchBase
{
[Command("test")]
public void MyTest(string arg1)
{
Console.WriteLine("hello {0}", arg1);
}
}
class Program
{
static async Task Main(string[] args)
{
await BatchHost.CreateDefaultBuilder().RunBatchEngineAsync(args);
}
}
}
Expected behavior
print "hello aaaa"
Actual behavior
print "Type or method does not found on this Program. args: test -arg1 aaaa" then exit.
Additional
dotnet run -- MyBatch.MyTest -arg1 aaaa
seems to work- did
dotnet run -- list
, then outputMyBatch.MyTest
was output
Yoshifumi Kawai commented
Oh!
Thank you, I'll check soon.
Yusuke Ito commented
I found some code where it might be the cause.
https://github.com/Cysharp/MicroBatchFramework/blob/f6a549c07d77b4a6f1ac8b0eda27cbd63ac863d5/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs#L203-L207
and I fixed the code like following, it seems to be fine(still it has not been verified enough)
diff --git a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs
index 7a7002c..e29c7db 100644
--- a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs
+++ b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs
@@ -200,43 +200,48 @@ namespace MicroBatchFramework
static (Type, MethodInfo) GetTypeFromAssemblies(string arg0)
{
- var split = arg0.Split('.');
- if (split.Length != 2)
- {
- return (null, null);
- }
- var typeName = split[0];
- var methodName = split[1];
-
var batchBaseTypes = GetBatchTypes();
if (batchBaseTypes == null)
{
return (null, null);
}
+ var split = arg0.Split('.');
Type foundType = null;
- foreach (var item in batchBaseTypes)
+ MethodInfo foundMethod = null;
+ foreach (var baseType in batchBaseTypes)
{
- if (item.Name == typeName)
+ if (split.Length == 2)
{
- if (foundType != null)
+ if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
{
- throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + item.FullName);
+ if (foundType != null)
+ {
+ throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
+ }
+ foundType = baseType;
+ foundMethod = baseType.GetMethod(split[1]);
}
- foundType = item;
}
- }
-
- if (foundType != null)
- {
- var method = foundType.GetMethod(methodName);
- if (method != null)
+ else
{
- return (foundType, method);
+ foreach (var (method, cmdattr) in baseType.GetMethods().
+ Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
+ {
+ if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
+ {
+ foundType = baseType;
+ foundMethod = method;
+ }
+ }
}
}
-
+ if(foundType != null && foundMethod != null)
+ {
+ return (foundType, foundMethod);
+ }
return (null, null);
+
}
}
}
\ No newline at end of file
Yoshifumi Kawai commented
Thanks, I've released v1.3.0.