In my Program.cs:
ScanAndMapGrpcServices(app);
static void ScanAndMapGrpcServices(WebApplication app)
{
var currentAssembly = Assembly.GetExecutingAssembly();
var allTypes = currentAssembly.GetTypes().Concat(
currentAssembly
.GetReferencedAssemblies()
.SelectMany(assemblyName => Assembly.Load(assemblyName).GetTypes()))
.Where(type => type.Namespace == "<my namespace>"
&& !type.IsInterface
&& !type.IsAbstract);
Console.WriteLine(allTypes.Count());
foreach (var type in allTypes)
{
var method = typeof(GrpcEndpointRouteBuilderExtensions).GetMethod("MapGrpcService")?.MakeGenericMethod([type]);
object? invokedBuilder = method?.Invoke(app, [app]);
if (invokedBuilder is GrpcServiceEndpointConventionBuilder builder)
{
_ = builder.RequireCors();
}
}
}
When running on Release mode, I got this error
throw new ArgumentException(SR.Format(SR.Argument_GenConstraintViolation, i.ToString(), type, definition, type2), e);
But in debug mode it’s totally fine.
At first glance, I thought my assemblies are trimmed but then I print Console.WriteLine(allTypes.Count());
, both Debug mode and Release mode returns 22.