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
/// </summary>
public static class ServiceCollectionExtensions
{
internal static IServiceCollection ServiceCollection;
/// <summary>
/// Adds and configures the consistence services for the consistency.
/// </summary>
......@@ -34,10 +35,9 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(setupAction));
}
services.TryAddSingleton<CapMarkerService>();
ServiceCollection = services;
//Consumer service
AddSubscribeServices(services);
services.TryAddSingleton<CapMarkerService>();
//Serializer and model binder
services.TryAddSingleton<IContentSerializer, JsonContentSerializer>();
......@@ -80,24 +80,5 @@ namespace Microsoft.Extensions.DependencyInjection
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
/// </summary>
public class ConsumerExecutorDescriptor
{
public TypeInfo ServiceTypeInfo { get; set; }
public MethodInfo MethodInfo { get; set; }
public TypeInfo ImplTypeInfo { get; set; }
......
......@@ -76,23 +76,21 @@ namespace DotNetCore.CAP
{
var executorDescriptorList = new List<ConsumerExecutorDescriptor>();
using (var scoped = provider.CreateScope())
{
var scopedProvider = scoped.ServiceProvider;
var consumerServices = scopedProvider.GetServices<ICapSubscribe>();
foreach (var service in consumerServices)
{
var typeInfo = service.GetType().GetTypeInfo();
if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo))
{
continue;
}
executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo));
var capSubscribeTypeInfo = typeof(ICapSubscribe).GetTypeInfo();
foreach (var service in ServiceCollectionExtensions.ServiceCollection.Where(o => o.ImplementationType != null && o.ServiceType != null))
{
var typeInfo = service.ImplementationType.GetTypeInfo();
if (!capSubscribeTypeInfo.IsAssignableFrom(typeInfo))
{
continue;
}
return executorDescriptorList;
}
var serviceTypeInfo = service.ServiceType.GetTypeInfo();
executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo, serviceTypeInfo));
}
return executorDescriptorList;
}
protected virtual IEnumerable<ConsumerExecutorDescriptor> FindConsumersFromControllerTypes()
......@@ -112,7 +110,7 @@ namespace DotNetCore.CAP
return executorDescriptorList;
}
protected IEnumerable<ConsumerExecutorDescriptor> GetTopicAttributesDescription(TypeInfo typeInfo)
protected IEnumerable<ConsumerExecutorDescriptor> GetTopicAttributesDescription(TypeInfo typeInfo, TypeInfo serviceTypeInfo = null)
{
foreach (var method in typeInfo.DeclaredMethods)
{
......@@ -135,7 +133,7 @@ namespace DotNetCore.CAP
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
private static ConsumerExecutorDescriptor InitDescriptor(
TopicAttribute attr,
MethodInfo methodInfo,
TypeInfo implType)
TypeInfo implType,
TypeInfo serviceTypeInfo)
{
var descriptor = new ConsumerExecutorDescriptor
{
Attribute = attr,
MethodInfo = methodInfo,
ImplTypeInfo = implType
ImplTypeInfo = implType,
ServiceTypeInfo = serviceTypeInfo
};
return descriptor;
......
......@@ -2,6 +2,8 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions;
using Microsoft.Extensions.DependencyInjection;
......@@ -38,9 +40,20 @@ namespace DotNetCore.CAP.Internal
using (var scope = _serviceProvider.CreateScope())
{
var provider = scope.ServiceProvider;
var provider = scope.ServiceProvider;
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 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