Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update Connect WPF Sample? #3724

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions dotnet-interactive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactiv
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactive.Parsing.Tests", "src\Microsoft.DotNet.Interactive.Parsing.Tests\Microsoft.DotNet.Interactive.Parsing.Tests.csproj", "{55138BD7-111A-43A5-BFBB-326606C4C1B5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{49E6C2EA-FE81-47DF-B206-F818669BEFB1}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been keeping samples out of the solution. Ideally the samples build against NuGet packages, since most people using them won't be building this code.

EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfConnect", "samples\connect-wpf\WpfConnect.csproj", "{98689A19-6B13-4EA9-8C0E-6CB665322531}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -709,6 +713,18 @@ Global
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x64.Build.0 = Release|Any CPU
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x86.ActiveCfg = Release|Any CPU
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x86.Build.0 = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|x64.ActiveCfg = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|x64.Build.0 = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|x86.ActiveCfg = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Debug|x86.Build.0 = Debug|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|Any CPU.Build.0 = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|x64.ActiveCfg = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|x64.Build.0 = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|x86.ActiveCfg = Release|Any CPU
{98689A19-6B13-4EA9-8C0E-6CB665322531}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -762,6 +778,7 @@ Global
{BF68D266-500C-49AB-80EB-1B673E37E13A} = {B95A8485-8C53-4F56-B0CE-19C0726B5805}
{B12834B8-373E-4932-852B-90E332A4BCED} = {11BA3480-4584-435C-BA9A-8C554DB60E9F}
{55138BD7-111A-43A5-BFBB-326606C4C1B5} = {11BA3480-4584-435C-BA9A-8C554DB60E9F}
{98689A19-6B13-4EA9-8C0E-6CB665322531} = {49E6C2EA-FE81-47DF-B206-F818669BEFB1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D05A9AF-CFFB-4187-8599-574387B76727}
Expand Down
36 changes: 28 additions & 8 deletions samples/connect-wpf/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.CommandLine;
using System.IO.Pipes;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Directives;
using Microsoft.DotNet.Interactive.NamedPipeConnector;
using Microsoft.DotNet.Interactive.PackageManagement;

namespace WpfConnect
{
Expand Down Expand Up @@ -41,7 +45,7 @@ await csharpKernel.SendAsync(new SubmitCode(@$"#r ""{typeof(App).Assembly.Locati
await csharpKernel.SetValueAsync("App", this, GetType());

//Start named pipe
_kernel.AddKernelConnector(new ConnectNamedPipeCommand());
_kernel.AddKernelConnector(new ConnectNamedPipeDirective());
});
}

Expand Down Expand Up @@ -76,23 +80,39 @@ private void SetUpNamedPipeKernelConnection()

private void AddDispatcherMagicCommand(Kernel kernel)
{
var enabledOption = new Option<bool>("--enabled", getDefaultValue: () => true);
var dispatcherCommand = new Command("#!dispatcher", "Enable or disable running code on the Dispatcher")
//// var enabledOption = new KernelDirectiveParameter<bool>("--enabled", getDefaultValue: () => true);
var dispatcherCommand = new KernelActionDirective("#!dispatcher")
{
enabledOption
Description = "Enable or disable running code on the Dispatcher",
Parameters = [
new("--enabled")
{
TypeHint = "bool",
}
],
};

dispatcherCommand.SetHandler(
/*dispatcherCommand.SetHandler(
enabled => RunOnDispatcher = enabled,
enabledOption);
enabledOption);*/

kernel.AddDirective<DispatcherCommand>(dispatcherCommand, (k, kc) => {
RunOnDispatcher = bool.Parse(k.Enabled);

kernel.AddDirective(dispatcherCommand);
return Task.CompletedTask;
});
}

private CSharpKernel RegisterCSharpKernel()
{
var csharpKernel = new CSharpKernel()
.UseNugetDirective()
.UseNugetDirective((k, resolvedPackageReference) =>
{

k.AddAssemblyReferences(resolvedPackageReference
.SelectMany(r => r.AssemblyPaths));
return Task.CompletedTask;
}, false)
.UseKernelHelpers()
.UseWho()
.UseValueSharing()
Expand Down
3 changes: 3 additions & 0 deletions samples/connect-wpf/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<!-- Only here so that the default Directory.Build.props will not be used. -->
</Project>
23 changes: 23 additions & 0 deletions samples/connect-wpf/DispatcherCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.Interactive.Commands;
using System.Threading.Tasks;
using System;

namespace WpfConnect;

public class DispatcherCommand : KernelCommand
{
public DispatcherCommand(
string enabled,
string targetKernelName = null) : base(targetKernelName)
{
if (string.IsNullOrWhiteSpace(enabled))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(enabled));
}

Enabled = enabled;
}

public string Enabled { get; }
}
4 changes: 3 additions & 1 deletion samples/connect-wpf/WpfConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.DotNet.Interactive.CSharp" Version="1.0.0-beta.23403.1" />
<ProjectReference Include="..\..\src\Microsoft.DotNet.Interactive.CSharp\Microsoft.DotNet.Interactive.CSharp.csproj" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be package references and not project references.

<ProjectReference Include="..\..\src\Microsoft.DotNet.Interactive.NamedPipeConnector\Microsoft.DotNet.Interactive.NamedPipeConnector.csproj" />
<ProjectReference Include="..\..\src\Microsoft.DotNet.Interactive.PackageManagement\Microsoft.DotNet.Interactive.PackageManagement.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions samples/connect-wpf/readme.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
}
],
"source": [
"#i \"nuget:I:\\oss\\dotnetinteractive\\artifacts\\packages\\Debug\\Shipping\"\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referencing a hard-coded path won't work for most people, and we don't want to expect people to build the solution in any case.

"#r \"nuget:Microsoft.DotNet.Interactive.Documents,*-*\"\n",
"#r \"nuget:Microsoft.DotNet.Interactive.NamedPipeConnector,*-*\"\n",
"#!connect named-pipe --kernel-name wpf --pipe-name InteractiveWpf"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public override async Task<IEnumerable<Kernel>> ConnectKernelsAsync(

return new Kernel[] { proxyKernel };
}

public static void AddToRootKernel()
{
if (KernelInvocationContext.Current is { } context &&
context.HandlingKernel.RootKernel is CompositeKernel root)
{
root.AddKernelConnector(new ConnectNamedPipeDirective());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<PackageId Condition="'$(PackageId)'==''">Microsoft.DotNet.Interactive.NamedPipeConnectorConnector</PackageId>
<PackageId Condition="'$(PackageId)'==''">Microsoft.DotNet.Interactive.NamedPipeConnector</PackageId>
<NoWarn>$(NoWarn);8002;CS8002</NoWarn>
<NoWarn>$(NoWarn);CA1416</NoWarn> <!-- Ignore: This call site is reachable on all platforms. 'PipeTransmissionMode.Message' is supported on: 'windows'. -->
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!csharp

Microsoft.DotNet.Interactive.NamedPipeConnector.ConnectNamedPipeCommand.AddToRootKernel();
await Microsoft.DotNet.Interactive.NamedPipeConnector.ConnectNamedPipeDirective.AddToRootKernel();

Loading