Skip to content

Commit

Permalink
Merge pull request #711 from Cysharp/hotfix/FixAssemblyNameCheck
Browse files Browse the repository at this point in the history
Fix check for ignored assembly names
  • Loading branch information
mayuki authored Oct 31, 2023
2 parents 61c9be2 + 341a4f0 commit 75f3916
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 46 deletions.
95 changes: 49 additions & 46 deletions src/MagicOnion.Server/MagicOnionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@ public static class MagicOnionEngine
const string LoggerNameMagicOnionEngine = "MagicOnion.Server.MagicOnionEngine";
const string LoggerNameMethodHandler = "MagicOnion.Server.MethodHandler";

static readonly string[] wellKnownIgnoreAssemblies = new[]
{
"netstandard",
"mscorlib",
"Microsoft.AspNetCore.*",
"Microsoft.CSharp.*",
"Microsoft.CodeAnalysis.*",
"Microsoft.Extensions.*",
"Microsoft.Win32.*",
"NuGet.*",
"System.*",
"Newtonsoft.Json",
"Microsoft.Identity.*",
"Microsoft.IdentityModel.*",
"StackExchange.Redis.*",
// gRPC
"Grpc.*",
// WPF
"Accessibility",
"PresentationFramework",
"PresentationCore",
"WindowsBase",
// MessagePack, MemoryPack
"MessagePack.*",
"MemoryPack.*",
// MagicOnion
"MagicOnion.Server.*",
"MagicOnion.Client.*", // MagicOnion.Client.DynamicClient (MagicOnionClient.Create<T>)
"MagicOnion.Abstractions",
"MagicOnion.Shared",
};

/// <summary>
/// Search MagicOnion service from all assemblies.
/// </summary>
Expand All @@ -33,53 +65,8 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IServiceP
public static MagicOnionServiceDefinition BuildServerServiceDefinition(IServiceProvider serviceProvider, MagicOnionOptions options)
{
// NOTE: Exclude well-known system assemblies from automatic discovery of services.
var wellKnownIgnoreAssemblies = new[]
{
"netstandard",
"mscorlib",
"Microsoft.AspNetCore.*",
"Microsoft.CSharp.*",
"Microsoft.CodeAnalysis.*",
"Microsoft.Extensions.*",
"Microsoft.Win32.*",
"NuGet.*",
"System.*",
"Newtonsoft.Json",
"Microsoft.Identity.*",
"Microsoft.IdentityModel.*",
"StackExchange.Redis.*",
// gRPC
"Grpc.*",
// WPF
"Accessibility",
"PresentationFramework",
"PresentationCore",
"WindowsBase",
// MessagePack, MemoryPack
"MessagePack.*",
"MemoryPack.*",
// MagicOnion
"MagicOnion.Server.*",
"MagicOnion.Client.*", // MagicOnion.Client.DynamicClient (MagicOnionClient.Create<T>)
"MagicOnion.Abstractions",
"MagicOnion.Shared",
};

var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(x =>
{
return !wellKnownIgnoreAssemblies.Any(y =>
{
if (y.EndsWith(".*"))
{
return x.GetName().Name!.StartsWith(y.Substring(0, y.Length - 2));
}
else
{
return x.GetName().Name == y;
}
});
})
.Where(x => !ShouldIgnoreAssembly(x.GetName().Name!))
.ToArray();

return BuildServerServiceDefinition(serviceProvider, assemblies, options);
Expand Down Expand Up @@ -264,4 +251,20 @@ internal static void VerifyServiceType(Type type)
throw new InvalidOperationException($"Type '{type.FullName}' is generic type definition. A service type must be plain or constructed-generic class.");
}
}

internal static bool ShouldIgnoreAssembly(string name)
{
return wellKnownIgnoreAssemblies.Any(y =>
{
if (y.EndsWith(".*"))
{
return name.StartsWith(y.Substring(0, y.Length - 1)) || // Starts with 'MagicOnion.Client.'
name == y.Substring(0, y.Length - 2); // Exact match 'MagicOnion.Client' (w/o last dot)
}
else
{
return name == y;
}
});
}
}
19 changes: 19 additions & 0 deletions tests/MagicOnion.Server.Tests/MagicOnionEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,23 @@ class GenericService<T> : ServiceBase<IMyService>, IMyService
public UnaryResult<Nil> MethodA() => default;
public UnaryResult<Nil> MethodB() => default;
}

[Fact]
public void ShouldIgnoreAssembly()
{
MagicOnionEngine.ShouldIgnoreAssembly("mscorlib").Should().BeTrue();
MagicOnionEngine.ShouldIgnoreAssembly("System.Private.CoreLib").Should().BeTrue();
MagicOnionEngine.ShouldIgnoreAssembly("Grpc.Net.Client").Should().BeTrue();
MagicOnionEngine.ShouldIgnoreAssembly("MagicOnion.Client").Should().BeTrue();
MagicOnionEngine.ShouldIgnoreAssembly("MagicOnion.Client._DynamicClient_").Should().BeTrue();
MagicOnionEngine.ShouldIgnoreAssembly("MagicOnion.Server").Should().BeTrue();

MagicOnionEngine.ShouldIgnoreAssembly("").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("A").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("AspNetCoreSample").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("GrpcSample").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("MyGrpc.Net").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("MyApp.System.Net").Should().BeFalse();
MagicOnionEngine.ShouldIgnoreAssembly("MagicOnionSample").Should().BeFalse();
}
}

0 comments on commit 75f3916

Please sign in to comment.