-
-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #871 from Cysharp/feature/JsonTranscoding
MagicOnion.Server.JsonTranscoding
- Loading branch information
Showing
64 changed files
with
3,501 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/JsonTranscoding/JsonTranscodingSample.Server/JsonTranscodingSample.Server.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\MagicOnion.Server.JsonTranscoding.Swagger\MagicOnion.Server.JsonTranscoding.Swagger.csproj" /> | ||
<ProjectReference Include="..\..\..\src\MagicOnion.Server.JsonTranscoding\MagicOnion.Server.JsonTranscoding.csproj" /> | ||
<ProjectReference Include="..\..\..\src\MagicOnion.Server\MagicOnion.Server.csproj" /> | ||
<ProjectReference Include="..\JsonTranscodingSample.Shared\JsonTranscodingSample.Shared.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
7 changes: 7 additions & 0 deletions
7
samples/JsonTranscoding/JsonTranscodingSample.Server/MyFirstHub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using JsonTranscodingSample.Shared; | ||
using MagicOnion.Server.Hubs; | ||
|
||
namespace JsonTranscodingSample.Server; | ||
|
||
// NOTE: JsonTranscoding is not supported for StreamingHub. JsonTranscoding will ignore the StreamingHub. | ||
public class MyFirstHub : StreamingHubBase<IMyFirstHub, IMyFirstHubReceiver>, IMyFirstHub; |
44 changes: 44 additions & 0 deletions
44
samples/JsonTranscoding/JsonTranscodingSample.Server/MyFirstService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Grpc.Core; | ||
using JsonTranscodingSample.Shared; | ||
using MagicOnion; | ||
using MagicOnion.Server; | ||
|
||
namespace JsonTranscodingSample.Server; | ||
|
||
public class MyFirstService : ServiceBase<IMyFirstService>, IMyFirstService | ||
{ | ||
public UnaryResult<string> SayHelloAsync(string name, int age) | ||
{ | ||
return UnaryResult.FromResult($"Hello {name} ({age})!"); | ||
} | ||
|
||
public UnaryResult<RegisterUserResponse> RegisterUserAsync(RegisterUserRequest request) | ||
{ | ||
return new UnaryResult<RegisterUserResponse>(new RegisterUserResponse() | ||
{ | ||
Success = true, | ||
Message = $"Welcome {request.Name}!", | ||
RegisteredUser = request, | ||
}); | ||
} | ||
|
||
public UnaryResult<RegisterUserResponse> RegisterUserWithRoleAsync(RegisterUserRequest request, string role) | ||
{ | ||
return new UnaryResult<RegisterUserResponse>(new RegisterUserResponse() | ||
{ | ||
Success = true, | ||
Message = $"Welcome {request.Name}! You are registered as '{role}'.", | ||
RegisteredUser = request, | ||
}); | ||
} | ||
|
||
public UnaryResult ThrowAsync() | ||
{ | ||
throw new InvalidOperationException("Something went wrong."); | ||
} | ||
|
||
public UnaryResult ThrowWithReturnStatusCodeAsync(int statusCode, string detail) | ||
{ | ||
throw new ReturnStatusException((StatusCode)statusCode, detail); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
samples/JsonTranscoding/JsonTranscodingSample.Server/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
builder.Services.AddGrpc(); | ||
|
||
// Add MagicOnion services to the container and enable JSON transcoding feature. | ||
builder.Services.AddMagicOnion().AddJsonTranscoding(); | ||
// Add MagicOnion JSON transcoding Swagger support. | ||
builder.Services.AddMagicOnionJsonTranscodingSwagger(); | ||
// Add Swagger generator services. | ||
builder.Services.AddSwaggerGen(options => | ||
{ | ||
// Reflect the XML documentation comments of the service definition in Swagger. | ||
options.IncludeMagicOnionXmlComments(Path.Combine(AppContext.BaseDirectory, "JsonTranscodingSample.Shared.xml")); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
|
||
// Enable middleware to serve generated Swagger as a JSON endpoint. | ||
app.UseSwagger(); | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); | ||
}); | ||
} | ||
|
||
app.MapMagicOnionService(); | ||
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); | ||
|
||
app.Run(); |
32 changes: 32 additions & 0 deletions
32
samples/JsonTranscoding/JsonTranscodingSample.Server/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": false, | ||
"applicationUrl": "http://localhost:5073", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": false, | ||
"applicationUrl": "https://localhost:7192;http://localhost:5073", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https (Production)": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": false, | ||
"applicationUrl": "https://localhost:7192;http://localhost:5073", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Production" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/JsonTranscoding/JsonTranscodingSample.Server/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/JsonTranscoding/JsonTranscodingSample.Server/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"Kestrel": { | ||
"EndpointDefaults": { | ||
"Protocols": "Http2" | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
samples/JsonTranscoding/JsonTranscodingSample.Shared/IMyFirstHub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using MagicOnion; | ||
|
||
namespace JsonTranscodingSample.Shared; | ||
|
||
public interface IMyFirstHub : IStreamingHub<IMyFirstHub, IMyFirstHubReceiver>; | ||
public interface IMyFirstHubReceiver; |
90 changes: 90 additions & 0 deletions
90
samples/JsonTranscoding/JsonTranscodingSample.Shared/IMyFirstService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using MagicOnion; | ||
using MessagePack; | ||
|
||
namespace JsonTranscodingSample.Shared; | ||
|
||
/// <summary> | ||
/// This is a service interface for the demonstration. | ||
/// </summary> | ||
public interface IMyFirstService : IService<IMyFirstService> | ||
{ | ||
/// <summary> | ||
/// Say hello to the specified name. | ||
/// </summary> | ||
/// <param name="name"> A name to say hello. </param> | ||
/// <param name="age">An age of the person to say hello.</param> | ||
/// <returns>Returns a message to the specified name.</returns> | ||
UnaryResult<string> SayHelloAsync(string name, int age); | ||
|
||
/// <summary> | ||
/// Register a user with the specified request. | ||
/// </summary> | ||
/// <param name="request">The request to register a user.</param> | ||
/// <returns>Returns a response to the user registration request.</returns> | ||
UnaryResult<RegisterUserResponse> RegisterUserAsync(RegisterUserRequest request); | ||
|
||
/// <summary> | ||
/// Register a user with the specified request and role. | ||
/// </summary> | ||
/// <param name="request">The request to register a user.</param> | ||
/// <param name="role">A role to assign to the user.</param> | ||
/// <returns>Returns a response to the user registration request.</returns> | ||
UnaryResult<RegisterUserResponse> RegisterUserWithRoleAsync(RegisterUserRequest request, string role); | ||
|
||
/// <summary> | ||
/// Throw an exception. | ||
/// </summary> | ||
/// <returns></returns> | ||
UnaryResult ThrowAsync(); | ||
|
||
/// <summary> | ||
/// Throw an exception with the specified status code and detail. | ||
/// </summary> | ||
/// <param name="statusCode">A status code to return.</param> | ||
/// <param name="detail">A detail message to return.</param> | ||
/// <returns></returns> | ||
UnaryResult ThrowWithReturnStatusCodeAsync(int statusCode, string detail); | ||
} | ||
|
||
/// <summary> | ||
/// Represents a request to register a user. | ||
/// </summary> | ||
[MessagePackObject] | ||
public class RegisterUserRequest | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of the user. | ||
/// </summary> | ||
[Key(0)] | ||
public required string Name { get; init; } | ||
|
||
/// <summary> | ||
/// Gets or sets the age of the user. | ||
/// </summary> | ||
[Key(1)] | ||
public required int Age { get; init; } | ||
} | ||
|
||
/// <summary> | ||
/// Represents a response to a user registration request. | ||
/// </summary> | ||
public class RegisterUserResponse | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the operation was successful. | ||
/// </summary> | ||
[Key(0)] | ||
public required bool Success { get; init; } | ||
|
||
/// <summary> | ||
/// Gets or sets a message. | ||
/// </summary> | ||
[Key(1)] | ||
public required string Message { get; init; } | ||
|
||
/// <summary> | ||
/// Gets or sets the registered user. | ||
/// </summary> | ||
[Key(2)] | ||
public required RegisterUserRequest RegisteredUser { get; init; } | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/JsonTranscoding/JsonTranscodingSample.Shared/JsonTranscodingSample.Shared.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\MagicOnion.Abstractions\MagicOnion.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.