I am running an asynchronous service. After evaluating Microsoft's example , I wonder if their approach is really asynchronous. I am sure that it is, but some of the samples that I saw online and the AsyncCallback parameter make me wonder.
In accordance with this example, we need to implement a couple of Begin and End methods:
public IAsyncResult BeginGetAcmeAnvil(AsyncCallback callback, object state) { // Starts synchronous task var acmeAsyncResult = new AcmeAsyncResult<Anvil> { Data = new Anvil() }; return acmeAsyncResult; } public Anvil EndGetAcmeAnvil(IAsyncResult result) { var acmeAsyncResult = result as AcmeAsyncResult<Anvil>; return acmeAsyncResult != null ? acmeAsyncResult.Data : new Anvil(); }
Pretty simple, but why do we have an AsyncCallback parameter? Shouldn't we call a callback , which in turn will call the End method?
Here is what I mean:
public delegate void AsyncMethodCaller(AcmeAsyncResult<Anvil> acmeAsyncResult, AsyncCallback callback); public IAsyncResult BeginGetAcmeAnvil(AsyncCallback callback, object state) { var acmeAsyncResult = new AcmeAsyncResult<Anvil>(); var asyncMethodCaller = new AsyncMethodCaller(GetAnvilAsync);
I did some load testing using loadUI , but there were no obvious changes in performance.
c # asynchronous wcf
André hauptfleisch
source share