From 3e25be3f8c1492e95ec6947d6ceed7ce210303dc Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 27 Apr 2019 21:16:30 +0800 Subject: [PATCH] change the IConsumerServiceSelector interface access modifier (#333) * change the IConsumerServiceSelector interface access modifier * change the IConsumerServiceSelector interface access modifier --- CAP.sln | 7 +++ .../ConsumerExecutorDescriptor.cs | 2 +- .../IConsumerServiceSelector.Default.cs | 10 ++-- .../IConsumerServiceSelector.cs | 4 +- .../ConsumerServiceSelectorTest.cs | 58 +++++++++++++++++++ .../DotNetCore.CAP.CastleCoreTest.csproj | 22 +++++++ .../IConsumerServiceSelector.CastleCore.cs | 46 +++++++++++++++ 7 files changed, 141 insertions(+), 8 deletions(-) rename src/DotNetCore.CAP/{Internal => }/ConsumerExecutorDescriptor.cs (93%) rename src/DotNetCore.CAP/{Internal => }/IConsumerServiceSelector.Default.cs (95%) rename src/DotNetCore.CAP/{Internal => }/IConsumerServiceSelector.cs (93%) create mode 100644 test/DotNetCore.CAP.CastleCoreTest/ConsumerServiceSelectorTest.cs create mode 100644 test/DotNetCore.CAP.CastleCoreTest/DotNetCore.CAP.CastleCoreTest.csproj create mode 100644 test/DotNetCore.CAP.CastleCoreTest/IConsumerServiceSelector.CastleCore.cs diff --git a/CAP.sln b/CAP.sln index 8164c28..d0d9280 100644 --- a/CAP.sln +++ b/CAP.sln @@ -68,6 +68,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.InMemoryStor EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.AzureServiceBus.InMemory", "samples\Sample.AzureServiceBus.InMemory\Sample.AzureServiceBus.InMemory.csproj", "{1E1E959C-3D0E-45C3-ABCA-DAAACE68AAB8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCore.CAP.CastleCoreTest", "test\DotNetCore.CAP.CastleCoreTest\DotNetCore.CAP.CastleCoreTest.csproj", "{BA499B87-77A9-43A2-98A3-89ECF5034E26}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -142,6 +144,10 @@ Global {1E1E959C-3D0E-45C3-ABCA-DAAACE68AAB8}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E1E959C-3D0E-45C3-ABCA-DAAACE68AAB8}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E1E959C-3D0E-45C3-ABCA-DAAACE68AAB8}.Release|Any CPU.Build.0 = Release|Any CPU + {BA499B87-77A9-43A2-98A3-89ECF5034E26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA499B87-77A9-43A2-98A3-89ECF5034E26}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA499B87-77A9-43A2-98A3-89ECF5034E26}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA499B87-77A9-43A2-98A3-89ECF5034E26}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -165,6 +171,7 @@ Global {63B2A464-FBEA-42FB-8EFA-98AFA39FC920} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {58B6E829-C6C8-457C-9DD0-C600650254DF} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4} {1E1E959C-3D0E-45C3-ABCA-DAAACE68AAB8} = {3A6B6931-A123-477A-9469-8B468B5385AF} + {BA499B87-77A9-43A2-98A3-89ECF5034E26} = {C09CDAB0-6DD4-46E9-B7F3-3EF2A4741EA0} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2E70565D-94CF-40B4-BFE1-AC18D5F736AB} diff --git a/src/DotNetCore.CAP/Internal/ConsumerExecutorDescriptor.cs b/src/DotNetCore.CAP/ConsumerExecutorDescriptor.cs similarity index 93% rename from src/DotNetCore.CAP/Internal/ConsumerExecutorDescriptor.cs rename to src/DotNetCore.CAP/ConsumerExecutorDescriptor.cs index 6f8f532..730e600 100644 --- a/src/DotNetCore.CAP/Internal/ConsumerExecutorDescriptor.cs +++ b/src/DotNetCore.CAP/ConsumerExecutorDescriptor.cs @@ -4,7 +4,7 @@ using System.Reflection; using DotNetCore.CAP.Abstractions; -namespace DotNetCore.CAP.Internal +namespace DotNetCore.CAP { /// /// A descriptor of user definition method. diff --git a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs b/src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs similarity index 95% rename from src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs rename to src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs index 47b4be6..81cf664 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs +++ b/src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs @@ -11,13 +11,13 @@ using DotNetCore.CAP.Infrastructure; using Microsoft.Extensions.DependencyInjection; using System.Collections.Concurrent; -namespace DotNetCore.CAP.Internal +namespace DotNetCore.CAP { /// /// /// A default implementation. /// - internal class DefaultConsumerServiceSelector : IConsumerServiceSelector + public class DefaultConsumerServiceSelector : IConsumerServiceSelector { private readonly CapOptions _capOptions; private readonly IServiceProvider _serviceProvider; @@ -71,7 +71,7 @@ namespace DotNetCore.CAP.Internal return result; } - private IEnumerable FindConsumersFromInterfaceTypes( + protected virtual IEnumerable FindConsumersFromInterfaceTypes( IServiceProvider provider) { var executorDescriptorList = new List(); @@ -95,7 +95,7 @@ namespace DotNetCore.CAP.Internal } } - private IEnumerable FindConsumersFromControllerTypes() + protected virtual IEnumerable FindConsumersFromControllerTypes() { var executorDescriptorList = new List(); @@ -112,7 +112,7 @@ namespace DotNetCore.CAP.Internal return executorDescriptorList; } - private IEnumerable GetTopicAttributesDescription(TypeInfo typeInfo) + protected IEnumerable GetTopicAttributesDescription(TypeInfo typeInfo) { foreach (var method in typeInfo.DeclaredMethods) { diff --git a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.cs b/src/DotNetCore.CAP/IConsumerServiceSelector.cs similarity index 93% rename from src/DotNetCore.CAP/Internal/IConsumerServiceSelector.cs rename to src/DotNetCore.CAP/IConsumerServiceSelector.cs index 0b53af7..555a9d7 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.cs +++ b/src/DotNetCore.CAP/IConsumerServiceSelector.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; -namespace DotNetCore.CAP.Internal +namespace DotNetCore.CAP { /// /// Defines an interface for selecting an consumer service method to invoke for the current message. /// - internal interface IConsumerServiceSelector + public interface IConsumerServiceSelector { /// /// Selects a set of candidates for the current message associated with diff --git a/test/DotNetCore.CAP.CastleCoreTest/ConsumerServiceSelectorTest.cs b/test/DotNetCore.CAP.CastleCoreTest/ConsumerServiceSelectorTest.cs new file mode 100644 index 0000000..20be560 --- /dev/null +++ b/test/DotNetCore.CAP.CastleCoreTest/ConsumerServiceSelectorTest.cs @@ -0,0 +1,58 @@ +using Castle.DynamicProxy; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using System; +using Xunit; + +namespace DotNetCore.CAP.CastleDynamicProxyTest +{ + public class ConsumerServiceSelectorTest + { + private IServiceProvider _provider; + + public ConsumerServiceSelectorTest() + { + var services = new ServiceCollection(); + + services.AddSingleton(typeof(ICapSubscribe), f => + { + var generator = new ProxyGenerator(); + return generator.CreateClassProxy(typeof(TestSubscribeClass)); + }); + + services.AddSingleton(); + + services.AddLogging(); + + services.TryAddSingleton(); + + services.AddCap(x => { }); + + _provider = services.BuildServiceProvider(); + } + + [Theory] + [InlineData("cap.castle.sub")] + public void CanFindCapSubscribeTopic(string topic) + { + var selector = _provider.GetRequiredService(); + + var candidates = selector.SelectCandidates(); + Assert.Equal(2, candidates.Count); + } + } + + public interface ITestSubscribeClass + { + + } + + public class TestSubscribeClass : ITestSubscribeClass, ICapSubscribe + { + [CapSubscribe("cap.castle.sub")] + public void TestSubscribe(DateTime dateTime) + { + Console.WriteLine(dateTime); + } + } +} diff --git a/test/DotNetCore.CAP.CastleCoreTest/DotNetCore.CAP.CastleCoreTest.csproj b/test/DotNetCore.CAP.CastleCoreTest/DotNetCore.CAP.CastleCoreTest.csproj new file mode 100644 index 0000000..2972e6c --- /dev/null +++ b/test/DotNetCore.CAP.CastleCoreTest/DotNetCore.CAP.CastleCoreTest.csproj @@ -0,0 +1,22 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + + + + diff --git a/test/DotNetCore.CAP.CastleCoreTest/IConsumerServiceSelector.CastleCore.cs b/test/DotNetCore.CAP.CastleCoreTest/IConsumerServiceSelector.CastleCore.cs new file mode 100644 index 0000000..3b91017 --- /dev/null +++ b/test/DotNetCore.CAP.CastleCoreTest/IConsumerServiceSelector.CastleCore.cs @@ -0,0 +1,46 @@ +using Castle.Core; +using Castle.DynamicProxy; +using DotNetCore.CAP.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace DotNetCore.CAP.CastleDynamicProxyTest +{ + public class CastleCoreConsumerServiceSelector : DefaultConsumerServiceSelector + { + public CastleCoreConsumerServiceSelector(IServiceProvider serviceProvider, CapOptions capOptions) + : base(serviceProvider, capOptions) + { + + } + + protected override IEnumerable FindConsumersFromInterfaceTypes(IServiceProvider provider) + { + var executorDescriptorList = new List(); + + using (var scoped = provider.CreateScope()) + { + var scopedProvider = scoped.ServiceProvider; + var consumerServices = scopedProvider.GetServices(); + foreach (var service in consumerServices) + { + var serviceType = service.GetType(); + // Castle dynamic proxy... + TypeInfo typeInfo = ProxyServices.IsDynamicProxy(serviceType) ? ProxyUtil.GetUnproxiedType(service).GetTypeInfo() + : serviceType.GetTypeInfo(); + + if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo)) + { + continue; + } + + executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); + } + + return executorDescriptorList; + } + } + } +}