public static CSharpKernel UseAspNetCore(this CSharpKernel kernel) { InteractiveHost interactiveHost = null; var directive = new Command("#!aspnet", "Activate ASP.NET Core") { Handler = CommandHandler.Create(async() => { if (interactiveHost is {}) { return; } var interactiveLoggerProvider = new InteractiveLoggerProvider(); kernel.AddMiddleware(async(command, context, next) => { // REVIEW: Is there a way to log even when there's no command in progress? // This is currently necessary because the #!log command uses KernelInvocationContext.Current in // its LogEvents.Subscribe callback and KernelInvocationContext.Current is backed by an AsyncLocal. // Is there a way to log to diagnostic output without KernelInvocationContext.Current? using (command is SubmitCode ? interactiveLoggerProvider.SubscribePocketLogerWithCurrentEC() : null) { await next(command, context).ConfigureAwait(false); } }); // The middleware doesn't cover the current command's executions so we need this to capture startup logs. using (interactiveLoggerProvider.SubscribePocketLogerWithCurrentEC()) { // We could try to manage the host's lifetime, but for now just stop the kernel if you want to stop the host. interactiveHost = new InteractiveHost(interactiveLoggerProvider); var startHostTask = interactiveHost.StartAsync(); var rDirectives = string.Join(Environment.NewLine, _references.Select(a => $"#r \"{a.Location}\"")); var usings = string.Join(Environment.NewLine, _namespaces.Select(ns => $"using {ns};")); await kernel.SendAsync(new SubmitCode($"{rDirectives}{Environment.NewLine}{usings}"), CancellationToken.None).ConfigureAwait(false); await startHostTask.ConfigureAwait(false); var httpClient = HttpClientFormatter.CreateEnhancedHttpClient(interactiveHost.Address, interactiveLoggerProvider); await kernel.SetValueAsync <IApplicationBuilder>("App", interactiveHost.App).ConfigureAwait(false); await kernel.SetValueAsync <IEndpointRouteBuilder>("Endpoints", interactiveHost.Endpoints).ConfigureAwait(false); await kernel.SetValueAsync <HttpClient>("HttpClient", httpClient).ConfigureAwait(false); } }) };
public async Task Completions_suggest_existing_DataFrame_variables() { var kernel = new CSharpKernel() .UseNugetDirective(); await new DataFrameKernelExtension().OnLoadAsync(kernel); await kernel.SubmitCodeAsync($@" #r ""{typeof(DataFrame).Assembly.Location}"" using Microsoft.Data.Analysis; "); var dataFrameVariableName = "myDataFrame"; await kernel.SetValueAsync(dataFrameVariableName, CreateDataFrame()); var code = "#!linqify "; var result = await kernel.SendAsync(new RequestCompletions(code, new LinePosition(0, code.Length))); result.KernelEvents.ToSubscribedList().Should().ContainSingle <CompletionsProduced>() .Which .Completions .Select(c => c.DisplayText) .Should() .Contain(dataFrameVariableName); }
private static async Task <CSharpKernel> CreateKernelAndGenerateType( string magicCommand = "#!linqify frame") { var kernel = new CSharpKernel() .UseNugetDirective(); await new DataFrameKernelExtension().OnLoadAsync(kernel); await kernel.SubmitCodeAsync($@" #r ""{typeof(DataFrame).Assembly.Location}"" using Microsoft.Data.Analysis; "); await kernel.SetValueAsync("frame", CreateDataFrame()); await kernel.SubmitCodeAsync(magicCommand); return(kernel); }
public async Task The_generated_source_code_can_be_displayed() { var kernel = new CSharpKernel() .UseNugetDirective(); await new DataFrameKernelExtension().OnLoadAsync(kernel); await kernel.SubmitCodeAsync($@" #r ""{typeof(DataFrame).Assembly.Location}"" using Microsoft.Data.Analysis; "); await kernel.SetValueAsync("frame", CreateDataFrame()); var result = await kernel.SubmitCodeAsync("#!linqify frame --show-code"); result.KernelEvents .ToSubscribedList() .Should() .NotContainErrors(); result.KernelEvents .ToSubscribedList() .Should() .ContainSingle <DisplayedValueProduced>() .Which .FormattedValues .Should() .ContainSingle() .Which .Value .Should() .ContainAll( "public class DataFrame_", "public System.String name => "); }