protected async Task <TResult> MakeRequestAsync <TResult>(params object[] requestParameters) { var targetStack = new System.Diagnostics.StackTrace(1).GetFrames().FirstOrDefault(f => { var t = f.GetMethod().DeclaringType; return(t != typeof(ClientBase) && t.IsInstanceOfType(this)); }); string invokingMethod = targetStack.GetMethod().Name; var request = OperationWrapper.ForRequest(invokingMethod, requestParameters); _lock.Wait(); try { Socket socket = GetSocket(); // Send the client request. await socket.SendWrapperRequestAsync(request); // Now read back the server response. return((await socket.GetWrapperResponseAsync()).GetResultAs <TResult>()); } finally { _lock.Release(); } }
/// <summary> /// Sends a client request across the network socket and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="socket">The TCP client socket.</param> /// <param name="asyncServiceMethod">Projection of the requested asynchronous service method.</param> /// <param name="argument">The argument to send when making performing the service operation.</param> /// <returns>The result of the service operation.</returns> public static async Task <TResult> RequestAsync <TService, TResult>( this Socket socket, string methodName, params object[] arguments) { if (typeof(TService).GetMethod(methodName) == null) { throw new ArgumentException($"Target IWorker method '{methodName}' could not be found.", nameof(methodName)); } await SendWrapperRequestAsync(socket, OperationWrapper.ForRequest(methodName, arguments)).ConfigureAwait(false); return((await GetWrapperResponseAsync(socket).ConfigureAwait(false)).GetResultAs <TResult>()); }
/// <summary> /// Sends a client request across the network socket and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="socket">The TCP client socket.</param> /// <param name="methodName">The name of the method invoked over the service.</param> /// <param name="arguments">The arguments to send when performing the service operation.</param> /// <returns>The result of the service operation.</returns> public static TResult Request <TService, TResult>( this Socket socket, string methodName, params object[] arguments) { if (typeof(TService).GetMethod(methodName) == null) { throw new ArgumentException($"Target IWorker method '{methodName}' could not be found.", nameof(methodName)); } SendWrapperRequest(socket, OperationWrapper.ForRequest(methodName, arguments)); return(GetWrapperResponse(socket).GetResultAs <TResult>()); }
/// <summary> /// Sends a client request across the network socket and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="socket">The TCP client socket.</param> /// <param name="asyncServiceMethod">Projection of the requested asynchronous service method.</param> /// <returns>The result of the service operation.</returns> public static async Task <TResult> RequestAsync <TService, TResult>( this Socket socket, Expression <Func <TService, Func <Task <TResult> > > > asyncServiceMethod) { var targetMethod = (((asyncServiceMethod.Body as UnaryExpression) ?.Operand as MethodCallExpression) ?.Object as ConstantExpression) ?.Value as MethodInfo; if (targetMethod == null) { throw new ArgumentException("Target IWorker method could not be found from worker method expression.", nameof(asyncServiceMethod)); } await SendWrapperRequestAsync(socket, OperationWrapper.ForRequest(targetMethod.Name)).ConfigureAwait(false); return((await GetWrapperResponseAsync(socket).ConfigureAwait(false)).GetResultAs <TResult>()); }
/// <summary> /// Sends a client request across the network socket and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="socket">The TCP client socket.</param> /// <param name="serviceMethod">Projection of the requested service method.</param> /// <returns>The result of the service operation.</returns> public static TResult Request <TService, TResult>( this Socket socket, Expression <Func <TService, Func <TResult> > > serviceMethod) { var targetMethod = (((serviceMethod.Body as UnaryExpression) ?.Operand as MethodCallExpression) ?.Object as ConstantExpression) ?.Value as MethodInfo; if (targetMethod == null) { throw new ArgumentException("Target IWorker method could not be found from worker method expression.", nameof(serviceMethod)); } SendWrapperRequest(socket, OperationWrapper.ForRequest(targetMethod.Name)); return(GetWrapperResponse(socket).GetResultAs <TResult>()); }
/// <summary> /// Sends a client request across the network stream and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TArgument">The type of the argument for the requested service operation.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="stream">The network stream from the TCP client.</param> /// <param name="asyncServiceMethod">Projection of the requested asynchronous service method.</param> /// <param name="argument">The argument to send when making performing the service operation.</param> /// <returns>The result of the service operation.</returns> public static TResult Request <TService, TArgument, TResult>( this NetworkStream stream, Expression <Func <TService, Func <TArgument, Task <TResult> > > > asyncServiceMethod, TArgument argument) { var targetMethod = (((asyncServiceMethod.Body as UnaryExpression) ?.Operand as MethodCallExpression) ?.Object as ConstantExpression) ?.Value as MethodInfo; if (targetMethod == null) { throw new ArgumentException("Target IWorker method could not be found from worker method expression.", nameof(asyncServiceMethod)); } SendWrapperRequest(stream, OperationWrapper.ForRequest(targetMethod.Name, new object[] { argument })); return(GetWrapperResponse(stream).GetResultAs <TResult>()); }
/// <summary> /// Sends a client request across the network stream and retrieves the result. /// </summary> /// <typeparam name="TService">The contract for the service.</typeparam> /// <typeparam name="TArgument">The type of the argument for the requested service operation.</typeparam> /// <typeparam name="TResult">The type of the return value of the requested service operation.</typeparam> /// <param name="stream">The network stream from the TCP client.</param> /// <param name="asyncServiceMethod">Projection of the requested asynchronous service method.</param> /// <param name="argument">The argument to send when making performing the service operation.</param> /// <returns>The result of the service operation.</returns> public static async Task <TResult> RequestAsync <TService, TArgument, TResult>( this NetworkStream stream, Expression <Func <TService, Func <TArgument, Task <TResult> > > > asyncServiceMethod, TArgument argument, CancellationToken cancellationToken = default) { var targetMethod = (((asyncServiceMethod.Body as UnaryExpression) ?.Operand as MethodCallExpression) ?.Object as ConstantExpression) ?.Value as MethodInfo; if (targetMethod == null) { throw new ArgumentException("Target IWorker method could not be found from worker method expression.", nameof(asyncServiceMethod)); } await SendWrapperRequestAsync(stream, OperationWrapper.ForRequest(targetMethod.Name, new object[] { argument }), cancellationToken).ConfigureAwait(false); return((await GetWrapperResponseAsync(stream, cancellationToken).ConfigureAwait(false)).GetResultAs <TResult>()); }