我试图使用命名管道在同一台机器上的服务器和客户端进程之间进行通信.服务器向客户端发送消息,客户端对其执行某些操作并返回结果,服务器应该得到结果.
这是服务器的代码:
using System;
using System.IO;
using System.IO.Pipes;
class PipeServer
{
static void Main()
{
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut))
{
Console.WriteLine("NamedPipeServerStream object created.");
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
pipeServer.WaitForPipeDrain();
using (StreamReader sr = new StreamReader(pipeServer))
{
// Display the read …Run Code Online (Sandbox Code Playgroud) [编辑] 我现在已经用构造函数和调用客户端中的代码以及 OnDeserializing() 和 OnDeserialized() 方法编辑了 D。
我有一个 WCF 服务(通过 namedpipe)和一个客户端。我需要传递一个对象(最好是该对象的引用)作为我的 OperationContract 的参数。
[DataContract]
public class D
{
[DataMember]
public int Id;
[DataMember]
public string StrId;
//...
public D(int id, string strid)
{
Id = id;
StrId = strid;
//...
}
[OnDeserialized]
void OnDeserialized(StreamingContext strmCtx)
{
} // breakpoint here (1)
[OnDeserializing]
void OnDeserializing(StreamingContext strmCtx)
{
} // breakpoint here (2)
}
Run Code Online (Sandbox Code Playgroud)
这是服务合同:
[ServiceContract]
public interface ICalc
{
[OperationContract]
int Calculate(string date, int count);
// d is input of this …Run Code Online (Sandbox Code Playgroud)