Beispiel #1
0
        private async Task <IPluginProxy> InternalLoadPluginAsync(PluginPath pluginPath, Uri pluginApiUri, CancellationToken cancellationToken)
        {
            var pluginId = pluginPath.GetPluginId();
            var token    = Token.NewFor(pluginId);

            using (_logger.Time($"Loading plugin '{pluginId}'"))
            {
                var appDomainSetup = new AppDomainSetup
                {
                    ShadowCopyFiles    = "true",
                    LoaderOptimization = LoaderOptimization.MultiDomain,
                };
                var configFilePath = $"{pluginPath.Value}.config";
                if (File.Exists(configFilePath))
                {
                    appDomainSetup.ConfigurationFile = configFilePath;
                }

                var appDomain = _appDomainManager.CreateDomain(pluginId.Value, null, appDomainSetup);

                var pluginHost     = appDomain.CreateInstanceAndUnwrap(Assembly.GetAssembly(typeof(PluginHostClient)).FullName, typeof(PluginHostClient).ToString()) as PluginHostClient;
                var autoResetEvent = new AutoResetEvent(false);
                var clientPort     = TcpHelper.GetFreePort();

                try
                {
                    pluginHost.Launch(pluginPath.Value, appDomain, pluginApiUri, clientPort, token.Value);
                    autoResetEvent.Set();
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Plugin '{pluginId}' failed");
                    AppDomain.Unload(appDomain);
                    throw;
                }

                var pluginProxy = new PluginProxy(
                    pluginId,
                    appDomain,
                    new Plugin(_logger, new Uri($"http://127.0.0.1:{clientPort}"),
                               _restClient));

                await pluginProxy.Plugin.PingAsync(cancellationToken).ConfigureAwait(false);

                return(pluginProxy);
            }
        }
Beispiel #2
0
        private async Task<IPluginProxy> InternalLoadPluginAsync(PluginPath pluginPath, Uri pluginApiUri, CancellationToken cancellationToken)
        {
            var pluginId = pluginPath.GetPluginId();
            var token = Token.NewFor(pluginId);

            using (_logger.Time($"Loading plugin '{pluginId}'"))
            {
                var appDomainSetup = new AppDomainSetup
                {
                    ShadowCopyFiles = "true",
                    LoaderOptimization = LoaderOptimization.MultiDomain,
                };
                var configFilePath = $"{pluginPath.Value}.config";
                if (File.Exists(configFilePath))
                {
                    appDomainSetup.ConfigurationFile = configFilePath;
                }

                var appDomain = _appDomainManager.CreateDomain(pluginId.Value, null, appDomainSetup);

                var pluginHost = appDomain.CreateInstanceAndUnwrap(Assembly.GetAssembly(typeof(PluginHostClient)).FullName, typeof(PluginHostClient).ToString()) as PluginHostClient;
                var autoResetEvent = new AutoResetEvent(false);
                var clientPort = TcpHelper.GetFreePort();

                try
                {
                    pluginHost.Launch(pluginPath.Value, appDomain, pluginApiUri, clientPort, token.Value);
                    autoResetEvent.Set();
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Plugin '{pluginId}' failed");
                    AppDomain.Unload(appDomain);
                    throw;
                }

                var pluginProxy = new PluginProxy(
                    pluginId,
                    appDomain,
                    new Plugin(_logger, new Uri($"http://127.0.0.1:{clientPort}"),
                    _restClient));

                await pluginProxy.Plugin.PingAsync(cancellationToken).ConfigureAwait(false);

                return pluginProxy;
            }
        }