public async Task pound_r_is_not_split_into_separate_command_from_csharp_code()
        {
            var receivedCommands = new List <IKernelCommand>();

            using var kernel = new CSharpKernel();

            kernel.AddMiddleware((command, context, next) =>
            {
                receivedCommands.Add(command);
                return(Task.CompletedTask);
            });

            kernel.UseNugetDirective();

            var path = Path.GetTempFileName();
            var poundR_and_usingStatement = $@"#r ""{path}""{Environment.NewLine}using Some.Namespace;";
            var nextSubmission            = "// the code";

            kernel.DeferCommand(new SubmitCode(poundR_and_usingStatement));

            await kernel.SubmitCodeAsync(nextSubmission);

            receivedCommands
            .Cast <SubmitCode>()
            .Select(c => c.Code.Trim())
            .Should()
            .BeEquivalentSequenceTo(
                poundR_and_usingStatement,
                nextSubmission);
        }
Пример #2
0
        private CSharpKernel RegisterCSharpKernel()
        {
            var csharpKernel = new CSharpKernel()
                               .UseDefaultFormatting()
                               .UseNugetDirective()
                               .UseKernelHelpers()
                               .UseWho()
                               .UseDotNetVariableSharing()
                               //This is added locally
                               .UseWpf();

            _Kernel.Add(csharpKernel);

            csharpKernel.AddMiddleware(async(KernelCommand command, KernelInvocationContext context, KernelPipelineContinuation next) =>
            {
                if (RunOnDispatcher)
                {
                    await Dispatcher.InvokeAsync(async() => await next(command, context));
                }
                else
                {
                    await next(command, context);
                }
            });

            return(csharpKernel);
        }
        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);
                    }
                })
            };
Пример #4
0
        public async Task Script_state_is_available_within_middleware_pipeline()
        {
            var variableCountBeforeEvaluation = 0;
            var variableCountAfterEvaluation  = 0;

            using var kernel = new CSharpKernel();

            kernel.AddMiddleware(async(command, context, next) =>
            {
                var k = context.HandlingKernel as CSharpKernel;

                await next(command, context);

                variableCountAfterEvaluation = k.ScriptState.Variables.Length;
            });

            await kernel.SendAsync(new SubmitCode("var x = 1;"));

            variableCountBeforeEvaluation.Should().Be(0);
            variableCountAfterEvaluation.Should().Be(1);
        }