Skip to content

Commit 44ca649

Browse files
authored
Move to new invoke proto, specify contenttype in response to the dapr… (#295)
* Move to new invoke proto, specify contenttype in response to the dapr/config message, clarify sample * Update for additional proto changes * cr Co-authored-by: LM <lemai>
1 parent c73afa9 commit 44ca649

22 files changed

+528
-304
lines changed

samples/AspNetCore/RoutingSample/Startup.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
namespace RoutingSample
77
{
8+
using System;
89
using System.Text.Json;
910
using System.Threading.Tasks;
10-
using Dapr;
1111
using Dapr.Client;
1212
using Microsoft.AspNetCore.Builder;
1313
using Microsoft.AspNetCore.Hosting;
@@ -83,25 +83,33 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSeri
8383

8484
async Task Balance(HttpContext context)
8585
{
86+
Console.WriteLine("Enter Balance");
8687
var client = context.RequestServices.GetRequiredService<DaprClient>();
8788

8889
var id = (string)context.Request.RouteValues["id"];
90+
Console.WriteLine("id is {0}", id);
8991
var account = await client.GetStateAsync<Account>(StoreName, id);
9092
if (account == null)
9193
{
94+
Console.WriteLine("Account not found");
9295
context.Response.StatusCode = 404;
9396
return;
9497
}
9598

99+
Console.WriteLine("Account balance is {0}", account.Balance);
100+
96101
context.Response.ContentType = "application/json";
97102
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
98103
}
99104

100105
async Task Deposit(HttpContext context)
101106
{
107+
Console.WriteLine("Enter Deposit");
108+
102109
var client = context.RequestServices.GetRequiredService<DaprClient>();
103110

104111
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
112+
Console.WriteLine("Id is {0}, Amount is {1}", transaction.Id, transaction.Amount);
105113
var account = await client.GetStateAsync<Account>(StoreName, transaction.Id);
106114
if (account == null)
107115
{
@@ -110,37 +118,43 @@ async Task Deposit(HttpContext context)
110118

111119
if (transaction.Amount < 0m)
112120
{
121+
Console.WriteLine("Invalid amount");
113122
context.Response.StatusCode = 400;
114123
return;
115124
}
116125

117126
account.Balance += transaction.Amount;
118127
await client.SaveStateAsync(StoreName, transaction.Id, account);
128+
Console.WriteLine("Balance is {0}", account.Balance);
119129

120130
context.Response.ContentType = "application/json";
121131
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
122132
}
123133

124134
async Task Withdraw(HttpContext context)
125135
{
136+
Console.WriteLine("Enter Withdraw");
126137
var client = context.RequestServices.GetRequiredService<DaprClient>();
127-
128138
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
139+
Console.WriteLine("Id is {0}", transaction.Id);
129140
var account = await client.GetStateAsync<Account>(StoreName, transaction.Id);
130141
if (account == null)
131142
{
143+
Console.WriteLine("Account not found");
132144
context.Response.StatusCode = 404;
133145
return;
134146
}
135147

136148
if (transaction.Amount < 0m)
137149
{
150+
Console.WriteLine("Invalid amount");
138151
context.Response.StatusCode = 400;
139152
return;
140153
}
141154

142155
account.Balance -= transaction.Amount;
143156
await client.SaveStateAsync(StoreName, transaction.Id, account);
157+
Console.WriteLine("Balance is {0}", account.Balance);
144158

145159
context.Response.ContentType = "application/json";
146160
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);

samples/Client/DaprClient/Program.cs

+62-32
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
namespace DaprClient
77
{
88
using System;
9-
using System.Collections.Generic;
10-
using System.Threading;
9+
using System.Text.Json;
1110
using System.Threading.Tasks;
1211
using Dapr.Client;
12+
using Dapr.Client.Http;
1313

1414
/// <summary>
1515
/// Shows Dapr client calls.
@@ -26,7 +26,15 @@ public class Program
2626
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
2727
public static async Task Main(string[] args)
2828
{
29-
var client = new DaprClientBuilder().Build();
29+
var jsonOptions = new JsonSerializerOptions()
30+
{
31+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
32+
PropertyNameCaseInsensitive = true,
33+
};
34+
35+
var client = new DaprClientBuilder()
36+
.UseJsonSerializationOptions(jsonOptions)
37+
.Build();
3038

3139
await PublishEventAsync(client);
3240

@@ -42,26 +50,31 @@ public static async Task Main(string[] args)
4250
#region Service Invoke - Required RoutingService
4351
// This provides an example of how to invoke a method on another REST service that is listening on http.
4452
// To use it run RoutingService in this solution.
45-
// Invoke deposit operation on RoutingSample service by publishing event.
53+
// Invoke deposit operation on RoutingSample service by publishing event.
54+
4655
//await PublishDepositeEventToRoutingSampleAsync(client);
56+
57+
//await Task.Delay(TimeSpan.FromSeconds(1));
4758

48-
// Invoke deposit operation on RoutingSample service by POST.
59+
//await DepositUsingServiceInvocation(client);
60+
61+
//Invoke deposit operation on RoutingSample service by POST.
4962
//await InvokeWithdrawServiceOperationAsync(client);
5063

51-
// Invoke deposit operation on RoutingSample service by GET.
64+
//Invoke deposit operation on RoutingSample service by GET.
5265
//await InvokeBalanceServiceOperationAsync(client);
5366
#endregion
5467

68+
Console.WriteLine("Done");
5569
}
5670

5771
internal static async Task PublishDepositeEventToRoutingSampleAsync(DaprClient client)
5872
{
59-
var eventData = new { id = "17", amount = (decimal)10, };
73+
var eventData = new { Id = "17", Amount = (decimal)10, };
6074
await client.PublishEventAsync("deposit", eventData);
6175
Console.WriteLine("Published deposit event!");
6276
}
6377

64-
6578
internal static async Task PublishEventAsync(DaprClient client)
6679
{
6780
var eventData = new Widget() { Size = "small", Color = "yellow", };
@@ -95,6 +108,24 @@ internal static async Task DeleteStateAsync(DaprClient client)
95108
Console.WriteLine("Deleted State!");
96109
}
97110

111+
internal static async Task DepositUsingServiceInvocation(DaprClient client)
112+
{
113+
Console.WriteLine("DepositUsingServiceInvocation");
114+
var data = new { id = "17", amount = (decimal)99 };
115+
116+
HTTPExtension httpExtension = new HTTPExtension()
117+
{
118+
Verb = HTTPVerb.Post
119+
};
120+
121+
// Invokes a POST method named "depoit" that takes input of type "Transaction" as define in the RoutingSample.
122+
Console.WriteLine("invoking");
123+
124+
var a = await client.InvokeMethodAsync<object, Account>("routing", "deposit", data, httpExtension);
125+
Console.WriteLine("Returned: id:{0} | Balance:{1}", a.Id, a.Balance);
126+
127+
Console.WriteLine("Completed");
128+
}
98129

99130
/// <summary>
100131
/// This example shows how to invoke a POST method on a service listening on http.
@@ -107,14 +138,16 @@ internal static async Task DeleteStateAsync(DaprClient client)
107138
/// <returns></returns>
108139
internal static async Task InvokeWithdrawServiceOperationAsync(DaprClient client)
109140
{
141+
Console.WriteLine("Invoking withdraw");
110142
var data = new { id = "17", amount = (decimal)10, };
111143

112-
// Add the verb to metadata if the method is other than a POST
113-
var metaData = new Dictionary<string, string>();
114-
metaData.Add("http.verb", "POST");
144+
HTTPExtension httpExtension = new HTTPExtension()
145+
{
146+
Verb = HTTPVerb.Post
147+
};
115148

116-
// Invokes a POST method named "Withdraw" that takes input of type "Transaction" as define in the RoutingSample.
117-
await client.InvokeMethodAsync<object>("routing", "Withdraw", data, metaData);
149+
// Invokes a POST method named "Withdraw" that takes input of type "Transaction" as define in the RoutingSample.
150+
await client.InvokeMethodAsync<object>("routing", "Withdraw", data, httpExtension);
118151

119152
Console.WriteLine("Completed");
120153
}
@@ -130,28 +163,16 @@ internal static async Task InvokeWithdrawServiceOperationAsync(DaprClient client
130163
/// <returns></returns>
131164
internal static async Task InvokeBalanceServiceOperationAsync(DaprClient client)
132165
{
133-
// Add the verb to metadata if the method is other than a POST
134-
var metaData = new Dictionary<string, string>();
135-
metaData.Add("http.verb", "GET");
166+
Console.WriteLine("Invoking balance");
136167

137168
// Invokes a GET method named "hello" that takes input of type "MyData" and returns a string.
138-
var res = await client.InvokeMethodAsync<object>("routing", "17", metaData);
139-
140-
Console.WriteLine($"Received balance {res}");
141-
}
142-
143-
/// <summary>
144-
/// This example shows how to invoke a method on a service listening on gRPC.
145-
/// </summary>
146-
/// <param name="client"></param>
147-
/// <returns></returns>
148-
internal static async Task InvokeMethodOnGrpcServiceAsync(DaprClient client)
149-
{
150-
MyData data = new MyData() { Message = "mydata" };
169+
HTTPExtension httpExtension = new HTTPExtension()
170+
{
171+
Verb = HTTPVerb.Get
172+
};
173+
var res = await client.InvokeMethodAsync<Account>("routing", "17", httpExtension);
151174

152-
// invokes a method named "hello" that takes input of type "MyData" and returns a string.
153-
string s = await client.InvokeMethodAsync<MyData, string>("nodeapp", "hello", data);
154-
Console.WriteLine("received {0}", s);
175+
Console.WriteLine($"Received balance {res.Balance}");
155176
}
156177

157178
private class Widget
@@ -167,5 +188,14 @@ public MyData()
167188

168189
public String Message { get; set; }
169190
}
191+
192+
193+
internal class Account
194+
{
195+
public string Id { get; set; }
196+
197+
public decimal Balance { get; set; }
198+
}
170199
}
171200
}
201+

src/Dapr.Actors.AspNetCore/RouterBuilderExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public static void AddDaprConfigRoute(this IRouteBuilder routeBuilder)
1616
{
1717
routeBuilder.MapGet("dapr/config", async context =>
1818
{
19-
await ActorRuntime.Instance.SerializeSettingsAndRegisteredTypes(context.Response.BodyWriter);
19+
context.Response.ContentType = "application/json";
20+
await ActorRuntime.Instance.SerializeSettingsAndRegisteredTypes(context.Response.BodyWriter);
2021
});
2122
}
2223

src/Dapr.Client/Dapr.Client.csproj

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Protobuf Include="Protos\dapr\proto\common\v1\common.proto" ProtoRoot="Protos" GrpcServices="Client" />
8+
<Protobuf Include="Protos\dapr\proto\dapr\v1\dapr.proto" ProtoRoot="Protos" GrpcServices="Client" />
9+
</ItemGroup>
10+
11+
<!-- Additional Nuget package properties. -->
12+
<PropertyGroup>
13+
<Description>This package contains the reference assemblies for developing services using Dapr.</Description>
14+
</PropertyGroup>
15+
<ItemGroup>
16+
<PackageReference Include="Google.Protobuf" Version="3.11.4" />
17+
<PackageReference Include="Grpc.Net.Client" Version="2.27.0" />
18+
<PackageReference Include="Grpc.Tools" Version="2.27.0" PrivateAssets="All" />
19+
</ItemGroup>
20+
<ItemGroup>
21+
<Folder Include="Protos\" />
22+
</ItemGroup>
623

7-
<!-- Additional Nuget package properties. -->
8-
<PropertyGroup>
9-
<Description>This package contains the reference assemblies for developing services using Dapr.</Description>
10-
</PropertyGroup>
11-
<ItemGroup>
12-
<Protobuf Include="Protos\dapr.proto" GrpcServices="Client" />
13-
<Protobuf Include="Protos\daprclient.proto" GrpcServices="Client" />
14-
</ItemGroup>
15-
<ItemGroup>
16-
<PackageReference Include="Google.Protobuf" Version="3.11.4" />
17-
<PackageReference Include="Grpc.Net.Client" Version="2.27.0" />
18-
<PackageReference Include="Grpc.Tools" Version="2.27.0" PrivateAssets="All" />
19-
</ItemGroup>
20-
2124
</Project>

0 commit comments

Comments
 (0)