-
Notifications
You must be signed in to change notification settings - Fork 18
Create .NET RPC sample that doesn't use codegen #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json.Serialization; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SampleRpcClient | ||
| { | ||
| public class PayloadObject | ||
| { | ||
| [JsonPropertyName("SomeField")] | ||
| public string? SomeField { get; set; } | ||
|
|
||
| [JsonPropertyName("OtherField")] | ||
| public string? OtherField { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using Azure.Iot.Operations.Mqtt.Session; | ||
| using Azure.Iot.Operations.Protocol; | ||
| using Azure.Iot.Operations.Protocol.Connection; | ||
| using Microsoft.Extensions.Configuration; | ||
| using SampleRpcClient; | ||
|
|
||
| string commandName = "someCommandName"; | ||
|
|
||
| var configuration = new ConfigurationBuilder() | ||
| .AddJsonFile("appsettings.json") | ||
| .AddEnvironmentVariables() | ||
| .AddCommandLine(args) | ||
| .Build(); | ||
|
|
||
| var mqttDiag = Convert.ToBoolean(configuration["mqttDiag"]); | ||
| if (mqttDiag) Trace.Listeners.Add(new ConsoleTraceListener()); | ||
| await using MqttSessionClient mqttClient = new(new MqttSessionClientOptions { EnableMqttLogging = mqttDiag }); | ||
| ApplicationContext applicationContext = new(); | ||
|
|
||
| await using SampleCommandInvoker rpcInvoker = new(applicationContext, mqttClient, commandName, new Utf8JsonSerializer()); | ||
|
|
||
| await mqttClient.ConnectAsync(MqttConnectionSettings.FromEnvVars()); | ||
|
|
||
| var payload = new PayloadObject() | ||
| { | ||
| SomeField = "someValue", | ||
| OtherField = "someOtherValue" | ||
| }; | ||
|
|
||
| PayloadObject response = (await rpcInvoker.InvokeCommandAsync(payload)).Response; | ||
| Console.WriteLine("Received RPC response"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Azure.Iot.Operations.Protocol; | ||
| using Azure.Iot.Operations.Protocol.RPC; | ||
|
|
||
| namespace SampleRpcClient | ||
| { | ||
| [CommandTopic("rpc/command-samples/{commandName}")] | ||
| public class SampleCommandInvoker : CommandInvoker<PayloadObject, PayloadObject> | ||
| { | ||
| public SampleCommandInvoker(ApplicationContext applicationContext, IMqttPubSubClient mqttClient, string commandName, IPayloadSerializer serializer) : base(applicationContext, mqttClient, commandName, serializer) | ||
| { | ||
| TopicTokenMap["commandName"] = commandName; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Azure.Iot.Operations.Mqtt\Azure.Iot.Operations.Mqtt.csproj" /> | ||
| <ProjectReference Include="..\..\..\src\Azure.Iot.Operations.Protocol\Azure.Iot.Operations.Protocol.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Update="appsettings.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Buffers; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Azure.Iot.Operations.Protocol; | ||
| using Azure.Iot.Operations.Protocol.Models; | ||
|
|
||
| namespace SampleRpcClient; | ||
|
|
||
| public class EmptyJson | ||
| { | ||
| } | ||
|
|
||
| public class Utf8JsonSerializer : IPayloadSerializer | ||
| { | ||
| protected static readonly JsonSerializerOptions _jsonSerializerOptions = new() | ||
| { | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
| }; | ||
|
|
||
| public const string ContentType = "application/json"; | ||
|
|
||
| public const MqttPayloadFormatIndicator PayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData; | ||
|
|
||
| public T FromBytes<T>(ReadOnlySequence<byte> payload, string? contentType, MqttPayloadFormatIndicator payloadFormatIndicator) | ||
| where T : class | ||
| { | ||
| if (contentType != null && contentType != ContentType) | ||
| { | ||
| throw new AkriMqttException($"Content type {contentType} is not supported by this implementation; only {ContentType} is accepted.") | ||
| { | ||
| Kind = AkriMqttErrorKind.HeaderInvalid, | ||
| HeaderName = "Content Type", | ||
| HeaderValue = contentType, | ||
| IsShallow = false, | ||
| IsRemote = false, | ||
| }; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if (payload.IsEmpty) | ||
| { | ||
| if (typeof(T) != typeof(EmptyJson)) | ||
| { | ||
| throw AkriMqttException.GetPayloadInvalidException(); | ||
| } | ||
|
|
||
| return (new EmptyJson() as T)!; | ||
| } | ||
|
|
||
| Utf8JsonReader reader = new(payload); | ||
| return JsonSerializer.Deserialize<T>(ref reader, _jsonSerializerOptions)!; | ||
| } | ||
| catch (Exception) | ||
| { | ||
| throw AkriMqttException.GetPayloadInvalidException(); | ||
| } | ||
| } | ||
|
|
||
| public SerializedPayloadContext ToBytes<T>(T? payload) | ||
| where T : class | ||
| { | ||
| try | ||
| { | ||
| if (typeof(T) == typeof(EmptyJson)) | ||
| { | ||
| return new(ReadOnlySequence<byte>.Empty, ContentType, PayloadFormatIndicator); | ||
| } | ||
|
|
||
| return new(new(JsonSerializer.SerializeToUtf8Bytes(payload, _jsonSerializerOptions)), ContentType, PayloadFormatIndicator); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| throw AkriMqttException.GetPayloadInvalidException(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "mqttDiag": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json.Serialization; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SampleRpcServer | ||
| { | ||
| public class PayloadObject | ||
| { | ||
| [JsonPropertyName("SomeField")] | ||
| public string? SomeField { get; set; } | ||
|
|
||
| [JsonPropertyName("OtherField")] | ||
| public string? OtherField { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System.Diagnostics; | ||
| using Azure.Iot.Operations.Mqtt.Session; | ||
| using Azure.Iot.Operations.Protocol; | ||
| using Azure.Iot.Operations.Protocol.Connection; | ||
| using Microsoft.Extensions.Configuration; | ||
| using SampleRpcServer; | ||
|
|
||
| string commandName = "someCommandName"; | ||
|
|
||
| var configuration = new ConfigurationBuilder() | ||
| .AddJsonFile("appsettings.json") | ||
| .AddEnvironmentVariables() | ||
| .AddCommandLine(args) | ||
| .Build(); | ||
|
|
||
| var mqttDiag = Convert.ToBoolean(configuration["mqttDiag"]); | ||
| if (mqttDiag) Trace.Listeners.Add(new ConsoleTraceListener()); | ||
| await using MqttSessionClient mqttClient = new(new MqttSessionClientOptions { EnableMqttLogging = mqttDiag }); | ||
| ApplicationContext applicationContext = new(); | ||
|
|
||
| await mqttClient.ConnectAsync(MqttConnectionSettings.FromEnvVars()); | ||
|
|
||
| await using SampleCommandExecutor rpcExecutor = new(applicationContext, mqttClient, commandName, new Utf8JsonSerializer()) | ||
| { | ||
| OnCommandReceived = (request, cancellationToken) => | ||
| { | ||
| Console.WriteLine("Received RPC request"); | ||
|
|
||
| var responsePayload = new PayloadObject() | ||
| { | ||
| SomeField = request.Request.SomeField, | ||
| OtherField = request.Request.OtherField, | ||
| }; | ||
|
|
||
| // Echo the payload back to the sender | ||
| return Task.FromResult(new Azure.Iot.Operations.Protocol.RPC.ExtendedResponse<PayloadObject>() | ||
| { | ||
| Response = responsePayload | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| await rpcExecutor.StartAsync(); | ||
|
|
||
| await Task.Delay(TimeSpan.FromMinutes(1)); | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Azure.Iot.Operations.Protocol; | ||
| using Azure.Iot.Operations.Protocol.RPC; | ||
|
|
||
| namespace SampleRpcServer | ||
| { | ||
| [CommandTopic("rpc/command-samples/{commandName}")] | ||
| public class SampleCommandExecutor : CommandExecutor<PayloadObject, PayloadObject> | ||
| { | ||
| public SampleCommandExecutor(ApplicationContext applicationContext, IMqttPubSubClient mqttClient, string commandName, IPayloadSerializer serializer) : base(applicationContext, mqttClient, commandName, serializer) | ||
| { | ||
| TopicTokenMap["commandName"] = commandName; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Azure.Iot.Operations.Mqtt\Azure.Iot.Operations.Mqtt.csproj" /> | ||
|
||
| <ProjectReference Include="..\..\..\src\Azure.Iot.Operations.Protocol\Azure.Iot.Operations.Protocol.csproj" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Update="appsettings.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
| </Project> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please just add the Utf8JsonSerializer into the SDK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not. This library's core is not about serialization. If we pin ourselves to any particular serialization package (even System.Text.Json) we open ourselves up to inevitable security issues from that library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've already gone through security reviews for this library where we were explicitly told that this is the correct way to handle serialization libraries. Changing course now may require another round of security reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the code generation, would create this class I don't see the point, in the end tooling from us would cause this dependency in the application of the customer. Also, System.Text.Json is part of dotnet framework and no separate nuget anymore and used in other places also.