Skip to content

Commit ef821d1

Browse files
shawnallen85rabbah
authored andcommitted
Async task (#25)
Support for async/await.
1 parent c73dae1 commit ef821d1

9 files changed

Lines changed: 63 additions & 11 deletions

File tree

core/dotnet2.2/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@
2323
## 1.13
2424
Changes:
2525
- Initial release
26+
27+
## Release TBD
28+
Changes:
29+
- Support for async methods. Example:
30+
31+
```csharp
32+
public async Task<JObject> MainAsync(JObject args)
33+
{
34+
await Task.Delay(10); // Just do a delay to have an async/await process occur.
35+
return (args);
36+
}
37+
```

core/dotnet2.2/proxy/Apache.OpenWhisk.Runtime.Common/Init.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Init
3333
private Type Type { get; set; }
3434
private MethodInfo Method { get; set; }
3535
private ConstructorInfo Constructor { get; set; }
36+
private bool AwaitableMethod { get; set; }
3637

3738
public Init()
3839
{
@@ -51,7 +52,7 @@ public async Task<Run> HandleRequest(HttpContext httpContext)
5152
{
5253
await httpContext.Response.WriteError("Cannot initialize the action more than once.");
5354
Console.Error.WriteLine("Cannot initialize the action more than once.");
54-
return (new Run(Type, Method, Constructor));
55+
return (new Run(Type, Method, Constructor, AwaitableMethod));
5556
}
5657

5758
string body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync();
@@ -156,7 +157,9 @@ await httpContext.Response.WriteError(ex.Message
156157

157158
await httpContext.Response.WriteResponse(200, "OK");
158159

159-
return (new Run(Type, Method, Constructor));
160+
AwaitableMethod = (Method.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null);
161+
162+
return (new Run(Type, Method, Constructor, AwaitableMethod));
160163
}
161164
catch (Exception ex)
162165
{

core/dotnet2.2/proxy/Apache.OpenWhisk.Runtime.Common/Run.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public class Run
2929
private readonly Type _type;
3030
private readonly MethodInfo _method;
3131
private readonly ConstructorInfo _constructor;
32+
private readonly bool _awaitableMethod;
3233

33-
public Run(Type type, MethodInfo method, ConstructorInfo constructor)
34+
public Run(Type type, MethodInfo method, ConstructorInfo constructor, bool awaitableMethod)
3435
{
3536
_type = type;
3637
_method = method;
3738
_constructor = constructor;
39+
_awaitableMethod = awaitableMethod;
3840
}
3941

4042
public async Task HandleRequest(HttpContext httpContext)
@@ -79,7 +81,14 @@ await Console.Error.WriteLineAsync(
7981

8082
try
8183
{
82-
JObject output = (JObject) _method.Invoke(owObject, new object[] {valObject});
84+
JObject output;
85+
86+
if(_awaitableMethod) {
87+
output = (JObject) await (dynamic) _method.Invoke(owObject, new object[] {valObject});
88+
}
89+
else {
90+
output = (JObject) _method.Invoke(owObject, new object[] {valObject});
91+
}
8392

8493
if (output == null)
8594
{

core/dotnet3.0/CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
# .NET Core 3.0 OpenWhisk Runtime Container
2121

2222

23-
## 1.14 (next Apache release)
23+
## Release TBD
2424
Changes:
2525
- Initial release
26+
- Support for async methods. Example:
27+
28+
```csharp
29+
public async Task<JObject> MainAsync(JObject args)
30+
{
31+
await Task.Delay(10); // Just do a delay to have an async/await process occur.
32+
return (args);
33+
}
34+
```

core/dotnet3.0/proxy/Apache.OpenWhisk.Runtime.Common/Init.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Init
3333
private Type Type { get; set; }
3434
private MethodInfo Method { get; set; }
3535
private ConstructorInfo Constructor { get; set; }
36+
private bool AwaitableMethod { get; set; }
3637

3738
public Init()
3839
{
@@ -51,7 +52,7 @@ public async Task<Run> HandleRequest(HttpContext httpContext)
5152
{
5253
await httpContext.Response.WriteError("Cannot initialize the action more than once.");
5354
Console.Error.WriteLine("Cannot initialize the action more than once.");
54-
return (new Run(Type, Method, Constructor));
55+
return (new Run(Type, Method, Constructor, AwaitableMethod));
5556
}
5657

5758
string body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync();
@@ -156,7 +157,9 @@ await httpContext.Response.WriteError(ex.Message
156157

157158
await httpContext.Response.WriteResponse(200, "OK");
158159

159-
return (new Run(Type, Method, Constructor));
160+
AwaitableMethod = (Method.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null);
161+
162+
return (new Run(Type, Method, Constructor, AwaitableMethod));
160163
}
161164
catch (Exception ex)
162165
{

core/dotnet3.0/proxy/Apache.OpenWhisk.Runtime.Common/Run.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public class Run
2929
private readonly Type _type;
3030
private readonly MethodInfo _method;
3131
private readonly ConstructorInfo _constructor;
32+
private readonly bool _awaitableMethod;
3233

33-
public Run(Type type, MethodInfo method, ConstructorInfo constructor)
34+
public Run(Type type, MethodInfo method, ConstructorInfo constructor, bool awaitableMethod)
3435
{
3536
_type = type;
3637
_method = method;
3738
_constructor = constructor;
39+
_awaitableMethod = awaitableMethod;
3840
}
3941

4042
public async Task HandleRequest(HttpContext httpContext)
@@ -79,7 +81,14 @@ await Console.Error.WriteLineAsync(
7981

8082
try
8183
{
82-
JObject output = (JObject) _method.Invoke(owObject, new object[] {valObject});
84+
JObject output;
85+
86+
if(_awaitableMethod) {
87+
output = (JObject) await (dynamic) _method.Invoke(owObject, new object[] {valObject});
88+
}
89+
else {
90+
output = (JObject) _method.Invoke(owObject, new object[] {valObject});
91+
}
8392

8493
if (output == null)
8594
{

tests/dotnetshared/Echo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using System;
1919
using Newtonsoft.Json.Linq;
20+
using System.Threading.Tasks;
2021

2122
namespace Apache.OpenWhisk.Tests.Dotnet
2223
{
@@ -26,5 +27,11 @@ public JObject Main(JObject args)
2627
{
2728
return (args);
2829
}
30+
31+
public async Task<JObject> MainAsync(JObject args)
32+
{
33+
await Task.Delay(10); // Just do a delay to have an async/await process occur.
34+
return (args);
35+
}
2936
}
3037
}

tests/src/test/scala/actionContainers/DotNet2_2ActionContainerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DotNet2_2ActionContainerTests extends BasicActionRunnerTests with WskActor
5252
}
5353

5454
val testEchoNoWrite = {
55-
TestConfig(functionb64, main = "Apache.OpenWhisk.Tests.Dotnet::Apache.OpenWhisk.Tests.Dotnet.Echo::Main")
55+
TestConfig(functionb64, main = "Apache.OpenWhisk.Tests.Dotnet::Apache.OpenWhisk.Tests.Dotnet.Echo::MainAsync")
5656
}
5757

5858
override val testUnicode = {

tests/src/test/scala/actionContainers/DotNet3_0ActionContainerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DotNet3_0ActionContainerTests extends BasicActionRunnerTests with WskActor
5252
}
5353

5454
val testEchoNoWrite = {
55-
TestConfig(functionb64, main = "Apache.OpenWhisk.Tests.Dotnet::Apache.OpenWhisk.Tests.Dotnet.Echo::Main")
55+
TestConfig(functionb64, main = "Apache.OpenWhisk.Tests.Dotnet::Apache.OpenWhisk.Tests.Dotnet.Echo::MainAsync")
5656
}
5757

5858
override val testUnicode = {

0 commit comments

Comments
 (0)