Commit d240ed9c authored by Null's avatar Null Committed by Savorboard

Try to get the service instance using the serviceProvider.GetServices method. (#356)

parent d7277b1e
...@@ -21,6 +21,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -21,6 +21,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary> /// </summary>
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {
internal static IServiceCollection ServiceCollection;
/// <summary> /// <summary>
/// Adds and configures the consistence services for the consistency. /// Adds and configures the consistence services for the consistency.
/// </summary> /// </summary>
...@@ -34,10 +35,9 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -34,10 +35,9 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(setupAction)); throw new ArgumentNullException(nameof(setupAction));
} }
services.TryAddSingleton<CapMarkerService>(); ServiceCollection = services;
//Consumer service services.TryAddSingleton<CapMarkerService>();
AddSubscribeServices(services);
//Serializer and model binder //Serializer and model binder
services.TryAddSingleton<IContentSerializer, JsonContentSerializer>(); services.TryAddSingleton<IContentSerializer, JsonContentSerializer>();
...@@ -80,24 +80,5 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -80,24 +80,5 @@ namespace Microsoft.Extensions.DependencyInjection
return new CapBuilder(services); return new CapBuilder(services);
} }
private static void AddSubscribeServices(IServiceCollection services)
{
var consumerListenerServices = new List<KeyValuePair<Type, Type>>();
foreach (var rejectedServices in services)
{
if (rejectedServices.ImplementationType != null
&& typeof(ICapSubscribe).IsAssignableFrom(rejectedServices.ImplementationType))
{
consumerListenerServices.Add(new KeyValuePair<Type, Type>(typeof(ICapSubscribe),
rejectedServices.ImplementationType));
}
}
foreach (var service in consumerListenerServices)
{
services.TryAddEnumerable(ServiceDescriptor.Transient(service.Key, service.Value));
}
}
} }
} }
\ No newline at end of file
...@@ -11,6 +11,8 @@ namespace DotNetCore.CAP ...@@ -11,6 +11,8 @@ namespace DotNetCore.CAP
/// </summary> /// </summary>
public class ConsumerExecutorDescriptor public class ConsumerExecutorDescriptor
{ {
public TypeInfo ServiceTypeInfo { get; set; }
public MethodInfo MethodInfo { get; set; } public MethodInfo MethodInfo { get; set; }
public TypeInfo ImplTypeInfo { get; set; } public TypeInfo ImplTypeInfo { get; set; }
......
...@@ -76,23 +76,21 @@ namespace DotNetCore.CAP ...@@ -76,23 +76,21 @@ namespace DotNetCore.CAP
{ {
var executorDescriptorList = new List<ConsumerExecutorDescriptor>(); var executorDescriptorList = new List<ConsumerExecutorDescriptor>();
using (var scoped = provider.CreateScope()) var capSubscribeTypeInfo = typeof(ICapSubscribe).GetTypeInfo();
{
var scopedProvider = scoped.ServiceProvider; foreach (var service in ServiceCollectionExtensions.ServiceCollection.Where(o => o.ImplementationType != null && o.ServiceType != null))
var consumerServices = scopedProvider.GetServices<ICapSubscribe>(); {
foreach (var service in consumerServices) var typeInfo = service.ImplementationType.GetTypeInfo();
{ if (!capSubscribeTypeInfo.IsAssignableFrom(typeInfo))
var typeInfo = service.GetType().GetTypeInfo(); {
if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo)) continue;
{
continue;
}
executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo));
} }
var serviceTypeInfo = service.ServiceType.GetTypeInfo();
return executorDescriptorList;
} executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo, serviceTypeInfo));
}
return executorDescriptorList;
} }
protected virtual IEnumerable<ConsumerExecutorDescriptor> FindConsumersFromControllerTypes() protected virtual IEnumerable<ConsumerExecutorDescriptor> FindConsumersFromControllerTypes()
...@@ -112,7 +110,7 @@ namespace DotNetCore.CAP ...@@ -112,7 +110,7 @@ namespace DotNetCore.CAP
return executorDescriptorList; return executorDescriptorList;
} }
protected IEnumerable<ConsumerExecutorDescriptor> GetTopicAttributesDescription(TypeInfo typeInfo) protected IEnumerable<ConsumerExecutorDescriptor> GetTopicAttributesDescription(TypeInfo typeInfo, TypeInfo serviceTypeInfo = null)
{ {
foreach (var method in typeInfo.DeclaredMethods) foreach (var method in typeInfo.DeclaredMethods)
{ {
...@@ -135,7 +133,7 @@ namespace DotNetCore.CAP ...@@ -135,7 +133,7 @@ namespace DotNetCore.CAP
attr.Group = attr.Group + "." + _capOptions.Version; attr.Group = attr.Group + "." + _capOptions.Version;
} }
yield return InitDescriptor(attr, method, typeInfo); yield return InitDescriptor(attr, method, typeInfo, serviceTypeInfo);
} }
} }
} }
...@@ -143,13 +141,15 @@ namespace DotNetCore.CAP ...@@ -143,13 +141,15 @@ namespace DotNetCore.CAP
private static ConsumerExecutorDescriptor InitDescriptor( private static ConsumerExecutorDescriptor InitDescriptor(
TopicAttribute attr, TopicAttribute attr,
MethodInfo methodInfo, MethodInfo methodInfo,
TypeInfo implType) TypeInfo implType,
TypeInfo serviceTypeInfo)
{ {
var descriptor = new ConsumerExecutorDescriptor var descriptor = new ConsumerExecutorDescriptor
{ {
Attribute = attr, Attribute = attr,
MethodInfo = methodInfo, MethodInfo = methodInfo,
ImplTypeInfo = implType ImplTypeInfo = implType,
ServiceTypeInfo = serviceTypeInfo
}; };
return descriptor; return descriptor;
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Licensed under the MIT License. See License.txt in the project root for license information. // Licensed under the MIT License. See License.txt in the project root for license information.
using System; using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions; using DotNetCore.CAP.Abstractions;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
...@@ -38,9 +40,20 @@ namespace DotNetCore.CAP.Internal ...@@ -38,9 +40,20 @@ namespace DotNetCore.CAP.Internal
using (var scope = _serviceProvider.CreateScope()) using (var scope = _serviceProvider.CreateScope())
{ {
var provider = scope.ServiceProvider; var provider = scope.ServiceProvider;
var serviceType = context.ConsumerDescriptor.ImplTypeInfo.AsType(); var serviceType = context.ConsumerDescriptor.ImplTypeInfo.AsType();
var obj = ActivatorUtilities.GetServiceOrCreateInstance(provider, serviceType); object obj = null;
if (context.ConsumerDescriptor.ServiceTypeInfo != null)
{
obj = provider.GetServices(context.ConsumerDescriptor.ServiceTypeInfo.AsType())
.FirstOrDefault(o => o.GetType() == serviceType);
}
if (obj == null)
{
obj = ActivatorUtilities.GetServiceOrCreateInstance(provider, serviceType);
}
var jsonContent = context.DeliverMessage.Content; var jsonContent = context.DeliverMessage.Content;
var message = _messagePacker.UnPack(jsonContent); var message = _messagePacker.UnPack(jsonContent);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment