/// <summary>
        /// Builds a new method call message.
        /// </summary>
        /// <param name="serializer">Serializer adapter used to serialize argument values</param>
        /// <param name="remoteServiceName">Unique name of the remote service that should be called</param>
        /// <param name="targetMethod">Target method information</param>
        /// <param name="args">Array of arguments, which should passed a parameters</param>
        /// <returns>The created method call message</returns>
        public MethodCallMessage BuildMethodCallMessage(
            ISerializerAdapter serializer,
            string remoteServiceName,
            MethodInfo targetMethod,
            object[] args)
        {
            if (targetMethod == null)
            {
                throw new ArgumentNullException(nameof(targetMethod));
            }

            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            args ??= new object[0];

            var message = new MethodCallMessage()
            {
                ServiceName         = remoteServiceName,
                MethodName          = targetMethod.Name,
                Parameters          = BuildMethodParameterInfos(serializer, targetMethod, args).ToArray(),
                CallContextSnapshot = CallContext.GetSnapshot()
            };

            return(message);
        }
Exemple #2
0
        /// <summary>
        /// Unwraps parameter values and parameter types from a deserialized MethodCallMessage.
        /// </summary>
        /// <param name="callMessage">MethodCallMessage object</param>
        /// <param name="parameterValues">Out: Unwrapped parameter values</param>
        /// <param name="parameterTypes">Out: Unwrapped parameter types</param>
        public static void UnwrapParametersFromDeserializedMethodCallMessage(
            this MethodCallMessage callMessage,
            out object[] parameterValues,
            out Type[] parameterTypes)
        {
            parameterTypes  = new Type[callMessage.Parameters.Length];
            parameterValues = new object[callMessage.Parameters.Length];

            for (int i = 0; i < callMessage.Parameters.Length; i++)
            {
                var parameter     = callMessage.Parameters[i];
                var parameterType = Type.GetType(parameter.ParameterTypeName);
                parameterTypes[i] = parameterType;

                parameterValues[i] =
                    parameter.IsValueNull
                        ? null
                        : parameter.Value is Envelope envelope
                            ? envelope.Value == null
                                ? null
                                : Convert.ChangeType(envelope.Value, envelope.Type)
                            : parameter.Value;
            }
        }