简体   繁体   English

WCF上的TextReader和TextWriter

[英]TextReader and TextWriter over WCF

I'm trying to write this question for the fourth time, as I'm unfortunately not sure what I actually want. 我正在尝试第四次写这个问题,因为我不确定我到底想要什么。 Please excuse me being vague or a bit off. 请原谅我含糊不清或有点过时。

I have a command line .NET4.5 C# application. 我有一个命令行.NET4.5 C#应用程序。 I'll run it at the same time at two geographically distant locations. 我将在两个地理位置遥远的位置同时运行它。 I want to be able to turn one instance into host and the other into client. 我希望能够将一个实例变成主机,将另一个实例变成客户端。

In the heart of it, I want a TextReader implementation, that upon Read/ReadLine awaits for an input from a remote client. 从本质上讲,我想要一个TextReader实现,该实现在Read / ReadLine上等待来自远程客户端的输入。 I also want a TextWriter that upon Write/WriteLine will call a remote client and pass text to it. 我还想要一个TextWriter,它在Write / WriteLine上将调用一个远程客户端并将文本传递给它。

The TextWriter is of course fairly simple matter, as I basically do that: on write, call client.SendText(s); TextWriter当然是相当简单的事情,因为我基本上是这样做的:在写入时,调用client.SendText(s); to the other instance. 到另一个实例。

How do I go about implementing TextReader.ReadLine() in that scenario? 在这种情况下,如何实现TextReader.ReadLine() A naive aproach would be to have, in the WCF service, a method: 天真的方法是在WCF服务中使用以下方法:

class Service : IService{
    void SendText(string s){
        Console.WriteLine(s); // or whatever is the destination TextWriter
    }
    string ReadLine(){
        return Console.ReadLine(); // or whatever is the source TextReader
    }
}

but is keeping the WCF operation call open for minutes or even hours a good idea? 但是将WCF操作呼叫保持几分钟甚至几个小时打开是一个好主意吗?

Another way I can see is to have: 我可以看到的另一种方法是:

class Service : IService{
    void RegisterClient(string url){/* url points to an IService endpoint in client*/}
    void SendString(string s){...}
}

And then host it in both instances, call RegisterClient from client providing own endpoint url and then if host wants to return some data to client - just calls SendString, no problem. 然后在两个实例中托管它,从提供自己的端点URL的客户端调用RegisterClient,然后如果主机希望将一些数据返回给客户端-只需调用SendString,就没有问题。 But if hosts must wait for input from client, how to solve that reasonably? 但是,如果主机必须等待客户端的输入,那么如何合理地解决呢?

class WCFTextReader : TextReader{
   public override string ReadLine(){
       // what here? it should return whatever comes in the next SendString call from client...
   }
}

I could have a Queue<string> of incoming messages and then do a while(true) and either return first from Queue or Thread.Sleep for a short while and re-check, but every time I feel that I need while(true) and Thread.Sleep I'd rather come here, because I think I'm missing something.... 我可能会有传入消息的Queue<string> ,然后执行while(true) ,然后先从QueueThread.Sleep返回一小段时间,然后重新检查,但是每次我觉得我需要while(true)Thread.Sleep我想来这里,因为我想我缺少了一些东西。

The default WCF message pattern is request/response; 默认的WCF消息模式是请求/响应。 a client sends a request, and the server responds to it. 客户端发送一个请求,然后服务器响应。

There is, however, a duplex message pattern, in which the service interface contract identifies another interface as its callback interface contract; 但是,存在一种双工消息模式,其中服务接口协定将另一个接口标识为其回调接口协定。 the client must implement this callback interface contract, similar to how the service must implement the service interface contract. 客户端必须实现此回调接口协定,类似于服务必须实现服务接口协定的方式。

This message pattern should allow your server to arbitrarily send a message to the client without the client having to prompt it to do so. 此消息模式应允许您的服务器随意向客户端发送消息,而无需客户端提示。

http://msdn.microsoft.com/en-us/library/ms731064%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/ms731064%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/ms731184%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/ms731184%28v=vs.110%29.aspx

** Fair warning: in my experience, you potentially still have to deal with the issue of keeping the connection alive, in case you plan to have the server send a message back to the client after a significant period of inactivity. **合理警告:以我的经验,如果您打算让服务器在长时间不活动后将消息发送回客户端,则可能仍然需要处理保持连接活动的问题。

If keeping the connection open is acceptable to you, then you can implement some kind of keep-alive mechanism. 如果保持连接打开对您来说是可以接受的,那么您可以实现某种保持活动机制。

If keeping the connection open is not acceptable, then I think you'd need to look at a more complex approach that would involve the client periodically checking in with the server in order to give the server an opportunity to send the client any queued up messages that it has for the client. 如果不能保持开放连接,那么我认为您需要考虑一种更复杂的方法,该方法将涉及客户端定期向服务器进行检入,以便使服务器有机会向客户端发送任何排队的消息它为客户提供的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM