예제 #1
0
        private void ExecuteAssembly(HostedPlugin plugin, string input)
        {
            var context      = new CollectibleAssemblyContext();
            var assemblyPath = Path.Combine(plugin.FilePath);

            using (var fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read))
            {
                var assembly = context.LoadFromStream(fs);

                var type          = assembly.GetType("PluginSystem.Plugin");
                var executeMethod = type.GetMethod("Execute");

                var instance = Activator.CreateInstance(type);

                var dic = PluginResponses.GetOrCreate(plugin.Name);

                dic.Add(executeMethod.Invoke(instance, new object[] { input }).ToString());
            }

            context.Unload();
        }
예제 #2
0
        private async Task <Assembly> LoadAssemblyFromWebUrl(WebLazyAssembly lazywebassembly)
        {
            var assemblyToLoad = lazywebassembly.Url;

            Console.WriteLine("TRY LOAD " + assemblyToLoad);
            Assembly newLoadedAssembly;

            foreach (var loaded in alreadyLazyLoaded)
            {
                Console.WriteLine("  ALREADY LOADED " + loaded);
            }


            if (!alreadyLazyLoaded.ContainsKey(assemblyToLoad))
            {
                var client  = new HttpClient();
                var content = await client.GetStreamAsync(assemblyToLoad);

                Console.WriteLine("  DOWNLOADED " + assemblyToLoad);
                // The runtime loads assemblies into an isolated context by default. As a result,
                // assemblies that are loaded via Assembly.Load aren't available in the app's context
                // AKA the default context. To work around this, we explicitly load the assemblies
                // into the default app context.

                lock (alreadyLazyLoaded)
                {
                    // BECAUSE of async operation during download another Component i.e. could try to load
                    if (!alreadyLazyLoaded.ContainsKey(assemblyToLoad))
                    {
                        var context = new CollectibleAssemblyContext();
                        // load
                        var loadedAssembly = context.LoadFromStream(content);
                        //var loadedAssembly = AssemblyLoadContext.Default.LoadFromStream(content);
                        Console.WriteLine("  LOADED " + assemblyToLoad);

                        // init
                        var iPluginType = typeof(IPlugin);
                        var pluginClass = loadedAssembly.GetTypes().SingleOrDefault(t => iPluginType.IsAssignableFrom(t));

                        if (pluginClass != null)
                        {
                            var plugin = (IPlugin)Activator.CreateInstance(pluginClass);

                            var x = new ServiceCollection();
                            plugin.Init(x);
                            container.AddFrom(x);
                        }


                        // depency injection add services
                        container.AddAllInterfaceServices(loadedAssembly);

                        // avoid reloading
                        alreadyLazyLoaded.Add(assemblyToLoad, loadedAssembly);
                        newLoadedAssembly = loadedAssembly;
                    }
                    else
                    {
                        newLoadedAssembly = alreadyLazyLoaded[assemblyToLoad];
                    }
                }
            }
            else
            {
                newLoadedAssembly = alreadyLazyLoaded[assemblyToLoad];
            }

            return(newLoadedAssembly);
        }