简体   繁体   English

由于网络不良,WCF异步方法失败

[英]WCF Async methods failure because of bad network

I am new into WCF stuff. 我是WCF新手。 I am curious to know two things about async methods: 我很想知道关于异步方法的两件事:

  1. What happens for some reason network goes down when I sending request to the service? 当我向服务发送请求时,由于某种原因网络出现故障会发生什么? Do I get any kind of exception or something k=like that? 我是否会遇到任何类型的异常或类似的东西?

  2. What happens if network goes down when service tries to respond to the request? 如果网络在服务尝试响应请求时出现故障,会发生什么? Does the result gets lost? 结果会丢失吗?

Thanks for the help! 谢谢您的帮助!

Sending a message through TCP (this includes HTTP) to WCF goes rougly through three phases: 通过TCP(包括HTTP)向WCF发送消息通过三个阶段变得粗糙:

  1. Client sends message to the WCF service 客户端将消息发送到WCF服务
  2. Only when the entire message has been successfully received, it gets deserialized and its contents are passed to the appropriate service operation. 只有在成功接收到整个消息后,才会对其进行反序列化,并将其内容传递给相应的服务操作。 The service operation runs to completion. 服务操作运行完成。
  3. The response (ie what you return or throw from the service operation) gets sent to the client. 响应(即您从服务操作返回或抛出的内容)将被发送到客户端。

If the connection is lost during 1., your service won't see the message and your client will throw an exception. 如果连接在1.期间丢失,则您的服务将看不到该消息,并且您的客户端将抛出异常。

If the connection is lost during 2., I think (but you can easily simulate that, for example using Thread.Sleep() ) the service operation continues execution. 如果连接在2.期间丢失, 我认为 (但您可以轻松地模拟,例如使用Thread.Sleep() )服务操作继续执行。

If the connection is lost during 3., the response will be lost, but the operation will have run. 如果连接在3.期间丢失,则响应将丢失,但操作将运行。

exception will be throw if the service did not respond. 如果服务没有响应,将抛出异常。 the program will receive the exception. 该程序将收到例外。 you can use try...catch block to catch it. 你可以使用try ... catch块来捕获它。 then write to a log file if you want to record it. 然后写入日志文件,如果你想记录它。

Based on my experience, mostly the 1st connection takes time to initialize. 根据我的经验,大多数第一个连接需要时间来初始化。 Sometime I get EndpointNotFoundException or TimeoutException if the service dint responds. 有时,如果服务dint响应,我会收到EndpointNotFoundExceptionTimeoutException

Therefore, i create a method to initialize the connection by passing message "connecting" to the service, if the connection established, service will return me a "connected" message. 因此,我创建了一种方法来通过将消息“连接”传递给服务来初始化连接,如果建立连接,服务将返回一个“连接”消息。 else if it return a exception , i will just let it retry. 否则,如果它返回异常,我将让它重试。

I set the number of retry = 5. So it will loop for 5 time if the connection still not establish. 我设置重试次数= 5.如果连接仍未建立,它将循环5次。 Once establish , it will continue for next process. 一旦建立,它将继续进行下一个过程。

we cant control the network connection and we cant expect and which point , which part it fail. 我们无法控制网络连接,我们无法期待和哪一点,它失败了哪一部分。 but we can control our program when the service failed. 但是我们可以在服务失败时控制我们的程序。 Depends on what you want to do if the service failed. 如果服务失败,取决于您想要做什么。 maybe clean the data, restart the process or stop the program immediately and log the error for investigation. 也许清理数据,重启过程或立即停止程序并记录错误进行调查。

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

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