我正在尝试将C#.Net项目转换为Mono。 NamedPipeServerStream& NamedPipeClientStream类在Mono中不支持并抛出Unhandle异常。 你能帮我一个人吗?
代码:
class Program
{
static string _PipeName = "testPipe";
static void Main(string[] args)
{
ListenForPipeServer();
}
static void ListenForPipeServer()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(
_PipeName,
PipeDirection.InOut,
2,
PipeTransmissionMode.Message,
PipeOptions.None
))
{
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
sw.WriteLine("Testing");
sw.Flush();
pipeServer.WaitForPipeDrain();
Console.WriteLine(sr.ReadLine());
}
catch (Exception)
{
throw;
}
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) pipeServer.Disconnect();
}
} while (true);
}
}
}
未处理的例外:
System.IO.IOException:创建命名管道时出错:错误代码-1
在System.IO.Pipes.UnixNamedPipe.EnsureTargetFile(System.String name) [0x0001f] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /班/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:131 在System.IO.Pipes.UnixNamedPipeServer..ctor (System.IO.Pipes.NamedPipeServerStream所有者,System.String pipeName, Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeAccessRights权限,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,HandleInheritability 继承性)[0x00012] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /班/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:265 在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability继承性,PipeAccessRights additionalAccessRights)[0x0004b] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /类/ System.Core程序/ System.IO.Pipes / NamedPipeServerStream.cs:100 在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability inheritability)[0x00000] in :System.IO.Pipes.NamedPipeServerStream..ctor中的0 (System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode, PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize, System.IO.Pipes.NipePipeServerStream..ctor中的System.IO.Pipes.PipeSecurity pipeSecurity)[0x00000] in:0 (System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode, PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize) [0x00000] in:0 at System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName, PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions options)[0x00000] in:0 at(wrapper remoting-invoke-with-check) System.IO.Pipes.NamedPipeServerStream:.ctor (字符串,System.IO.Pipes.PipeDirection,INT,System.IO.Pipes.PipeTransmissionMode,System.IO.Pipes.PipeOptions) 在ConsoleTestPipeApp1.Program.ListenForPipeServer()[0x00000]中 /Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:21 在ConsoleTestPipeApp1.Program.Main(System.String [] args)[0x00000] 在 /Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:16 [错误]致命的未处理的异常:System.IO.IOException:错误 创建命名管道:错误代码-1 at System.IO.Pipes.UnixNamedPipe.EnsureTargetFile(System.String name) [0x0001f] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /班/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:131 在System.IO.Pipes.UnixNamedPipeServer..ctor (System.IO.Pipes.NamedPipeServerStream所有者,System.String pipeName, Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode,PipeAccessRights权限,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,HandleInheritability 继承性)[0x00012] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /班/ System.Core程序/ System.IO.Pipes / PipeUnix.cs:265 在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability继承性,PipeAccessRights additionalAccessRights)[0x0004b] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10 .11.git / MCS /类/ System.Core程序/ System.IO.Pipes / NamedPipeServerStream.cs:100 在System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize,System.IO.Pipes.PipeSecurity pipeSecurity,HandleInheritability inheritability)[0x00000] in :System.IO.Pipes.NamedPipeServerStream..ctor中的0 (System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode, PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize, System.IO.Pipes.NipePipeServerStream..ctor中的System.IO.Pipes.PipeSecurity pipeSecurity)[0x00000] in:0 (System.String pipeName,PipeDirection方向,Int32 maxNumberOfServerInstances,PipeTransmissionMode transmissionMode, PipeOptions选项,Int32 inBufferSize,Int32 outBufferSize) [0x00000] in:0 at System.IO.Pipes.NamedPipeServerStream..ctor(System.String pipeName, PipeDirection方向,Int32 maxNumberOfServerInstances, PipeTransmissionMode transmissionMode,PipeOptions options)[0x00000] in:0 at(wrapper remoting-invoke-with-check) System.IO.Pipes.NamedPipeServerStream:.ctor (字符串,System.IO.Pipes.PipeDirection,INT,System.IO.Pipes.PipeTransmissionMode,System.IO.Pipes.PipeOptions) 在ConsoleTestPipeApp1.Program.ListenForPipeServer()[0x00000]中 /Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:21 在ConsoleTestPipeApp1.Program.Main(System.String [] args)[0x00000] 在 /Users/Vadivelu/Project/Service_Test/ConsoleTestPipeApp1/ConsoleTestPipeApp1/Program.cs:16
答案 0 :(得分:0)
唯一的问题是,当C#代码访问管道时,您正在使用的名为“testPipe”的管道不存在。如果您首先运行命令mkfifo testPipe
(在unix上),然后运行以下C#代码,它应该可以工作。