响应式WCF客户端的双工回调或客户端线程

时间:2022-08-26 19:40:24

I have a certain service where specific functions will take longer to call than others, sometimes they might take seconds to return. In order to prevent the client's UI being blocked when this happens what is the preferred solution:

我有一个特定的服务,其中特定的功能需要比其他功能更长的时间,有时它们可​​能需要几秒钟才能返回。为了防止在发生这种情况时阻止客户端的UI,首选解决方案是什么:

  1. Use a Duplex channel and simply use the callbacks to update the UI when data is received.
  2. 使用双工通道,只需使用回调即可在收到数据时更新UI。

  3. Use a separate thread to call the service, and simply use request-reply operations, and then update the ui thread when data is returned.
  4. 使用单独的线程来调用服务,只需使用请求 - 回复操作,然后在返回数据时更新ui线程。

Which solution is better, particularly when interoperability is favored but not strictly necessary, and in your opinion, which one is faster (and cleaner) to implement and maintain?

哪种解决方案更好,特别是在互操作性受到青睐但不是绝对必要时,并且您认为哪种解决方案更快(更清洁)的实施和维护?

2 个解决方案

#1


5  

If you implement callback contracts then you are removing the need for the client to implement multithreading code. This might not be a significant advantage when working with .Net clients (as VS will auto generate the asynch proxy code for you), though could prove beneficial when working with clients of other platforms/languages.

如果实现回调契约,则无需客户端实现多线程代码。在使用.Net客户端时,这可能不是一个显着的优势(因为VS会自动为您生成异步代理代码),但在与其他平台/语言的客户端合作时可能会有所帮助。

Which one is cleaner? Well, that depends whether you are a client or server developer. If, as I suspect in your case, you are both, and you can just use .Net for client and server, then I'd probably be tempted to avoid callbacks for now. If you'd have implied that the service calls where taking 45 seconds then I'd say call back contracts, it really is subjective, but if I were to stick my neck out then I'd say that if responses take longer than 5 seconds then it is time to move to callbacks.

哪一个更干净?那么,这取决于您是客户端还是服务器开发人员。如果我怀疑你是两个,并且你可以只使用.Net作为客户端和服务器,那么我现在可能会想要避免回调。如果你暗示服务呼叫需要45秒,那么我会说回叫合同,这确实是主观的,但如果我要坚持下去,那么我会说如果响应时间超过5秒那么是时候转向回调了。

#2


0  

You should implement a CallBackcontract.

您应该实现CallBackcontract。

Here is an example.

这是一个例子。

#1


5  

If you implement callback contracts then you are removing the need for the client to implement multithreading code. This might not be a significant advantage when working with .Net clients (as VS will auto generate the asynch proxy code for you), though could prove beneficial when working with clients of other platforms/languages.

如果实现回调契约,则无需客户端实现多线程代码。在使用.Net客户端时,这可能不是一个显着的优势(因为VS会自动为您生成异步代理代码),但在与其他平台/语言的客户端合作时可能会有所帮助。

Which one is cleaner? Well, that depends whether you are a client or server developer. If, as I suspect in your case, you are both, and you can just use .Net for client and server, then I'd probably be tempted to avoid callbacks for now. If you'd have implied that the service calls where taking 45 seconds then I'd say call back contracts, it really is subjective, but if I were to stick my neck out then I'd say that if responses take longer than 5 seconds then it is time to move to callbacks.

哪一个更干净?那么,这取决于您是客户端还是服务器开发人员。如果我怀疑你是两个,并且你可以只使用.Net作为客户端和服务器,那么我现在可能会想要避免回调。如果你暗示服务呼叫需要45秒,那么我会说回叫合同,这确实是主观的,但如果我要坚持下去,那么我会说如果响应时间超过5秒那么是时候转向回调了。

#2


0  

You should implement a CallBackcontract.

您应该实现CallBackcontract。

Here is an example.

这是一个例子。