WCF:当服务器不存在时,使用流绑定的通道不会出现错误
我正在编写一个小型框架来帮助使用 WCF 进行客户端-服务器连接,但我遇到了意外的行为。
如果客户端打开到不可用服务的通道,然后对该服务进行方法调用,则结果行为会有所不同,具体取决于通道的绑定是否将 TransferMode 设置为“Streaming”。
如果绑定未设置为流并且服务不可用,WCF 会立即引发 ICommunicationObject.Faulted 事件。另一方面,如果是,WCF 将引发 ICommunicationObject.Opened 事件。
谁能解释这种行为差异的原因?看起来很奇怪。
I'm writing a mini-framework to aid with client-server connections using WCF, and I've encountered unexpected behavior.
If a client opens a channel to a service that is not available, and then makes a method call vis-a-vis that service, the resulting behavior differs depending on whether or not the binding for the channel has transferMode set to "Streaming".
If the binding is not set to streaming and the service is unavailable, WCF immediately raises the ICommunicationObject.Faulted event. On the other hand, if it is, WCF raises the ICommunicationObject.Opened event.
Can anyone explain the reason for this difference in behavior? Seems pretty odd.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我已经研究了几个小时,并相信我有一个答案(如果没有人有更好的答案,我最终会将其标记为答案)。
根据 msdn 上的这篇文章,“所有流式调用是通过单个通道(数据报通道)进行的,即使所使用的绑定配置为使用会话,该通道也不支持会话。” (请注意粗体文本。)虽然我不太清楚通过 TCP/IP 协议使用数据报通道意味着什么,但我将从表面上理解它。
因此,另一篇 msdn 文章指出“数据报通道是其中所有消息不相关的通道,对于数据报通道,如果输入或输出操作失败,则下一个操作通常不受影响,并且可以重用相同的通道,因此,数据报通道通常不受影响。过错。”
因此,显然,流媒体频道根本不会出错。虽然尝试通过通道发送消息会引发套接字异常(因为如果服务不可用,显然套接字无法打开),但就 WCF 而言,这并不意味着通道的状态出现故障。即,您仍然可以通过同一通道发送另一条消息,并且如果服务可用,则消息可能会成功传输。
但对我来说仍然很奇怪,当套接字连接从未建立时,通道的状态被认为是打开的。因此,如果有人对此有更深入的理解,我仍然很高兴听到解释。
Well, I've been researching this for several hours and believe that I have an answer (I'll mark this as the answer eventually if no one has a better answer).
According to this article on msdn, "All streaming calls are made through a single channel (the datagram channel) that does not support sessions even if the binding being used is configured to use sessions." (Note the bolded text.) Although it's not so clear to me what it means to be using a datagram channel over a TCP/IP protocol, I'll take it at face-value.
So, another msdn article, states that "A datagram channel is a channel in which all messages are uncorrelated. With a datagram channel, if an input or output operation fails, the next operation is typically unaffected, and the same channel can be reused. Because of this, datagram channels typically do not fault."
So, apparently, streaming channels simply do not fault. Although trying to send a message over the channel throws a socket exception (because if the service is unavailable obviously the socket cannot be opened), as far as WCF is concerned, this does not mean that the state of the channel is faulted. I.e., you can still send another message over the same channel and, if the service becomes available, the message might transfer successfully.
It still seems odd to me though, that a channel's state is considered to be open when the socket connection was never made. So I'd still be glad to hear an explanation if someone has a deeper understanding of this.