小编jam*_*dev的帖子

在c#中使用命名管道的两个进程之间的双工操作

我试图使用命名管道在同一台机器上的服务器和客户端进程之间进行通信.服务器向客户端发送消息,客户端对其执行某些操作并返回结果,服务器应该得到结果.

这是服务器的代码:

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)

c# duplex named-pipes

6
推荐指数
1
解决办法
7157
查看次数

在 WCF 中使用 DataContract 传递对象

[编辑] 我现在已经用构造函数和调用客户端中的代码以及 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)

c# wcf struct object

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×2

duplex ×1

named-pipes ×1

object ×1

struct ×1

wcf ×1