diff --git a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs index 7a7002c..6f763a4 100644 --- a/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs +++ b/src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs @@ -200,43 +200,51 @@ static List GetBatchTypes() 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) + bool isFound = false; + foreach (var (method, cmdattr) in baseType.GetMethods(). + Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute())).Where(x => x.Attr != null)) { - if (foundType != null) + if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase))) { - throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + item.FullName); + if(foundType != null && foundMethod != null) + { + throw new InvalidOperationException($"Duplicate BatchBase Command name is not allowed, {foundType.FullName}.{foundMethod.Name} and {baseType.FullName}.{method.Name}"); + } + foundType = baseType; + foundMethod = method; + isFound = true; } - foundType = item; } - } - - if (foundType != null) - { - var method = foundType.GetMethod(methodName); - if (method != null) + if (!isFound && split.Length == 2) { - return (foundType, method); + if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase)) + { + if (foundType != null) + { + throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName); + } + foundType = baseType; + foundMethod = baseType.GetMethod(split[1]); + } } } - + if(foundType != null && foundMethod != null) + { + return (foundType, foundMethod); + } return (null, null); + } } } \ No newline at end of file diff --git a/tests/MicroBatchFramework.Tests/CommandAttributeTest.cs b/tests/MicroBatchFramework.Tests/CommandAttributeTest.cs new file mode 100644 index 0000000..eae3853 --- /dev/null +++ b/tests/MicroBatchFramework.Tests/CommandAttributeTest.cs @@ -0,0 +1,47 @@ +using FluentAssertions; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Threading.Tasks; +using Xunit; +using Xunit.Abstractions; + +namespace MicroBatchFramework.Tests +{ + public class CommandAttributeTest + { + class CommandAttributeTestCommand : BatchBase + { + ResultContainer _Result; + public CommandAttributeTestCommand(ResultContainer r) + { + _Result = r; + } + [Command("test")] + public void TestCommand(int value) + { + _Result.X = value; + } + } + class ResultContainer + { + public int X; + } + [Fact] + public async Task TestCommandName() + { + var host = BatchHost.CreateDefaultBuilder() + .ConfigureServices((c, services) => + { + services.AddSingleton(); + }) + .UseBatchEngine(new string[]{ "test", "-value", "1" }) + .Build(); + var result = host.Services.GetService(); + await host.RunAsync(); + result.X.Should().Be(1); + } + + } +} \ No newline at end of file