public static bool IsRpc(RouteMetadata route) { var returnType = route.Method.ReturnType; if (returnType == typeof(Task)) { return(false); } if (returnType.IsConstructedGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>)) { return(true); } throw new InvalidOperationException("Invalid return type"); }
public static BusHandler BuildBusHandler(object plugin, RouteMetadata route) { var method = route.Method; if (method.ReturnType != typeof(Task)) { throw new InvalidOperationException("Return type of Bus method should be Task"); } var builder = new RouteHandlerBuilder(); foreach (var param in method.GetParameters()) { builder.AddArgument(param); } return(builder.Build <BusHandler>(plugin, method)); }
public static RpcHandler BuildRpcHandler(object plugin, RouteMetadata route) { var method = route.Method; if (!method.ReturnType.IsConstructedGenericType || method.ReturnType.GetGenericTypeDefinition() != typeof(Task <>)) { throw new InvalidOperationException("Return type of RPC method should be Task<T>"); } var builder = new RouteHandlerBuilder(); foreach (var param in method.GetParameters()) { builder.AddArgument(param); } var returnType = method.ReturnType.GetGenericArguments()[0]; builder.SetReturnType(returnType); return(builder.Build <RpcHandler>(plugin, method)); }
private static async Task RegisterRouteAsync(IRoutingRegistration model, object plugin, RouteMetadata route) { var pattern = route.Route; if (RouteHandlerFactory.IsRpc(route)) { var handler = RouteHandlerFactory.BuildRpcHandler(plugin, route); await model.RegisterRpcHandlerAsync(pattern, new Func <Message, CancellationToken, Task <Message> >(handler)); } else { var handler = RouteHandlerFactory.BuildBusHandler(plugin, route); await model.RegisterBusHandlerAsync(pattern, new Func <Message, CancellationToken, Task>(handler)); } }