我需要编写一个程序,既可以与其他 .NET 程序通信,又可以与 TCP 上的旧版 VFP 程序通信。我需要选择一种 VFP 程序员可以使用的相当简单的 TCP 消息格式。它可以像一系列小的 XML blob 一样简单,由......我不知道,空字符?任何。
我需要在 TcpListener/TcpClient 和 WCF 之间进行选择。我开始研究 WCF,但它的体系结构似乎不透明,而且内置的 Visual Studio 模板严重偏向于制作“Web 服务”,其行为类似于 RPC 机制,但需要应用程序外部的特殊“主机”或 Web 服务器。而微软的6阶段教程让WCF听起来相当麻烦(涉及代码生成器) 、命令行和 XML 垃圾只是为了远程减去或乘两个数字)。
我想要一个独立的应用程序(没有“主机”),我想要控制有线协议,并且我想了解它是如何工作的。 WCF 似乎并没有促进这些事情,所以我由于 TcpListener/TcpClient 的原因放弃了它。
然而,该程序将充当单个(VFP)服务器和许多(.NET)客户端之间的中介,并且将存在双向和跨不同连接的通信。使用 TcpListener
和 TcpClient
,处理连接和线程的工作变得有点混乱,我没有使用 IAsyncResult
的经验,而且我'我不仅仅是对我的代码质量没有信心。
所以想再次征求意见:我还应该考虑WCF吗?如果是,您能否指出以下问题的答案?
- Web 上哪里有关于 WCF 架构的详细解释?或者我需要一本书吗?
- WCF 中的双向通信是如何完成的,其中(单个 TCP 连接的)任何一方都可以随时发送消息?
- 我怎样才能超越所有的 Web 服务和 RPC 的繁琐,并控制有线协议?
- 在 WCF 中,如何干净地关闭应用程序,并行关闭所有连接,而无需使用 hacky Thread.Abort() 命令?
如果不是,我如何设置我的代码(使用 TcpListener/TcpClient/NetworkStream),以便我可以从 NetworkStream 读取消息,但也可以接受来自其他连接的请求,随时干净地关闭,并避免浪费 CPU 时间轮询不活动的队列和 NetworkStream?
I need to write a program that will communicate with other .NET programs ... but also a legacy VFP program over TCP. I need to choose a fairly simple TCP message format that the VFP programmer can use. It could be as simple as a sequence of small XML blobs delimited by... I dunno, a null character? Whatever.
I need to choose between TcpListener/TcpClient and WCF. I started researching WCF but its architecture seems opaque and built-in Visual Studio templates are heavily biased toward making "web services" that act like a sort of RPC mechanism, but require a special "host" or web server that is external to the application. And Microsoft's 6-stage tutorial makes WCF sound pretty cumbersome (involving code generators, command-line and XML crap just to remotely subtract or multiply two numbers).
I want a self-contained app (no "host"), I want control of the wire protocol, and I want to understand how it works. WCF doesn't seem to facilitate these things, so I abandoned it in factor of TcpListener/TcpClient.
However, the program is to serve as an intermediary between a single (VFP) server and many (.NET) clients, and there will be communication in both directions and across different connections. Using TcpListener
and TcpClient
, the work of juggling the connections and threads is getting a bit messy, I have no experience with IAsyncResult
, and I'm not just not confident in my code quality.
So I would like to solicit opinions again: should I still consider WCF instead? If yes, can you point me toward answers to the following questions?
- Where in the web is a good explanation of WCF's architecture? Or do I need a book?
- How is bi-directional communication done in WCF, where either side (of a single TCP connection) can send a message at any time?
- How can I get past all the web-services and RPC mumbo-jumbo, and control the wire protocol?
- In WCF, how do I shut down the app cleanly, closing all connections in parallel without hacky Thread.Abort() commands?
If no, how can I set up my code (that uses TcpListener/TcpClient/NetworkStream) so that I can read a message from a NetworkStream, but also accept requests from other connections, shut down cleanly at any time, and avoid wasting CPU time to poll queues and NetworkStreams that are inactive?
发布评论
评论(2)
简短的回答:使用 WCF。虽然有大量的工具和代码生成以及其他花哨的东西,但没有什么可以阻止您在代码中设置所有内容(您可以在代码中定义合同、设置端点等)。
对于您的具体问题:
最后,我强烈建议您不要使用 NetTcpBinding; VFP 会很难消耗协议的时间。但是,如果您使用基于 HTTP 的协议,VFP 总是可以轻松使用一些工具来进行调用并使用内容(假设您坚持使用 XML)。
The short answer: go with WCF. While there's a good amount of tooling and code-generation and other bells and whistles around it, there's nothing that is preventing you from setting up everything in code (you can define your contracts, set the endpoints up, etc. all in code).
For your specific questions:
In the end, I'd strongly recommend that you not use the NetTcpBinding; VFP will have a difficult time consuming the protocol. However, if you use an HTTP-based protocol, there are always tools that VFP can easily use to make the call and consume the contents (assuming you stick with XML).
只是为了补充一下使用 DCOM 的常见问题,VFP 可以利用 DCOM,但需要使用 CreateObjectEx() 来完成...唯一的大区别是您需要知道在任何服务器上连接到的类实例的 GUID它正在连接到,以及它要连接到的机器名称。
然后,远程对象通过公开的函数完成其工作,但从网络上的其他计算机调用它的 VFP 会将其视为在本地执行该函数并获取任何返回值。
早在 10 年前我就已经为一家保险公司使用 VFP 完成了 DCOM...
Just to tack on about a common on using DCOM, VFP can utilize DCOM, but needs to be done with CreateObjectEx()... the only big difference is you need to know the GUID of the class instance you are connecting to on whatever server it is connecting to, AND the machine name its going to connect to.
Then the remote object does its work via exposed functions, but VFP calling it from some other machine on the network treats it as if the function was being performed locally and gets whatever the return values are.
I've done DCOM with VFP even as far back as 10 yrs ago for an insurance company...