1

I found this documentation that explains how to implement an asynchronous WCF service operation:

https://msdn.microsoft.com/en-us/library/ms731177(v=vs.110).aspx

This makes me wonder why (or when) would I want to make my service operations asynchronous, when the client can generate asynchronous versions of the operations himself. When a client generates asynchronous versions of the operations, is it the same as if the service already implemented asynchronous operations itself?

Also, in the sample code in the documentation, there is no OperationContractAttribute for the end methods. Why?

Drake
  • 2,679
  • 4
  • 45
  • 88
  • This also baffles me. Seems like everyone is going Aync Await berserk, but if the Server is already handling requests as they come in, we can't be getting much of an efficiency boost and we're getting a lot of headache. Seems like if you had a server with a ton of processing power, you could handle more load. Is that the only reason? – Mr. B Oct 06 '15 at 04:11
  • 1
    I think when the client generates asynchronous versions of the operations, it's still running synchronous on the server. Correct? – Drake Oct 06 '15 at 04:18
  • Yes per request though, right. So If I say DoThing1, DoThing2, DoThing3, wouldn't those each be asynchronous requests? Seems like the only advantage you're getting is the CPU can dance between the operations occurring inside each request regardless of requester using a bunch of CPUs. I would really like to know if I'm missing something here. Seems like Twitter or something with super high traffic would be the only one to see any benefit here. – Mr. B Oct 06 '15 at 04:23
  • 1
    Yes, those would be asynchronous requests on the client machine (who generated them that way), but they will still run synchronously on the server machine since they're not originally asynchronous functions. So yeah I think only super high traffic servers would need to implement asynchronous service operations. – Drake Oct 06 '15 at 04:37
  • 1
    Almost there with you. But they wouldn't run synchronously on the Server either, without using a lock of some kind. Each request lives in a separate thread right? So if I dispatch DoThing1, then DoThing2 from the same client asynchronously, there is a chance DoThing2 will finish first, because they run in separate threads. So its just that tiny amount of traffic where all threads are in use when the threading can say handle part of this one, then part of that one. – Mr. B Oct 06 '15 at 06:42
  • Okay, yes, I get it now - each request is already in a separate thread, so implementing asynchronous service operations is just crazy (for typical use at the least) – Drake Oct 06 '15 at 09:18
  • Yep. That is what I think too! – Mr. B Oct 06 '15 at 13:01
  • 1
    If you are processor bound async is pointless (not much is anymore)... if your are I/O bound async gives you much better performance. – Matthew Whited Oct 06 '15 at 15:01

1 Answers1

4

I think the answer to your question is right after the link you have provided. Here (https://msdn.microsoft.com/en-us/library/ms734701%28v=vs.110%29.aspx) you can see:

Use an asynchronous approach in a service operation implementation if the operation service implementation makes a blocking call, such as doing I/O work. When you are in an asynchronous operation implementation, try to call asynchronous operations and methods to extend the asynchronous call path as far as possible. For example, call a BeginOperationTwo() from within BeginOperationOne().

So when your service consumes some another service, for instance Redis, or downloads anything, that makes absolutely perfect sense to implement those operations also in async way and not to block working threads of your server. The number of working threads is limited and at some point your server would simply get stuck, because all of them are serving calls. In case of async implementation you can take advantage of usin I/O completion port and appropriate I/O threads from separate thread pool. I/O completion ports is very efficient mechanism in Windows, which helps to avoid resource wasting.

Community
  • 1
  • 1
Mimas
  • 525
  • 3
  • 7