Community.Wsl.Sdk
0.1.4
See the version list below for details.
dotnet add package Community.Wsl.Sdk --version 0.1.4
NuGet\Install-Package Community.Wsl.Sdk -Version 0.1.4
<PackageReference Include="Community.Wsl.Sdk" Version="0.1.4" />
paket add Community.Wsl.Sdk --version 0.1.4
#r "nuget: Community.Wsl.Sdk, 0.1.4"
// Install Community.Wsl.Sdk as a Cake Addin #addin nuget:?package=Community.Wsl.Sdk&version=0.1.4 // Install Community.Wsl.Sdk as a Cake Tool #tool nuget:?package=Community.Wsl.Sdk&version=0.1.4
Community SDK for Windows Subsystem for Linux
❗ Fork: This repository is based on the awsome work of https://github.com/wslhub/wsl-sdk-dotnet/tree/main/src/Wslhub.Sdk
This project contains a WSL API wrapper for Windows developers who wants to integrate WSL features into existing Windows applications. You can enumerate, query, executing WSL commands via C# classes.
Supported frameworks
- .NET Standard 2.1
- .Net 5
- .Net 6
Supported Operating Systems
- Windows 10 x64 16299 or higher
- Windows 11 x64
How to use
There are two different implementations of IWslApi
:
- A COM-based one that has specific requirements and requires special setup, too
- An alternative implementation that uses
wsl.exe
and (mostly) public information stored in the registry.
It's recommended to use the alternative because it's more versatile and easier to use.
How to install
This package is available on nuget.org
: https://www.nuget.org/packages/Community.Wsl.Sdk
You can add a reference using dotnet / nuget
:
dotnet add package Community.Wsl.Sdk
API
WSL Api
Classes | Description |
---|---|
ManagedWslApi |
Get list of installed linux distributions. |
ManagedCommand |
Execute command in specified linux distribution. |
ComCommand |
Get list of installed linux distributions. |
ComBasedWslApi |
Execute command in specified linux distribution. |
WSL command execution
There are two implementations, one uses the com based api to execute, the other calls wsl.exe
internally.
The com based implementation has proven to be quite limited and buggy.
All implementations implement ICommand
. Feel free to use your favorite DI or mocking framework to help with testing.
Code Example
Basic usage of wsl api
using Community.Wsl.Sdk.Strategies.Api;
// Create instance
var api = new ManagedWslApi();
// When class ComBasedWslApi is used
// Place the code Wsl.InitializeSecurityModel() at the top of your application's Main() method.
// api.InitializeSecurity();
// Check if wsl is supported
bool isSupported = api.IsWslSupported();
// OR check if wsl is supported and also know why not:
string reason;
bool isSupported = api.IsWslSupported(out reason);
// Enumerate distro list
var distros = api.GetDistroList();
// Query default distro details
var defaultDistro = api.GetDefaultDistro();
Basic command execution
using Community.Wsl.Sdk.Strategies.Api;
// Setup
var api = new ManagedWslApi();
var distroName = api.GetDefaultDistro()!.Value.DistroName;
// Get command result
var cmd = new ManagedCommand(
distroName,
"echo",
new string[] { "-n", "test" },
new CommandExecutionOptions() { StdoutDataProcessingMode = DataProcessingMode.String }
);
cmd.Start();
// Wait for the command to finish. Execution is executed asynchronically!
// WaitAndGetResults will fetch the data if instructed or drop it.
var result = cmd.WaitAndGetResults();
// result.Stdout is "test"
How to use com-based IWslApi
(not recommended)
You will need the below items to use the WSL APIs.
- Add an application manifest which describes your application is compatible with the
Windows 10 (GUID:
8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a
) and therequestedExecutionLevel
tag withasInvoker
level. - Create instance of
ComBasedWslApi
- Call
InitializeSecurityModel()
at the top of your application'sMain()
method.
Limitations
Due to the limitation of COM security model requirements of WSL APIs, you can not run this SDK within the Visual Studio Test Explorer or LINQPAD.
To circumvent this technical limitation, instruct ComBasedWslApi
to use a different set of native methods:
bool isTestEnv = ...;
// decide on implementation at run time
BaseNativeMethods nativeMethods = isTestEnv ? new StubNativeMethods() : new Win32NativeMethods();
// use first parameter of constructor to change the behaviour
var api = new ComBasedWslApi(nativeMethods);
Yet another approach is to use a different implementation of IWslApi
altogether.
If you already use mocking frameworks & DI, use them to create a test friedly
class, for example using FakeItEasy
:
var api = A.Fake<IWslApi>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net5.0-windows7.0 is compatible. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net6.0-windows7.0 is compatible. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Win32.Registry (>= 5.0.0)
-
net5.0-windows7.0
- Microsoft.Win32.Registry (>= 5.0.0)
-
net6.0-windows7.0
- Microsoft.Win32.Registry (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.1.0 | 136 | 8/17/2024 |
3.1.0-alpha1 | 108 | 8/17/2024 |
3.0.0 | 1,215 | 6/12/2022 |
3.0.0-alpha2 | 203 | 6/12/2022 |
3.0.0-alpha1 | 183 | 6/12/2022 |
2.0.2 | 551 | 4/17/2022 |
2.0.2-alpha3 | 200 | 4/17/2022 |
2.0.2-alpha2 | 183 | 4/17/2022 |
2.0.2-alpha1 | 193 | 4/17/2022 |
2.0.1 | 451 | 4/17/2022 |
0.1.4 | 356 | 12/27/2021 |
0.1.3 | 284 | 12/27/2021 |
0.1.2 | 287 | 12/26/2021 |
0.1.1 | 288 | 12/25/2021 |
0.1.0 | 278 | 12/25/2021 |
## 0.1.4
Propagate exit code (#3)