private static bool TryGetExportExternal(BaseOptions options, out INLUServiceFactory serviceFactory)
        {
            var assemblyName = $"dotnet-nlu-{options.Service}";

            string getAssemblyPath()
            {
                var paths = new string[9];

                paths[0] = AppDomain.CurrentDomain.BaseDirectory;
                Array.Fill(paths, "..", 1, paths.Length - 2);
                paths[paths.Length - 1] = assemblyName;
                var searchRoot = Path.GetFullPath(Path.Combine(paths));

                if (!Directory.Exists(searchRoot))
                {
                    return(null);
                }

                return(Directory.GetFiles(searchRoot, $"{assemblyName}.dll", SearchOption.AllDirectories).FirstOrDefault());
            }

            var assemblyPath = options.IncludePath ?? getAssemblyPath();

            if (assemblyPath == null)
            {
                serviceFactory = null;
                return(false);
            }

            return(new ContainerConfiguration()
                   .WithAssembly(Assembly.LoadFrom(assemblyPath))
                   .CreateContainer()
                   .TryGetExport(options.Service, out serviceFactory));
        }
Beispiel #2
0
        public static INLUTestClient CreateTestInstance(BaseOptions options, IConfiguration configuration, string settingsPath = null)
        {
            if (!ServiceResolver.TryResolve <INLUClientFactory>(options, out var serviceFactory))
            {
                throw new InvalidOperationException($"Invalid service type '{options.Service}'.");
            }

            return(serviceFactory.CreateTestInstance(configuration, settingsPath));
        }
Beispiel #3
0
        public static bool TryResolve <T>(BaseOptions options, out T instance)
        {
            string getAssemblyPath()
            {
                var assemblyName        = $"dotnet-nlu-{options.Service}.dll";
                var defaultSearchPath   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "providers");
                var defaultAssemblyPath = Directory.GetFiles(defaultSearchPath, assemblyName, SearchOption.AllDirectories).FirstOrDefault();

                if (defaultAssemblyPath != null)
                {
                    return(defaultAssemblyPath);
                }

                var paths = new string[8];

                paths[0] = AppDomain.CurrentDomain.BaseDirectory;
                Array.Fill(paths, "..", 1, paths.Length - 1);
                var searchRoot = Path.GetFullPath(Path.Combine(paths));

                if (!Directory.Exists(searchRoot))
                {
                    return(null);
                }

                try
                {
                    return(Directory.GetFiles(searchRoot, assemblyName, SearchOption.AllDirectories).FirstOrDefault());
                }
                catch (UnauthorizedAccessException)
                {
                    return(null);
                }
            }

            var includePath = options.IncludePath != null?Path.GetFullPath(options.IncludePath) : null;

            var assemblyPath = includePath ?? getAssemblyPath();

            if (assemblyPath == null)
            {
                instance = default(T);
                return(false);
            }

            var assembly = PluginLoader.CreateFromAssemblyFile(
                assemblyPath,
                PluginLoaderOptions.PreferSharedTypes)
                           .LoadDefaultAssembly();

            return(new ContainerConfiguration()
                   .WithAssembly(assembly)
                   .CreateContainer()
                   .TryGetExport(options.Service, out instance));
        }
Beispiel #4
0
        public static bool TryResolve <T>(BaseOptions options, out T instance)
        {
            string getAssemblyPath()
            {
                var assemblyName      = $"dotnet-nlu-{options.Service}.dll";
                var defaultSearchPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "providers", options.Service, assemblyName);

                if (File.Exists(defaultSearchPath))
                {
                    return(defaultSearchPath);
                }

                var paths = new string[9];

                paths[0] = AppDomain.CurrentDomain.BaseDirectory;
                Array.Fill(paths, "..", 1, paths.Length - 2);
                paths[paths.Length - 1] = assemblyName;
                var searchRoot = Path.GetFullPath(Path.Combine(paths));

                if (!Directory.Exists(searchRoot))
                {
                    return(null);
                }

                return(Directory.GetFiles(searchRoot, assemblyName, SearchOption.AllDirectories).FirstOrDefault());
            }

            var assemblyPath = options.IncludePath ?? getAssemblyPath();

            if (assemblyPath == null)
            {
                instance = default(T);
                return(false);
            }

            var providerDirectory   = Path.GetDirectoryName(assemblyPath);
            var assemblyLoadContext = new ProviderAssemblyLoadContext(providerDirectory);

            return(new ContainerConfiguration()
                   .WithAssembly(assemblyLoadContext.LoadFromAssemblyPath(assemblyPath))
                   .CreateContainer()
                   .TryGetExport(options.Service, out instance));
        }
        public static INLUService Create(BaseOptions options, IConfiguration configuration, string settingsPath = null)
        {
            var assemblies = new[]
            {
                typeof(LuisNLUServiceFactory).Assembly,
                typeof(LexNLUServiceFactory).Assembly,
            };

            var foundExport = new ContainerConfiguration()
                              .WithAssemblies(assemblies)
                              .CreateContainer()
                              .TryGetExport <INLUServiceFactory>(options.Service, out var serviceFactory);

            if (!foundExport && !TryGetExportExternal(options, out serviceFactory))
            {
                throw new ArgumentException($"Invalid service type '{options.Service}'.");
            }

            return(serviceFactory.CreateInstance(configuration, settingsPath));
        }