C# 通过命名管道访问Windows上的Docker API
根据Docker for Windows,“客户端可以通过命名管道连接到Docker引擎:npipe:////./pipe/docker_engine" 我一直试图通过命名管道连接到API,但没有成功:C# 通过命名管道访问Windows上的Docker API,c#,windows,docker,named-pipes,C#,Windows,Docker,Named Pipes,根据Docker for Windows,“客户端可以通过命名管道连接到Docker引擎:npipe:////./pipe/docker_engine" 我一直试图通过命名管道连接到API,但没有成功: public class DockerNamedPipeTest { private const string PIPE_PATH = "docker_engine"; public void Test() { using (NamedPipeClien
public class DockerNamedPipeTest
{
private const string PIPE_PATH = "docker_engine";
public void Test()
{
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream(
".",
PIPE_PATH,
PipeDirection.InOut,
PipeOptions.WriteThrough,
TokenImpersonationLevel.Impersonation))
{
pipeClient.Connect(30);
Send(pipeClient);
Receive(pipeClient);
}
}
public void Send(NamedPipeClientStream pipeClient)
{
if (pipeClient.IsConnected)
{
byte[] buffer = Encoding.UTF8.GetBytes("GET /containers/json");
pipeClient.Write(buffer, 0, buffer.Length);
pipeClient.WaitForPipeDrain();
pipeClient.Flush();
}
}
public void Receive(NamedPipeClientStream pipeClient)
{
string result = string.Empty;
if (pipeClient.IsConnected && pipeClient.CanRead)
{
do
{
byte b = (byte)pipeClient.ReadByte(); // <-- Hangs here
result += Convert.ToChar(b).ToString();
}
while (!pipeClient.IsMessageComplete);
}
Console.WriteLine(result);
}
}
公共类DockerNamedPipeTest
{
私有常量字符串管道路径=“docker\u引擎”;
公开无效测试()
{
使用(NamedPipeClientStream pipeClient=
新命名的PipeClientStream(
".",
烟斗径,
PipeDirection.InOut,
PipeOptions.WriteThrough,
TokenImpersonationLevel.Impersonation)
{
pipeClient.Connect(30);
发送(客户端);
接收(客户端);
}
}
public void Send(NamedPipeClientStream pipeClient)
{
if(pipeClient.IsConnected)
{
byte[]buffer=Encoding.UTF8.GetBytes(“GET/containers/json”);
写入(buffer,0,buffer.Length);
pipeClient.WaitForPipeDrain();
pipeClient.Flush();
}
}
public void Receive(NamedPipeClientStream pipeClient)
{
字符串结果=string.Empty;
if(pipeClient.IsConnected&&pipeClient.CanRead)
{
做
{
byte b=(byte)pipeClient.ReadByte();//Microsoft支持命名管道,您看过了吗
下面是一个例子:
using Docker.DotNet;
DockerClient client = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine"))
.CreateClient();
微软的命名管道支持,你看过吗
下面是一个例子:
using Docker.DotNet;
DockerClient client = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine"))
.CreateClient();
事实证明,答案可以在Docker.DotNet代码的源代码中找到,特别是在名为CloseWrite()
的方法中的DockerPipeStream.cs类中:
()
我将此方法应用于我的代码,代码不再挂起
我现在收到了一个400错误请求,但至少现在我知道了与docker守护进程的通信为何挂起。如果docker for Windows常见问题解答提到了这一细微差别,那就太好了。事实证明,答案可以在docker.DotNet代码的源代码中找到,特别是在DockerPipeStream.cs clas中在名为CloseWrite()
的方法中:
()
我将此方法应用于我的代码,代码不再挂起
我现在收到了一个400错误请求,但至少现在我知道了与docker守护进程的通信为何会中断。如果docker for Windows常见问题解答提到了这一细微差别,那就太好了。我已经很久没有接触命名管道了,但在浏览了一些文档之后,比如,您是否尝试过指定pipe路径,如\\./pipe/docker\u引擎
,或使用链接的文档中给出的完整URI(npipe:////./pipe/docker_engine
)?如果我使用这些,它会引发超时异常。仅使用docker\u引擎并连接。此外,运行sysinternals工具pipelist会显示正确的管道名称为docker\u引擎。我已经很久没有接触命名管道了,但是在浏览了一些文档之后,您是否尝试过像\./pipe/docker\u引擎那样指定管道路径
,或使用链接的文档中提供的完整URI(npipe:////./pipe/docker_engine
)?如果我使用这些,它会引发超时异常。仅使用docker\u引擎并连接。此外,运行sysinternals工具pipelist会显示正确的管道名称为docker\u引擎。