6.0.0
版本发布时间: 2024-01-11 18:26:46
Cysharp/MagicOnion最新发布版本:6.1.4(2024-07-17 15:51:31)
Highlights
MagicOnion.Client.SourceGenerator in https://github.com/Cysharp/MagicOnion/pull/688
This release introduces a source generator for platform that require pre-generated client code (e.g. Unity, NativeAOT, MAUI ...). The source generator also replaces MagicOnion.Generator (moc).
MagicOnion.Client.SourceGenerator is shipped with MagicOnion.Client package. This means that you no longer need to the install generator tool (moc) and setup additional build steps.
Supported development environments
- Unity 2021.3.0f1 or later
- .NET 6 or later
- Visual Studio 2022 version 17.2 or later
- Rider 2023.1 or later
Usage
Define a partial
class with any name of your choosing within the application. Mark it with the MagicOnionClientGeneration
attribute, and specify any service type found within the assembly where you want to search for the service interface.
For example, if the MyApp.Shared
assembly contains MyApp.Shared.Services.IGreeterService
and MyApp.Shared.Hubs.IChatHub
, specify one of them.
using MagicOnion.Client;
[MagicOnionClientGeneration(typeof(MyApp.Shared.Services.IGreeterService))]
partial class MagicOnionGeneratedClientInitializer {}
Next, configure MessagePack to use the generated MessagePack Resolver. This is the same as when using the legacy MagicOnion.Generator.
#if UNITY_2019_4_OR_NEWER
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)]
#elif NET5_0_OR_GREATER
[System.Runtime.CompilerServices.ModuleInitializer]
#endif
static void RegisterResolvers()
{
StaticCompositeResolver.Instance.Register(
// Add: Use MessagePack formatter resolver generated by the source generator.
MagicOnionGeneratedClientInitializer.Resolver,
MessagePack.Resolvers.GeneratedResolver.Instance,
BuiltinResolver.Instance,
PrimitiveObjectResolver.Instance
);
MessagePackSerializer.DefaultOptions = MessagePackSerializer.DefaultOptions
.WithResolver(StaticCompositeResolver.Instance);
}
Source generation options
You can specify options in the named constructor of the attribute.
-
DisableAutoRegistration
: Sets whether to disable automatically callingRegister
during start-up. (Automatic registration requires .NET 5+ or Unity) -
MessagePackFormatterNamespace
: Sets the namespace of pre-generated MessagePackFormatters. The default value isMessagePack.Formatters
. -
Serializer
: Sets the serializer used for message serialization. The default value isGenerateSerializerType.MessagePack
.
Breaking changes
MagicOnion.Generator (moc) has been removed. The legacy generator is no longer supported.
[!WARNING] While there is currently compatibility between MagicOnion.Client and MagicOnion.Generator (moc), there is no guarantee that this will be maintained.
Introduce ServiceContext.SetRawBytesResponse in https://github.com/Cysharp/MagicOnion/pull/677
This PR introduces ServiceContext.SetRawBytesResponse
method.
This method allows you to set raw byte sequences as a response. This makes it possible to send a cached response body without serialization.
public override async ValueTask Invoke(ServiceContext context, Func<ServiceContext, ValueTask> next)
{
if (ResponseBytesCache.TryGetValue(context.CallContext.Method, out var cachedBytes))
{
context.SetRawBytesResponse(cachedBytes);
return;
}
await next(context);
ResponseBytesCache[context.CallContext.Method] = MessagePackSerializer.Serialize(context.Result);
}
[!NOTE] The raw byte sequence must be serialized as a MessagePack (or custom serialization) format. MagicOnion will write a byte requence directly into the response buffer.
Add StreamingHub metrics in https://github.com/Cysharp/MagicOnion/pull/716
This release introduces metrics related to StreamingHub using System.Diagnostics.Metrics.
Meter: MagicOnion.Server
Metric | Unit | Tags |
---|---|---|
magiconion.server.streaminghub.connections | {connection} |
rpc.system , rpc.service |
magiconion.server.streaminghub.method_duration | ms |
rpc.system , rpc.service , rpc.method |
magiconion.server.streaminghub.method_completed | {request} |
rpc.system , rpc.service , rpc.method , magiconion.streaminghub.is_error |
magiconion.server.streaminghub.exceptions | {exception} |
rpc.system , rpc.service , rpc.method , error.type |
Tags
Tag name | Value |
---|---|
rpc.system | magiconion |
rpc.service | StreamingHub interface name (e.g. IGreeterService ) |
rpc.method | StreamingHub method name (e.g. HelloAsync ) |
magiconion.streaminghub.is_error | Whether a StreamingHub method call succeeded or failed. (e.g. true or false ) |
error.type | Thrown exception type (e.g. System.InvalidOperationException ) |
Breaking Changes
Use Grpc.Net.Client by default on Unity in https://github.com/Cysharp/MagicOnion/pull/704
This release changes the default gRPC library in Unity to grpc-dotnet. From this release, the recommended gRPC library combination is YetAnotherHttpHandler and grpc-dotnet.
However, at this time, we are still supporting C-core. If you continue to use the C-core gRPC library, please define USE_GRPC_CCORE
in "Scripting Define Symbols". We recommend transitioning as there is the possibility of removing support in the future.
refs: #661
Other Breaking Changes
- Switch to ILogger-based logging by @mayuki in https://github.com/Cysharp/MagicOnion/pull/683
- Remove MagicOnion.Server.OpenTelemetry by @mayuki in https://github.com/Cysharp/MagicOnion/pull/692
- Cleanup MagicOnion.Shared project by @mayuki in https://github.com/Cysharp/MagicOnion/pull/699
- Remove GenerateDefineDebugAttribute and GenerateIfDirectiveAttribute by @mayuki in https://github.com/Cysharp/MagicOnion/pull/701
- Reorganize internal shared code dependency by @mayuki in https://github.com/Cysharp/MagicOnion/pull/707
- Move DynamicClientFactoryProviders to DynamicClient by @mayuki in https://github.com/Cysharp/MagicOnion/pull/712
- Remove UniTask support by @mayuki in https://github.com/Cysharp/MagicOnion/pull/729
What's Changes
Features
- Add MagicOnionClient.Create overload by @mayuki in https://github.com/Cysharp/MagicOnion/pull/684
- MagicOnion.Client targets C# 9 and enable nullable annotations by @mayuki in https://github.com/Cysharp/MagicOnion/pull/698
- Adopt to .NET 8 by @mayuki in https://github.com/Cysharp/MagicOnion/pull/730
Other Changes
- Run continuation of hub method synchronously on Unity WebGL by @mayuki in https://github.com/Cysharp/MagicOnion/pull/659
- Replace Moq with NSubstitute by @mayuki in https://github.com/Cysharp/MagicOnion/pull/672
- Fix an incorrect root when a project references a shared project. by @mayuki in https://github.com/Cysharp/MagicOnion/pull/674
- Make to accept a factory for GrpcChannelOptions by @mayuki in https://github.com/Cysharp/MagicOnion/pull/678
- Fix code generation for formatter of Enum nested in a class by @mayuki in https://github.com/Cysharp/MagicOnion/pull/679
- Make marker response bytes a constant by @mayuki in https://github.com/Cysharp/MagicOnion/pull/681
- Refactor StreamingHub by @mayuki in https://github.com/Cysharp/MagicOnion/pull/682
- Update dependencies by @mayuki in https://github.com/Cysharp/MagicOnion/pull/697
- Fix code generation for MemoryPack by @mayuki in https://github.com/Cysharp/MagicOnion/pull/700
- Revert "Merge pull request #701 from Cysharp/feature/RemoveGenerateAttribute by @mayuki in https://github.com/Cysharp/MagicOnion/pull/706
- Fix check for ignored assembly names by @mayuki in https://github.com/Cysharp/MagicOnion/pull/711
- Update action.yaml to use Cysharp/Actions/setup-dotnet by @guitarrapc in https://github.com/Cysharp/MagicOnion/pull/715
- Restore MethodCollector tests by @mayuki in https://github.com/Cysharp/MagicOnion/pull/721
- Add support for interface inheritance on StreamingHub by @mayuki in https://github.com/Cysharp/MagicOnion/pull/722
- Fix error when a source code contains alias-qualified attribute names by @mayuki in https://github.com/Cysharp/MagicOnion/pull/727
Full Changelog: https://github.com/Cysharp/MagicOnion/compare/5.1.8...6.0.0
1、 MagicOnion.Client.Unity.unitypackage 266.66KB