简体   繁体   English

WCF与Web服务与套接字:选择哪个?

[英]WCF vs. Web service vs. Sockets: which to choose?

I have two related questions about Web services: 关于Web服务,我有两个相关的问题:

(1) I'm currently writing a set of applications, and it occurred to me that maybe I'm not using the right tool for the job. (1)我目前正在编写一组应用程序,但是我发现也许我没有使用正确的工具来完成这项工作。 Here is the spec: 规格如下:

  • There are many Windows servers behind different VPNs and firewalls. 在不同的VPN和防火墙后面有许多Windows服务器。
  • Each of the servers has a Windows service running, that reports various information about it to a centralized server, via a Web service, both of which I've written, and have access to. 每个服务器都有一个运行的Windows服务,该服务通过Web服务将其有关的各种信息通过Web服务报告给集中式服务器,我已编写并可以访问这两个信息。

So I'm both the producer and the consumer, and I'm staying on the same platform (.NET). 因此,我既是生产者又是消费者,并且我停留在同一平台(.NET)上。 Maybe a web service isn't the way to go? 也许Web服务不是要走的路? I'm using one purely because it's easy to write and deploy, and I'm the most comfortable with them. 我之所以纯粹使用它,是因为它易于编写和部署,并且我对它们最满意。 Should I really be using WCF for this? 我真的应该为此使用WCF吗?

(2) In the web service, I'm creating a State object to represent the state of the server, and sending it as a parameter. (2)在Web服务中,我正在创建一个State对象来表示服务器的状态,并将其作为参数发送。 However, adding a service reference creates a proxy of the State class. 但是,添加服务引用会创建State类的代理。 It seems gacky to copy the properties of the State object to the proxy, and then send the proxy. 将State对象的属性复制到代理,然后发送代理似乎很费劲。 Should I just replace the proxy class with the real class in the auto-generated code (ie, include a reference to the State class instead)? 我是否应该在自动生成的代码中将代理类替换为真实类(即,包括对State类的引用)?

By "web services" I assume you mean an ASMX? 通过“ Web服务”,我假设您是指ASMX? I would go with WCF is possible, simply because you lose nothing but gain lots of flexibility. 我认为WCF是可行的,仅仅是因为您没有损失,却获得了很大的灵活性。 You could, for example, switch from XML-over-HTTP to Binary-over-TCP through a simple config change. 例如,您可以通过简单的配置更改从XML-over-HTTP切换为Binary-over-TCP。

I would suggest to use WCF and use the Net.Tcp binding. 我建议使用WCF并使用Net.Tcp绑定。 It should be efficient enough for 300 clients. 对于300个客户来说,它应该足够有效。 For the proxy class issue use the /reference option for the svcutil tool when you generate the proxy. 对于代理类问题,在生成代理时,请对svcutil工具使用/ reference选项。 This will allow you to share classes between server and client. 这将允许您在服务器和客户端之间共享类。 I would not use this option if interoperability was a concern but since you stated that you develop both the clietn and the service and all in .Net it is a valid use in your case. 如果您担心互操作性,我将不使用此选项,但是由于您声明同时开发了clietn和服务,因此在.Net中这都是一种有效的用法。

Your distinction between "Web Services" and WCF is a false distinction. 您对“ Web服务”和WCF的区分是错误的。

ASMX Web Services is the original .NET SOAP Web Service technology, introduced in .NET 1.0. ASMX Web服务是.NET 1.0中引入的原始.NET SOAP Web服务技术。 It has been replaced by WCF, which can everything that ASMX can do, plus a whole lot more (including support for the WS-* standards). 它已被WCF取代,WCF可以完成ASMX的所有工作,以及更多功能(包括对WS- *标准的支持)。

Microsoft now considers ASMX Web Services, and the XML Serializer they're based on, to be "legacy technology". 微软现在认为ASMX Web服务以及它们所基于的XML序列化器是“传统技术”。 See " Microsoft says: ASMX Web Services are a “Legacy Technology” ". 请参阅“ Microsoft说:ASMX Web服务是“传统技术” ”。

With WCF, since you have control of both sides of the operation, and can share the .dll in which the service contract is defined, you can and perhaps should be using ChannelFactory<IYourServiceContractHere> instead of auto-generating those ugly proxy classes with service references. 使用WCF,由于您可以控制操作的两面,并且可以共享在其中定义服务协定的.dll,因此您可以并且应该使用ChannelFactory<IYourServiceContractHere>而不是通过服务自动生成那些丑陋的代理类。引用。

Here's the first hit I found on this topic: http://blogs.msdn.com/juveriak/archive/2008/02/03/using-channels-vs-proxies-in-wcf.aspx 这是我在此主题上发现的第一篇文章: http : //blogs.msdn.com/juveriak/archive/2008/02/03/using-channels-vs-proxies-in-wcf.aspx

如果它是平台无关的,我当然会推荐WCF。

I've done exactly what your describing to great effect across more than 300 locations. 我已经完全按照您的描述做了,对300多个地点产生了巨大影响。 I don't think you made the wrong call. 我认为您打错了电话。

Another thing you could consider that would work well is using MSMQ. 您可能会考虑的另一件事是使用MSMQ。 In this case, however, you'll either need to write event triggers (COM) or an event queue processing service. 但是,在这种情况下,您将需要编写事件触发器(COM)或事件队列处理服务。

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

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