From 5d718b033f79e930ff0cda4a40794cfa8f5bd4b8 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Wed, 18 Nov 2020 15:55:58 +0900 Subject: [PATCH] Use ActivatorUtilities with the service provider instead of Activator --- src/MagicOnion.Server/Hubs/Group.cs | 12 ++++++------ src/MagicOnion.Server/MagicOnionEngine.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/MagicOnion.Server/Hubs/Group.cs b/src/MagicOnion.Server/Hubs/Group.cs index 8be9eac36..11af130ac 100644 --- a/src/MagicOnion.Server/Hubs/Group.cs +++ b/src/MagicOnion.Server/Hubs/Group.cs @@ -10,16 +10,16 @@ namespace MagicOnion.Server.Hubs [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] public class GroupConfigurationAttribute : Attribute { - Type type; + public Type FactoryType { get; } public GroupConfigurationAttribute(Type groupRepositoryFactoryType) { - this.type = groupRepositoryFactoryType; - } + if (!typeof(IGroupRepositoryFactory).IsAssignableFrom(groupRepositoryFactoryType) && (groupRepositoryFactoryType.IsAbstract || groupRepositoryFactoryType.IsInterface)) + { + throw new ArgumentException("A Group repository factory must implement IGroupRepositoryFactory interface and must be a concrete class."); + } - public IGroupRepositoryFactory Create() - { - return (IGroupRepositoryFactory)Activator.CreateInstance(type)!; + this.FactoryType = groupRepositoryFactoryType; } } diff --git a/src/MagicOnion.Server/MagicOnionEngine.cs b/src/MagicOnion.Server/MagicOnionEngine.cs index 4caa95f1f..f565f3cbe 100644 --- a/src/MagicOnion.Server/MagicOnionEngine.cs +++ b/src/MagicOnion.Server/MagicOnionEngine.cs @@ -169,7 +169,7 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IServiceP var attr = classType.GetCustomAttribute(true); if (attr != null) { - factory = attr.Create(); + factory = (IGroupRepositoryFactory)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, attr.FactoryType); } else {