doc/docs/api-reference/csharp/core-classes/session.md
Represents a WSL-backed container host session.
public sealed class Session : IDisposable
{
public Session(SessionSettings settings);
public event SessionTerminationHandler Terminated;
public event ProcessCrashHandler ProcessCrashed;
public void Start();
public void Terminate();
public Container CreateContainer(ContainerSettings containerSettings);
public void PullImage(PullImageOptions options);
public IAsyncActionWithProgress<ImageProgress> PullImageAsync(PullImageOptions options);
public void ImportImage(string path, string imageName);
public IAsyncActionWithProgress<ImageProgress> ImportImageAsync(string path, string imageName);
public void LoadImage(string path);
public IAsyncActionWithProgress<ImageProgress> LoadImageAsync(string path);
public void PushImage(PushImageOptions options);
public IAsyncActionWithProgress<ImageProgress> PushImageAsync(PushImageOptions options);
public void DeleteImage(string nameOrId);
public void TagImage(TagImageOptions options);
public void CreateVhdVolume(VhdOptions options);
public void DeleteVhdVolume(string name);
public string Authenticate(Uri serverAddress, string username, string password);
public IReadOnlyList<ImageInfo> GetImages();
public void Dispose();
}
var session = new Session(sessionSettings);
Starts the session VM and registers the internal termination wait.
session.Start();
Terminates the session.
session.Terminate();
Creates a container object owned by the session.
Container container = session.CreateContainer(containerSettings);
Synchronous image pull.
session.PullImage(new PullImageOptions("docker.io/library/alpine:latest"));
Awaitable pull with progress.
var pull = session.PullImageAsync(new PullImageOptions("docker.io/library/alpine:latest"));
pull.Progress = (op, progress) =>
Console.WriteLine($"pull: {progress.Status} {progress.Id} {progress.CurrentBytes}/{progress.TotalBytes}");
await pull;
Synchronous image import from a file path.
session.ImportImage(@"C:\images\demo.tar", "demo:imported");
Imports an image tarball from a file path.
var importOp = session.ImportImageAsync(@"C:\images\demo.tar", "demo:imported");
importOp.Progress = (op, progress) =>
Console.WriteLine($"import: {progress.Status} {progress.Id}");
await importOp;
Synchronous image load from disk.
session.LoadImage(@"C:\images\docker-save.tar");
Loads an image archive from disk.
var loadOp = session.LoadImageAsync(@"C:\images\docker-save.tar");
loadOp.Progress = (op, progress) =>
Console.WriteLine($"load: {progress.Status} {progress.Id}");
await loadOp;
Synchronous image push to a registry.
session.PushImage(new PushImageOptions("registry.example.com/demo:latest", authToken));
Pushes an image to a registry.
var pushOp = session.PushImageAsync(new PushImageOptions("registry.example.com/demo:latest", authToken));
pushOp.Progress = (op, progress) =>
Console.WriteLine($"push: {progress.Status} {progress.Id}");
await pushOp;
Deletes an image by name or ID.
session.DeleteImage("demo:old");
Applies a new repository/tag to an existing image.
session.TagImage(new TagImageOptions("alpine:latest", "registry.example.com/alpine", "v1"));
Creates a named session VHD volume.
var vhd = new VhdOptions("cache", 2UL * 1024 * 1024 * 1024, VhdType.Dynamic)
{
Owner = new VhdOwner { Uid = 1000, Gid = 1000 }
};
session.CreateVhdVolume(vhd);
Deletes a named session VHD volume.
session.DeleteVhdVolume("cache");
Authenticates to a registry and returns an identity token string.
string token = session.Authenticate(
new Uri("https://registry.example.com"),
"user1",
"p@ssw0rd");
Returns a snapshot of images known to the session.
foreach (var image in session.GetImages())
{
Console.WriteLine(image.Name);
}
Raised when the session termination event is signaled.
session.Terminated += reason =>
Console.WriteLine($"Session terminated: {reason}");
Raised when a process crash dump is reported.
session.ProcessCrashed += information =>
Console.WriteLine($"Process crashed: {information.ProcessName} ({information.Pid})");
Releases the underlying WinRT session object.
session.Dispose();