Async Interface

Only members of this group can post notes and use other group resources. Join now to get access to all group resources!
Log in or register to join this group

Async RIA Services Invoke  Viewed 23643 time(s), 2 post(s)., 8/22/2013 12:33:58 PM - by Daniel-Svensson

Daniel-Svensson
Daniel-Svensson 8/22/2013 12:33:58 PM



How Invoke operation work today:
01.// On server
02.[Invoke]
03.public T CalculateSum(T1 arg1, T2 arg2) { return arg1  + arg2; }
04.  
05.//  On client
06.public InvokeOperation<T> CalculateSum(T1 arg1, T2 arg2, ...callback etc...);
07.  
08.// Invoking it on the client
09.// APM
10._ctx.CalculateSum(  (res) =>
11.{
12.   if(res.Error != null) {
13.         res.MarkErrorAsHandled();
14.       // Handle error
15.       return ;
16.    }
17.    else
18.      this.Sum=res.Value;
19.});

How I suggest Task-Async Invoke operations should work (which correspond with how they work in WCF)
01.// On server
02.[Invoke]
03.public T CalculateSum(T1 arg1, T2 arg2) { return arg1  + arg2; }
04.  
05.//  On client
06.public Task<T> CalculateSum(T1 arg1, T2 arg2, CancellationToken cancellationToken);
07.  
08.// Invoking it on the client
09.// TPM
10.try
11.{
12.  this.Sum = await _ctx.CalculateSumAsync(20, 22);
13.}
14.catch(Exception ex)
15.{
16.// Handle error
17.}

Changes to DomainContext
Add InvokeAsync which is a Task-Async version of InvokeOperation to DomainContext

Task InvokeOperationAsync(string operationName, IDictionary<string, Object> parameters, bool hasSideEffects, CancellationToken cts);
Task<T> InvokeOperationAsync<T>(string operationName, IDictionary<string, Object> parameters, bool hasSideEffect, CancellationToken cts);

Open questions:
* Should we generate additional overloads without CancellationToken or should we make it an optional parameter with default value None ("default(CancellationToken)")
* Should failed invoke operations throw DomainOperationExceptions or should we try to get exception handling more similar to how it is handled normally by WCF
>> Personally I think we should use DomainOperationException as for all other load/submit and invoke.

This content has not been rated yet. 
338 Reputation 44 Total posts
1
Daniel-Svensson
Daniel-Svensson 1/11/2014 12:21:13 PM

I have pushed an implementaion of async Invoke operation to the async branch on my fork (http://openriaservices.codeplex.com/SourceControl/network/forks/danneesset/openriaservices?branch=async)

The big change against my previous post is that the async invoke methods returns Task<InvokeResult<T>> instead of Task<T>.  This makes it more similar to how Submit and Load works and makes it possible to attach additional info/"return values" (such as headers, session information or any other state which might be introduced in the future) to the result.

// This means that instead of Writing
int result = await ctx.CalculateSum(2,3);
 
// we should now write either
LoadResult<int> result = await ctx.CalculateSum(2,3);
// or
int result = (await ctx.CalculateSum(2,3)).Value;

Regarding how the the open questions are currently implemented:

* Since "InvokeOperationAsync" is mainly used by code generated by code generation I did not provide a version without CancellationToken, instead it was biven a default value.
</br> </br>

The same applies to the generated functions so that there are only 3 methods on domain context instead of 4, this made the code generation code easier to implement and it will run slightly faster than if we would generate 4 methods.
</br> </br>

* the "async" Invoke operations throws DomainOperationExceptions just as Load and Submit

This content has not been rated yet. 
338 Reputation 44 Total posts
2