Skip to content

Commit

Permalink
Merge pull request #768 from Cysharp/feature/ImplicitlyAddLoggingAndGrpc
Browse files Browse the repository at this point in the history
Implicitly call AddLogging and AddGrpc
  • Loading branch information
mayuki authored May 8, 2024
2 parents 3ddd92f + 2563db3 commit 13fbd09
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- vNext
tags:
- "!*" # not a tag push
paths-ignore:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- vNext
tags:
- "!*" # not a tag push
paths-ignore:
Expand All @@ -13,6 +14,7 @@ on:
pull_request:
branches:
- main
- vNext
paths-ignore:
- '**.md'
- .github/**
Expand Down
14 changes: 11 additions & 3 deletions src/MagicOnion.Server/Extensions/MagicOnionServicesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,27 @@ public static IMagicOnionServerBuilder AddMagicOnion(this IServiceCollection ser
return services.AddMagicOnionCore(configureOptions);
}

static IMagicOnionServerBuilder AddMagicOnionCore(this IServiceCollection services, Action<MagicOnionOptions>? configureOptions = null)
// NOTE: `internal` is required for unit tests.
internal static IMagicOnionServerBuilder AddMagicOnionCore(this IServiceCollection services, Action<MagicOnionOptions>? configureOptions = null)
{
var configName = Options.Options.DefaultName;
var glueServiceType = MagicOnionGlueService.CreateType();

// Required services (ASP.NET Core, gRPC)
services.AddLogging();
services.AddGrpc();
services.AddMetrics();

// MagicOnion: Core services
var glueServiceType = MagicOnionGlueService.CreateType();
services.TryAddSingleton<IGroupRepositoryFactory, ImmutableArrayGroupRepositoryFactory>();

services.AddSingleton<MagicOnionServiceDefinitionGlueDescriptor>(sp => new MagicOnionServiceDefinitionGlueDescriptor(glueServiceType, sp.GetRequiredService<MagicOnionServiceDefinition>()));
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>).MakeGenericType(glueServiceType), typeof(MagicOnionGlueServiceMethodProvider<>).MakeGenericType(glueServiceType)));

services.AddMetrics();
// MagicOnion: Metrics
services.TryAddSingleton<MagicOnionMetrics>();

// MagicOnion: Options
services.AddOptions<MagicOnionOptions>(configName)
.Configure<IConfiguration>((o, configuration) =>
{
Expand Down
33 changes: 12 additions & 21 deletions tests/MagicOnion.Server.Tests/MagicOnionEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void CollectFromTypes_Empty()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{};
var options = new MagicOnionOptions();
Expand All @@ -32,7 +32,7 @@ public void CollectFromTypes_NonService()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{ typeof(NonService) };
var options = new MagicOnionOptions();
Expand All @@ -50,7 +50,7 @@ public void CollectFromTypes_Service()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{ typeof(MyService) };
var options = new MagicOnionOptions();
Expand All @@ -68,8 +68,7 @@ public void CollectFromTypes_StreamingHub()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{ typeof(MyHub) };
var options = new MagicOnionOptions();
Expand All @@ -87,8 +86,7 @@ public void CollectFromAssembly()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -106,8 +104,7 @@ public void CollectFromAssembly_Ignore()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyIgnoredService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -125,8 +122,7 @@ public void CollectFromAssembly_NonPublic()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyNonPublicService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -144,8 +140,7 @@ public void CollectFromAssembly_Abstract()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyAbstractService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -163,8 +158,7 @@ public void CollectFromAssembly_Generic_Constructed()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyGenericsService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -182,8 +176,7 @@ public void CollectFromAssembly_Generic_Definitions()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var assemblies = new Assembly[]{ typeof(IMyGenericsService).Assembly };
var options = new MagicOnionOptions();
Expand All @@ -203,8 +196,7 @@ public void CollectHandlers_Duplicated_Service()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{ typeof(MyService), typeof(MyService) };
var options = new MagicOnionOptions();
Expand All @@ -222,8 +214,7 @@ public void CollectHandlers_Duplicated_Hub()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IGroupRepositoryFactory, ConcurrentDictionaryGroupRepositoryFactory>();
services.AddMagicOnionCore();
var serviceProvider = services.BuildServiceProvider();
var types = new Type[]{ typeof(MyHub), typeof(MyHub) };
var options = new MagicOnionOptions();
Expand Down

0 comments on commit 13fbd09

Please sign in to comment.