private async Task LoadFromAssembly( FileInfo assemblyFile, IKernel kernel, KernelInvocationContext context) { if (assemblyFile == null) { throw new ArgumentNullException(nameof(assemblyFile)); } if (kernel == null) { throw new ArgumentNullException(nameof(kernel)); } if (!assemblyFile.Exists) { throw new ArgumentException($"File {assemblyFile.FullName} doesn't exist", nameof(assemblyFile)); } bool loadExtensions; lock (_lock) { loadExtensions = _loadedAssemblies.Add(AssemblyName.GetAssemblyName(assemblyFile.FullName)); } if (loadExtensions) { var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFile.FullName); var extensionTypes = assembly .ExportedTypes .Where(t => t.CanBeInstantiated() && typeof(IKernelExtension).IsAssignableFrom(t)) .ToArray(); foreach (var extensionType in extensionTypes) { var extension = (IKernelExtension)Activator.CreateInstance(extensionType); var displayed = await context.DisplayAsync( $"Loading kernel extension \"{extensionType.Name}\" from assembly {assemblyFile.FullName}"); try { await extension.OnLoadAsync(kernel); displayed.Update( $"Loaded kernel extension \"{extensionType.Name}\" from assembly {assemblyFile.FullName}"); } catch (Exception e) { context.Publish(new ErrorProduced( $"Failed to load kernel extension \"{extensionType.Name}\" from assembly {assemblyFile.FullName}", context.Command)); context.Fail(new KernelExtensionLoadException(e)); } } } }
public virtual async Task HandleAsync( SubmitCode submitCode, KernelInvocationContext context) { await using var connection = OpenConnection(); if (connection.State != ConnectionState.Open) { await connection.OpenAsync(); } await using var dbCommand = connection.CreateCommand(); dbCommand.CommandText = submitCode.Code; var results = Execute(dbCommand); await context.DisplayAsync(results); }
public async void ProgressDataAdding(object sender, DataAddingEventArgs e) { if (e.ItemAdded is ProgressRecord record) { if (_progresses.TryGetValue(record.ActivityId, out DisplayedValue displayedValue)) { displayedValue.Update(record); if (record.RecordType == ProgressRecordType.Completed) { _progresses.TryRemove(record.ActivityId, out DisplayedValue _); } } else { DisplayedValue dv = await _context.DisplayAsync(record); _progresses[record.ActivityId] = dv; } } }