From 8cf2732ef94758430ba7141344a1134e03068bbe Mon Sep 17 00:00:00 2001 From: Reuben Bond <203839+ReubenBond@users.noreply.github.com> Date: Tue, 31 Jan 2023 17:00:13 -0800 Subject: [PATCH] Fix IGrainFactory.GetGrain where T is a generic interface with no implementations (#8301) --- .../GrainInterfaceTypeToGrainTypeResolver.cs | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs b/src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs index 7a3a0b1501..ffd528f87c 100644 --- a/src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs +++ b/src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs @@ -97,7 +97,21 @@ public GrainType GetGrainType(GrainInterfaceType interfaceType, string prefix) /// public GrainType GetGrainType(GrainInterfaceType interfaceType) { - GrainType result = default; + if (!TryGetGrainType(interfaceType, out var result)) + { + throw new ArgumentException($"Could not find an implementation for interface {interfaceType}"); + } + + return result; + } + + /// + /// Resolves a which implements the provided , returning if an implementation was found; otherwise . + /// + /// if an implementation was found; otherwise . + public bool TryGetGrainType(GrainInterfaceType interfaceType, out GrainType result) + { + result = default; var cache = GetCache(); if (cache.Map.TryGetValue(interfaceType, out var entry)) { @@ -122,34 +136,32 @@ public GrainType GetGrainType(GrainInterfaceType interfaceType) else if (_genericMapping.TryGetValue(interfaceType, out result)) { } - else if (GenericGrainInterfaceType.TryParse(interfaceType, out var genericInterface)) + else if (GenericGrainInterfaceType.TryParse(interfaceType, out var genericInterface) && genericInterface.IsConstructed) { var unconstructedInterface = genericInterface.GetGenericGrainType(); - var unconstructed = GetGrainType(unconstructedInterface.Value); - if (GenericGrainType.TryParse(unconstructed, out var genericGrainType)) + if (TryGetGrainType(unconstructedInterface.Value, out var unconstructed)) { - if (genericGrainType.IsConstructed) + if (GenericGrainType.TryParse(unconstructed, out var genericGrainType)) { - result = genericGrainType.GrainType; + if (genericGrainType.IsConstructed) + { + result = genericGrainType.GrainType; + } + else + { + result = genericGrainType.GrainType.GetConstructed(genericInterface.Value); + } } else { - result = genericGrainType.GrainType.GetConstructed(genericInterface.Value); + result = unconstructed; } } - else - { - result = unconstructed; - } - _genericMapping[interfaceType] = result; - } - if (result.IsDefault) - { - throw new ArgumentException($"Could not find an implementation for interface {interfaceType}"); + _genericMapping[interfaceType] = result; } - return result; + return !result.IsDefault; } ///