Skip to content

Commit

Permalink
Merge pull request #94 from GnicoJP/fsharp-fix
Browse files Browse the repository at this point in the history
Fix MagicOnionEngine.BuildServerServiceDefinition cannot recognize F#'s override Methods(F#'s implementation methods of Interface).
  • Loading branch information
neuecc authored Feb 28, 2019
2 parents 463c3d2 + d6681f0 commit 8c42f75
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
52 changes: 31 additions & 21 deletions src/MagicOnion/Server/MagicOnionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,35 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IEnumerab

var isStreamingHub = typeof(IStreamingHubMarker).IsAssignableFrom(classType);
HashSet<StreamingHubHandler> tempStreamingHubHandlers = null;
MethodHandler tempParentStreamingMethodHandler = null;
if (isStreamingHub)
{
tempStreamingHubHandlers = new HashSet<StreamingHubHandler>();
}

foreach (var methodInfo in classType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
var inheritInterface = classType.GetInterfaces()
.First(x => x.IsGenericType && x.GetGenericTypeDefinition() == (isStreamingHub ? typeof(IStreamingHub<,>) : typeof(IService<>)))
.GenericTypeArguments[0];
var interfaceMap = classType.GetInterfaceMap(inheritInterface);

for (int i = 0; i < interfaceMap.TargetMethods.Length; ++i)
{
var methodInfo = interfaceMap.TargetMethods[i];
var methodName = interfaceMap.InterfaceMethods[i].Name;

if (methodInfo.IsSpecialName && (methodInfo.Name.StartsWith("set_") || methodInfo.Name.StartsWith("get_"))) continue;
if (methodInfo.GetCustomAttribute<IgnoreAttribute>(false) != null) continue; // ignore

var methodName = methodInfo.Name;

// ignore default methods
if (methodName == "Equals"
|| methodName == "GetHashCode"
|| methodName == "GetType"
|| methodName == "ToString"
|| methodName == "WithOptions"
|| methodName == "WithHeaders"
|| methodName == "WithDeadline"
|| methodName == "WithCancellationToken"
|| methodName == "WithHost"
)
|| methodName == "GetHashCode"
|| methodName == "GetType"
|| methodName == "ToString"
|| methodName == "WithOptions"
|| methodName == "WithHeaders"
|| methodName == "WithDeadline"
|| methodName == "WithCancellationToken"
|| methodName == "WithHost"
)
{
continue;
}
Expand All @@ -121,7 +126,7 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IEnumerab
else
{
// create handler
var handler = new MethodHandler(option, classType, methodInfo);
var handler = new MethodHandler(option, classType, methodInfo, methodName);
lock (builder)
{
if (!handlers.Add(handler))
Expand All @@ -130,20 +135,25 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IEnumerab
}
handler.RegisterHandler(builder);
}

if (isStreamingHub && methodName == "Connect")
{
tempParentStreamingMethodHandler = handler;
}
}
}

if (isStreamingHub)
{
var connectHandler = new MethodHandler(option, classType, classType.GetMethod("Connect"), "Connect");
lock (builder)
{
if (!handlers.Add(connectHandler))
{
throw new InvalidOperationException($"Method does not allow overload, {className}.Connect");
}
connectHandler.RegisterHandler(builder);
}

lock (streamingHubHandlers)
{
streamingHubHandlers.AddRange(tempStreamingHubHandlers);
StreamingHubHandlerRepository.RegisterHandler(tempParentStreamingMethodHandler, tempStreamingHubHandlers.ToArray());
StreamingHubHandlerRepository.RegisterHandler(connectHandler, tempStreamingHubHandlers.ToArray());
IGroupRepositoryFactory factory;
var attr = classType.GetCustomAttribute<GroupConfigurationAttribute>(true);
if (attr != null)
Expand All @@ -154,7 +164,7 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IEnumerab
{
factory = option.DefaultGroupRepositoryFactory;
}
StreamingHubHandlerRepository.AddGroupRepository(tempParentStreamingMethodHandler, factory.CreateRepository(option.ServiceLocator));
StreamingHubHandlerRepository.AddGroupRepository(connectHandler, factory.CreateRepository(option.ServiceLocator));
}
}
});
Expand Down
6 changes: 4 additions & 2 deletions src/MagicOnion/Server/MethodHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MethodHandler : IEquatable<MethodHandler>
int methodHandlerId = 0;

public string ServiceName { get; private set; }
public string MethodName { get; private set; }
public Type ServiceType { get; private set; }
public MethodInfo MethodInfo { get; private set; }
public MethodType MethodType { get; private set; }
Expand Down Expand Up @@ -47,13 +48,14 @@ public class MethodHandler : IEquatable<MethodHandler>
static readonly MethodInfo messagePackDeserialize = typeof(LZ4MessagePackSerializer).GetMethods()
.First(x => x.Name == "Deserialize" && x.GetParameters().Length == 2 && x.GetParameters()[0].ParameterType == typeof(byte[]));

public MethodHandler(MagicOnionOptions options, Type classType, MethodInfo methodInfo)
public MethodHandler(MagicOnionOptions options, Type classType, MethodInfo methodInfo, string methodName)
{
this.methodHandlerId = Interlocked.Increment(ref methodHandlerIdBuild);

this.ServiceType = classType;
this.ServiceName = classType.GetInterfaces().First(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == typeof(IService<>)).GetGenericArguments()[0].Name;
this.MethodInfo = methodInfo;
this.MethodName = methodName;
MethodType mt;
this.UnwrappedResponseType = UnwrapResponseType(methodInfo, out mt, out responseIsTask, out this.RequestType);
this.MethodType = mt;
Expand Down Expand Up @@ -292,7 +294,7 @@ Func<ServiceContext, ValueTask> BuildMethodBodyWithFilter(Func<ServiceContext, V

internal void RegisterHandler(ServerServiceDefinition.Builder builder)
{
var method = new Method<byte[], byte[]>(this.MethodType, this.ServiceName, this.MethodInfo.Name, MagicOnionMarshallers.ThroughMarshaller, MagicOnionMarshallers.ThroughMarshaller);
var method = new Method<byte[], byte[]>(this.MethodType, this.ServiceName, this.MethodName, MagicOnionMarshallers.ThroughMarshaller, MagicOnionMarshallers.ThroughMarshaller);

switch (this.MethodType)
{
Expand Down

0 comments on commit 8c42f75

Please sign in to comment.