这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions WorkspaceServer.Tests/Kernel/CSharpKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,30 @@ public async Task OnLoadAsync(IKernel kernel)
.ContainSingle(e => e.Value is CodeSubmissionEvaluated &&
e.Value.As<CodeSubmissionEvaluated>().Code.Contains("using System.Reflection;"));
}

[Fact]
public async Task It_can_reuse_loaded_assemblies()
{
var kernel = CreateKernel();

await kernel.SendAsync(new SubmitCode("#r \"nuget:Microsoft.ML.DataView, 1.3.1\""));
await kernel.SendAsync(new SubmitCode(@"using Microsoft.ML;
void DoIt(IDataView view)
{
}"));
await kernel.SendAsync(new SubmitCode(@"
IDataView dv = null;
DoIt(dv);
"));

KernelEvents.ValuesOnly()
.Last()
.Should()
.BeOfType<CodeSubmissionEvaluationFailed>()
.Which
.Message
.Should()
.Be("(1,1): error CS0103: The name 'aaaadd' does not exist in the current context");
}
}
}
8 changes: 5 additions & 3 deletions WorkspaceServer/Kernel/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using WorkspaceServer.Servers.Scripting;
using CompletionItem = Microsoft.DotNet.Interactive.CompletionItem;
using Task = System.Threading.Tasks.Task;
using Microsoft.CodeAnalysis.Scripting.Hosting;

namespace WorkspaceServer.Kernel
{
Expand All @@ -40,11 +41,13 @@ public class CSharpKernel : KernelBase

private StringBuilder _inputBuffer = new StringBuilder();
private ImmutableArray<MetadataReference> _metadataReferences;
private readonly InteractiveAssemblyLoader _loader;
private WorkspaceFixture _fixture;

public CSharpKernel()
{
_metadataReferences = ImmutableArray<MetadataReference>.Empty;
_loader = new InteractiveAssemblyLoader();
SetupScriptOptions();
}

Expand Down Expand Up @@ -131,9 +134,8 @@ private async Task HandleSubmitCode(
{
if (_scriptState == null)
{
_scriptState = await CSharpScript.RunAsync(
code,
ScriptOptions);
var script = CSharpScript.Create(code, ScriptOptions, assemblyLoader: _loader);
_scriptState = await script.RunAsync();
}
else
{
Expand Down
14 changes: 13 additions & 1 deletion WorkspaceServer/Kernel/CSharpKernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System.CommandLine;
using System.CommandLine.Invocation;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
Expand All @@ -12,6 +14,7 @@

namespace WorkspaceServer.Kernel
{

public static class CSharpKernelExtensions
{
public static CSharpKernel UseDefaultRendering(
Expand Down Expand Up @@ -63,7 +66,16 @@ public static CSharpKernel UseNugetDirective(this CSharpKernel kernel)
var refs = await restoreContext.AddPackage(package.PackageName, package.PackageVersion);
if (refs != null)
{
kernel.AddMetatadaReferences(refs);
var directives = new StringBuilder();
foreach (var reference in refs)
{
if (reference is PortableExecutableReference pe)
{
directives.AppendLine($"#r \"{pe.FilePath}\"");
}
}

await kernel.SendAsync(new SubmitCode(directives.ToString(), kernel.Name));
}

context.OnNext(new NuGetPackageAdded(package));
Expand Down