iTranslated by AI
[C#] Trying out MagicOnion JsonTranscoding
Introduction
When developing servers with MagicOnion, it is convenient to have a UI for operation verification.
A feature called JsonTranscoding is officially provided.
So, let's try using it right away.[1]
Installation
First, install MagicOnion.Server.JsonTranscoding.Swagger from NuGet.
dotnet add package MagicOnion.Server.JsonTranscoding.Swagger
Next, modify Program.cs as follows.
// Omitted
var isDevelopment = builder.Environment.IsDevelopment();
var magicOnion = builder.Services.AddMagicOnion();
if (isDevelopment)
{
// Enable Swagger specifically for development
magicOnion.AddJsonTranscoding();
builder.Services.AddMagicOnionJsonTranscodingSwagger();
builder.Services.AddSwaggerGen();
}
var app = builder.Build();
if (isDevelopment)
{
// Enable Swagger specifically for development
app.UseSwagger();
app.UseSwaggerUI(c =>
{
// Display Swagger UI at the root path
c.RoutePrefix = "";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}
// Omitted
If you are not hosting via HTTPS, Swagger UI will not work when running on HTTP/2, so modify appsettings.json accordingly.
{
"Kestrel": {
"Endpoints": {
"http": {
"Url": "http://localhost:5110",
"Protocols": "Http2"
+ },
+ "swagger": {
+ "Url": "http://localhost:5111",
+ "Protocols": "Http1"
}
}
}
}
Trying it out
Let's try running it with Aspire. Please refer to this article for instructions on how to do so.
When it starts up, it will look like this:

Then, when you access http://localhost:5111...

You can now call the API like this. This is very convenient.
Displaying Stack Traces When Exceptions Occur
As it is, stack traces are not displayed when an exception occurs. Specifically, an error like the following is returned:

Combined with the fact that it doesn't stop at the exception even during Debug execution, it's quite inconvenient! So, I looked for a way to display the stack trace.[2]
It seems that enabling options.IsReturnExceptionStackTraceInErrorDetail will do the trick. So, I will add it as follows:
var isDevelopment = builder.Environment.IsDevelopment();
-var magicOnion = builder.Services.AddMagicOnion();
+var magicOnion = builder.Services.AddMagicOnion(option =>
+{
+ option.IsReturnExceptionStackTraceInErrorDetail = isDevelopment;
+});
Then...

It worked. This will make debugging much more efficient.[3]
Displaying on the Client Side
This is also mentioned in the documentation, but you can receive it as follows:
try {
await sampleService.GetSampleData();
}
catch(RpcException ex) {
// = StatusCode.Unknown
Console.WriteLine($"Code: {ex.Status.StatusCode}");
// This will contain the stack trace
Console.WriteLine($"Error: {ex.Status.Detail}");
}
-
The official documentation doesn't have much content, so I'm exploring as I go... ↩︎
-
I searched by reading the source code. Then, while writing this article, I noticed it was mentioned at the very end of the documentation. ↩︎
-
Be sure to disable this in production environments. That goes without saying. ↩︎
Discussion