메인 콘텐츠로 건너뛰기
Translation of pages other than English and Japanese versions is maintained by community contributions. The project does not guarantee the accuracy of the content and may not reflect the latest content.

Pre-generated client codes

최종 업데이트

MagicOnion은 Unity, NativeAOT 등 Pre-generated client codes가 필요한 플랫폼을 Source Generator로 지원합니다.

Source Generator는 MagicOnion.Client 패키지에 포함되어 있으므로, 이전 버전과 같은 코드 생성 도구(moc)의 설치나 추가 빌드 단계 설정이 불필요합니다.

지원되는 개발 환경

  • Unity 2022.3.0f1 이상
  • .NET 6 이상
  • Visual Studio 2022 version 17.2 이상
  • Rider 2023.1 이상

사용 방법

임의의 이름의 partial 클래스를 애플리케이션 내에 정의합니다. 그리고 MagicOnionClientGeneration 속성을 붙이고, 서비스 인터페이스를 검색할 대상이 되는 어셈블리 내의 임의의 타입을 지정합니다.

예를 들어 MyApp.Shared 어셈블리에 MyApp.Shared.Services.IGreeterServiceMyApp.Shared.Hubs.IChatHub가 포함되어 있는 경우 그 중 하나를 지정합니다.

using MagicOnion.Client;

[MagicOnionClientGeneration(typeof(MyApp.Shared.Services.IGreeterService))]
partial class MagicOnionGeneratedClientInitializer {}

다음으로 생성된 MessagePack Resolver(MagicOnionGeneratedClientInitializer.Resolver 프로퍼티)를 사용하도록 MessagePack을 구성합니다. 이는 기존의 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,
StandardResolver.Instance
);

MessagePackSerializer.DefaultOptions = MessagePackSerializer.DefaultOptions
.WithResolver(StaticCompositeResolver.Instance);
}

소스 생성 옵션

생성자의 명명된 인자로 옵션을 지정할 수 있습니다.

  • DisableAutoRegistration: 시작 시 <MagicOnionClientGeneration속성을 추가한 클래스>.Register 메소드를 자동으로 호출하는 기능을 비활성화할지 여부를 설정합니다 (자동 등록은 .NET 5+ 또는 Unity에서만 작동합니다)
    • 이 옵션으로 Register를 호출하지 않은 경우, 수동으로 호출하거나 클라이언트 생성 시 ClientFactoryProvider 또는 StreamingHubClientFactoryProvider 프로퍼티를 전달해야 합니다
  • Serializer: 직렬화에 사용할 시리얼라이저를 설정합니다. 기본값은 GenerateSerializerType.MessagePack입니다

추가 옵션

MagicOnionClientGenerationOption 속성으로 추가 옵션을 지정할 수 있습니다.

MessagePack을 위해서는 과거의 mpc와의 호환성을 위한 옵션이 존재합니다.

  • MessagePack.FormatterNamespace: 사전 생성된 Formatter의 네임스페이스를 지정합니다. 기본값은 MessagePack.Formatters입니다
  • MessagePack.GenerateResolverForCustomFormatter: 사전 생성된 MessagePack Formatter를 사용할지 여부를 지정합니다. 기본값은 false입니다