我应该使用 WCF 来实现简单的文本传输协议吗?

发布于 2024-10-25 16:59:12 字数 1163 浏览 2 评论 0 原文

我需要编写一个程序,既可以与其他 .NET 程序通信,又可以与 TCP 上的旧版 VFP 程序通信。我需要选择一种 VFP 程序员可以使用的相当简单的 TCP 消息格式。它可以像一系列小的 XML blob 一样简单,由......我不知道,空字符?任何。

我需要在 TcpListener/TcpClient 和 WCF 之间进行选择。我开始研究 WCF,但它的体系结构似乎不透明,而且内置的 Visual Studio 模板严重偏向于制作“Web 服务”,其行为类似于 RPC 机制,但需要应用程序外部的特殊“主机”或 Web 服务器。而微软的6阶段教程让WCF听起来相当麻烦(涉及代码生成器) 、命令行和 XML 垃圾只是为了远程减去或乘两个数字)。

我想要一个独立的应用程序(没有“主机”),我想要控制有线协议,并且我想了解它是如何工作的。 WCF 似乎并没有促进这些事情,所以我由于 TcpListener/TcpClient 的原因放弃了它。

然而,该程序将充当单个(VFP)服务器和许多(.NET)客户端之间的中介,并且将存在双向和跨不同连接的通信。使用 TcpListenerTcpClient,处理连接和线程的工作变得有点混乱,我没有使用 IAsyncResult 的经验,而且我'我不仅仅是对我的代码质量没有信心。

所以想再次征求意见:我还应该考虑WCF吗?如果是,您能否指出以下问题的答案?

  1. Web 上哪里有关于 WCF 架构的详细解释?或者我需要一本书吗?
  2. WCF 中的双向通信是如何完成的,其中(单个 TCP 连接的)任何一方都可以随时发送消息?
  3. 我怎样才能超越所有的 Web 服务和 RPC 的繁琐,并控制有线协议?
  4. 在 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?

  1. Where in the web is a good explanation of WCF's architecture? Or do I need a book?
  2. How is bi-directional communication done in WCF, where either side (of a single TCP connection) can send a message at any time?
  3. How can I get past all the web-services and RPC mumbo-jumbo, and control the wire protocol?
  4. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-11-01 16:59:12

简短的回答:使用 WCF。虽然有大量的工具和代码生成以及其他花哨的东西,但没有什么可以阻止您在代码中设置所有内容(您可以在代码中定义合同、设置端点等)。

对于您的具体问题:

  1. WCF 架构 - 这是非常基本的,而且它应该可以让您相对快速地启动并运行。
  2. 您正在寻找的是双面服务。 NetTcpBinding 允许开箱即用的双工服务(尽管您可以使用 HTTP 来实现,但您需要特定的绑定)。
  3. 如果您想控制有线格式,您需要创建一个自定义编码器。但是,我必须强烈建议反对它。您想创建一个带有空字符的 XML 文件来描述单独的消息吗?没有必要这样做,XML 的本质是您可以创建子元素来执行适当的分组;可以嵌套的元素数量没有限制。实在没有这个必要。
  4. 只需通过调用 ServiceHost “http://msdn.microsoft.com/en-us/library/ms405496.aspx”rel="nofollow">关闭,这将允许所有未完成的请求完成,然后正常关闭。如果您确实想毫无顾虑地拆除,请调用 中止

最后,我强烈建议您不要使用 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:

  1. WCF Architecture - This is pretty basic, and it should get you up and running relatively quickly.
  2. What you are looking for is duplex services. The NetTcpBinding allows for duplex services out-of-the-box (although you can do it with HTTP, you need a specific binding).
  3. If you want to control the wire format, you will want to create a custom encoder. However, I have to strongly recommend against it. You want to create an XML file with null character to delineate separate messages? There's no need for that, the nature of XML is that you can create child elements to perform the appropriate grouping; there's no limit to how many elements you can nest. There's really no need for this.
  4. Simply shutting down the ServiceHost by calling Close, this will allow all outstanding requests to complete, and then shut down gracefully. If you really want to tear down without concern, then call Abort.

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).

伴随着你 2024-11-01 16:59:12

只是为了补充一下使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文