Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IGrainFactory.GetGrain<T> for unimplemented generic interfaces #8301

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs
Expand Up @@ -97,7 +97,21 @@ public GrainType GetGrainType(GrainInterfaceType interfaceType, string prefix)
/// </summary>
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;
}

/// <summary>
/// Resolves a <see cref="GrainType"/> which implements the provided <see cref="GrainInterfaceType"/>, returning <see langword="true"/> if an implementation was found; otherwise <see langword="false"/>.
/// </summary>
/// <returns><see langword="true"/> if an implementation was found; otherwise <see langword="false"/>.</returns>
public bool TryGetGrainType(GrainInterfaceType interfaceType, out GrainType result)
{
result = default;
var cache = GetCache();
if (cache.Map.TryGetValue(interfaceType, out var entry))
{
Expand All @@ -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;
}

/// <summary>
Expand Down