|
| 1 | +// SPDX-FileCopyrightText: © 2022 MONAI Consortium |
| 2 | +// SPDX-License-Identifier: Apache License 2.0 |
| 3 | + |
| 4 | +using System.IO.Abstractions; |
| 5 | +using System.Reflection; |
| 6 | +using Ardalis.GuardClauses; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Monai.Deploy.Messaging.API; |
| 9 | +using Monai.Deploy.Messaging.Configuration; |
| 10 | + |
| 11 | +namespace Monai.Deploy.Messaging |
| 12 | +{ |
| 13 | + public static class IServiceCollectionExtensions |
| 14 | + { |
| 15 | + private static IFileSystem? s_fileSystem; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Configures all dependencies required for the MONAI Deploy Message Broker Subscriber Service. |
| 19 | + /// </summary> |
| 20 | + /// <param name="services">Instance of <see cref="IServiceCollection"/>.</param> |
| 21 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 22 | + /// <returns>Instance of <see cref="IServiceCollection"/>.</returns> |
| 23 | + /// <exception cref="ConfigurationException"></exception> |
| 24 | + public static IServiceCollection AddMonaiDeployMessageBrokerSubscriberService(this IServiceCollection services, string fullyQualifiedTypeName) |
| 25 | + => AddMonaiDeployMessageBrokerSubscriberService(services, fullyQualifiedTypeName, new FileSystem()); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Configures all dependencies required for the MONAI Deploy Message Broker Subscriber Service. |
| 29 | + /// </summary> |
| 30 | + /// <param name="services">Instance of <see cref="IServiceCollection"/>.</param> |
| 31 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 32 | + /// <param name="fileSystem">Instance of <see cref="IFileSystem"/>.</param> |
| 33 | + /// <returns>Instance of <see cref="IServiceCollection"/>.</returns> |
| 34 | + /// <exception cref="ConfigurationException"></exception> |
| 35 | + public static IServiceCollection AddMonaiDeployMessageBrokerSubscriberService(this IServiceCollection services, string fullyQualifiedTypeName, IFileSystem fileSystem) |
| 36 | + => Add<IMessageBrokerSubscriberService, SubscriberServiceRegistrationBase>(services, fullyQualifiedTypeName, fileSystem); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Configures all dependencies required for the MONAI Deploy Message Broker Publisher Service. |
| 40 | + /// </summary> |
| 41 | + /// <param name="services">Instance of <see cref="IServiceCollection"/>.</param> |
| 42 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 43 | + /// <returns>Instance of <see cref="IServiceCollection"/>.</returns> |
| 44 | + /// <exception cref="ConfigurationException"></exception> |
| 45 | + public static IServiceCollection AddMonaiDeployMessageBrokerPublisherService(this IServiceCollection services, string fullyQualifiedTypeName) |
| 46 | + => AddMonaiDeployMessageBrokerPublisherService(services, fullyQualifiedTypeName, new FileSystem()); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Configures all dependencies required for the MONAI Deploy Message Broker Publisher Service. |
| 50 | + /// </summary> |
| 51 | + /// <param name="services">Instance of <see cref="IServiceCollection"/>.</param> |
| 52 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 53 | + /// <param name="fileSystem">Instance of <see cref="IFileSystem"/>.</param> |
| 54 | + /// <returns>Instance of <see cref="IServiceCollection"/>.</returns> |
| 55 | + /// <exception cref="ConfigurationException"></exception> |
| 56 | + public static IServiceCollection AddMonaiDeployMessageBrokerPublisherService(this IServiceCollection services, string fullyQualifiedTypeName, IFileSystem fileSystem) |
| 57 | + => Add<IMessageBrokerPublisherService, PublisherServiceRegistrationBase>(services, fullyQualifiedTypeName, fileSystem); |
| 58 | + |
| 59 | + private static IServiceCollection Add<T, U>(this IServiceCollection services, string fullyQualifiedTypeName, IFileSystem fileSystem) where U : ServiceRegistrationBase |
| 60 | + { |
| 61 | + Guard.Against.NullOrWhiteSpace(fullyQualifiedTypeName, nameof(fullyQualifiedTypeName)); |
| 62 | + Guard.Against.Null(fileSystem, nameof(fileSystem)); |
| 63 | + |
| 64 | + s_fileSystem = fileSystem; |
| 65 | + |
| 66 | + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; |
| 67 | + |
| 68 | + try |
| 69 | + { |
| 70 | + var serviceAssembly = LoadAssemblyFromDisk(GetAssemblyName(fullyQualifiedTypeName)); |
| 71 | + var serviceRegistrationType = serviceAssembly.GetTypes().FirstOrDefault(p => p.BaseType == typeof(U)); |
| 72 | + |
| 73 | + if (serviceRegistrationType is null || Activator.CreateInstance(serviceRegistrationType, fullyQualifiedTypeName) is not U serviceRegistrar) |
| 74 | + { |
| 75 | + throw new ConfigurationException($"Service registrar cannot be found for the configured plug-in '{fullyQualifiedTypeName}'."); |
| 76 | + } |
| 77 | + |
| 78 | + if (!IsSupportedType<T>(fullyQualifiedTypeName, serviceAssembly)) |
| 79 | + { |
| 80 | + throw new ConfigurationException($"The configured type '{fullyQualifiedTypeName}' does not implement the {typeof(T).Name} interface."); |
| 81 | + } |
| 82 | + |
| 83 | + return serviceRegistrar.Configure(services); |
| 84 | + } |
| 85 | + finally |
| 86 | + { |
| 87 | + AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static bool IsSupportedType<T>(string fullyQualifiedTypeName, Assembly storageServiceAssembly) |
| 92 | + { |
| 93 | + Guard.Against.NullOrWhiteSpace(fullyQualifiedTypeName, nameof(fullyQualifiedTypeName)); |
| 94 | + Guard.Against.Null(storageServiceAssembly, nameof(storageServiceAssembly)); |
| 95 | + |
| 96 | + var storageServiceType = Type.GetType(fullyQualifiedTypeName, assemblyeName => storageServiceAssembly, null, false); |
| 97 | + |
| 98 | + return storageServiceType is not null && |
| 99 | + storageServiceType.GetInterfaces().Contains(typeof(T)); |
| 100 | + } |
| 101 | + |
| 102 | + private static string GetAssemblyName(string fullyQualifiedTypeName) |
| 103 | + { |
| 104 | + var assemblyNameParts = fullyQualifiedTypeName.Split(',', StringSplitOptions.None); |
| 105 | + if (assemblyNameParts.Length < 2 || string.IsNullOrWhiteSpace(assemblyNameParts[1])) |
| 106 | + { |
| 107 | + throw new ConfigurationException($"The configured service type '{fullyQualifiedTypeName}' is not a valid fully qualified type name. E.g. {MessageBrokerServiceConfiguration.DefaultPublisherAssemblyName}") |
| 108 | + { |
| 109 | + HelpLink = "https://docs.microsoft.com/en-us/dotnet/standard/assembly/find-fully-qualified-name" |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + return assemblyNameParts[1].Trim(); |
| 114 | + } |
| 115 | + |
| 116 | + private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) |
| 117 | + { |
| 118 | + Guard.Against.Null(args, nameof(args)); |
| 119 | + |
| 120 | + var requestedAssemblyName = new AssemblyName(args.Name); |
| 121 | + return LoadAssemblyFromDisk(requestedAssemblyName.Name); |
| 122 | + } |
| 123 | + |
| 124 | + private static Assembly LoadAssemblyFromDisk(string assemblyName) |
| 125 | + { |
| 126 | + Guard.Against.NullOrWhiteSpace(assemblyName, nameof(assemblyName)); |
| 127 | + Guard.Against.Null(s_fileSystem, nameof(s_fileSystem)); |
| 128 | + |
| 129 | + if (!s_fileSystem.Directory.Exists(SR.PlugInDirectoryPath)) |
| 130 | + { |
| 131 | + throw new ConfigurationException($"Plug-in directory '{SR.PlugInDirectoryPath}' cannot be found."); |
| 132 | + } |
| 133 | + |
| 134 | + var assemblyFilePath = s_fileSystem.Path.Combine(SR.PlugInDirectoryPath, $"{assemblyName}.dll"); |
| 135 | + if (!s_fileSystem.File.Exists(assemblyFilePath)) |
| 136 | + { |
| 137 | + throw new ConfigurationException($"The configured plug-in '{assemblyFilePath}' cannot be found."); |
| 138 | + } |
| 139 | + |
| 140 | + var asesmblyeData = s_fileSystem.File.ReadAllBytes(assemblyFilePath); |
| 141 | + return Assembly.Load(asesmblyeData); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments