// <Snippet2>
        public override IMessage Invoke(IMessage myMessage)
        {
            IMethodCallMessage myCallMessage = (IMethodCallMessage)myMessage;

            IMethodReturnMessage myIMethodReturnMessage =
                RemotingServices.ExecuteMessage(myMarshalByRefObject, myCallMessage);

            Console.WriteLine("Method name : " + myIMethodReturnMessage.MethodName);
            Console.WriteLine("The return value is : " + myIMethodReturnMessage.ReturnValue);

            // Get number of 'ref' and 'out' parameters.
            int myArgOutCount = myIMethodReturnMessage.OutArgCount;

            Console.WriteLine("The number of 'ref', 'out' parameters are : " +
                              myIMethodReturnMessage.OutArgCount);
            // Gets name and values of 'ref' and 'out' parameters.
            for (int i = 0; i < myArgOutCount; i++)
            {
                Console.WriteLine("Name of argument {0} is '{1}'.",
                                  i, myIMethodReturnMessage.GetOutArgName(i));
                Console.WriteLine("Value of argument {0} is '{1}'.",
                                  i, myIMethodReturnMessage.GetOutArg(i));
            }
            Console.WriteLine();
            object[] myObjectArray = myIMethodReturnMessage.OutArgs;
            for (int i = 0; i < myObjectArray.Length; i++)
            {
                Console.WriteLine("Value of argument {0} is '{1}' in OutArgs",
                                  i, myObjectArray[i]);
            }
            return(myIMethodReturnMessage);
        }
    public override IMessage Invoke(IMessage request)
    {
        IMethodCallMessage call = (IMethodCallMessage)request;

        Console.WriteLine("Invoke " + call.MethodName);

        Console.Write("ARGS(");
        for (int i = 0; i < call.ArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(call.GetArgName(i) + " " +
                          call.GetArg(i));
        }
        Console.WriteLine(")");
        Console.Write("INARGS(");
        for (int i = 0; i < call.InArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(call.GetInArgName(i) + " " +
                          call.GetInArg(i));
        }
        Console.WriteLine(")");

        ((R1)target).test_field = 1;

        IMethodReturnMessage res = RemotingServices.ExecuteMessage(target, call);

        Console.Write("RESARGS(");
        for (int i = 0; i < res.ArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(res.GetArgName(i) + " " +
                          res.GetArg(i));
        }
        Console.WriteLine(")");

        Console.Write("RESOUTARGS(");
        for (int i = 0; i < res.OutArgCount; i++)
        {
            if (i != 0)
            {
                Console.Write(", ");
            }
            Console.Write(res.GetOutArgName(i) + " " +
                          res.GetOutArg(i));
        }
        Console.WriteLine(")");

        return(res);
    }
Example #3
0
 protected void PostProcess(IMethodCallMessage callMsg, ref IMethodReturnMessage retMsg)
 {
     Trace.WriteLine(String.Format("TracePostProcessor {0} Return:{1}", retMsg.MethodName, retMsg.ReturnValue));
     if (retMsg.OutArgCount > 0) Trace.WriteLine(String.Format("Out Argument Count {0}",retMsg.OutArgCount));
     for (int idx = 0; idx < retMsg.OutArgCount; idx++)
     {
         Trace.WriteLine(String.Format("{0}={1}", retMsg.GetOutArgName(idx), retMsg.GetOutArg(idx)));
     }
 }
Example #4
0
 public static string FormatReturnMessage(IMethodCallMessage callMsg,  IMethodReturnMessage retMsg)
 {
     string message = String.Format("Trace {0}.{1} Return:{2}", retMsg.MethodBase.DeclaringType.FullName, retMsg.MethodName, retMsg.ReturnValue) + System.Environment.NewLine;
     if (retMsg.OutArgCount > 0)
     {
         message += String.Format("Out Argument Count {0}", retMsg.OutArgCount) + System.Environment.NewLine;
         for (int idx = 0; idx < retMsg.OutArgCount; idx++)
         {
             message += String.Format("{0} = {1}", retMsg.GetOutArgName(idx), retMsg.GetOutArg(idx)) + System.Environment.NewLine;
         }
     }
     return message;
 }
Example #5
0
        private void PostProcess(IMessage msg, IMessage msgReturn)
        {
            // 处理返回方法调用时的接口
            if (!(msg is IMethodMessage) ||
                !(msgReturn is IMethodReturnMessage))
            {
                return;
            }
            if (!FilterMethodName(msg))
            {
                return;
            }
            IMethodReturnMessage retMsg = (IMethodReturnMessage)msgReturn;
            string    logText           = "PostProcessing: ";
            Exception e = retMsg.Exception;

            if (e != null)
            {
                logText += "Exception was thrown: " + e;
                Logger.Log.Error(logText);
                return;
            }

            // 遍历输出参数
            logText += mTypeAndName + "(";
            if (retMsg.OutArgCount > 0)
            {
                logText += "out parameters[";
                for (int i = 0; i < retMsg.OutArgCount; ++i)
                {
                    if (i > 0)
                    {
                        logText += ", ";
                    }
                    logText += retMsg.GetOutArgName(i) + " = " +
                               retMsg.GetOutArg(i);
                }
                logText += "]";
            }
            if (retMsg.ReturnValue.GetType() != typeof(void))
            {
                logText += " returned [" + retMsg.ReturnValue + "]";
            }
            logText += " )";
            Logger.Log.Info(logText);
        }
Example #6
0
        private void PostProcess(IMessage msg, IMessage msgReturn)
        {
            // We only want to process method return calls
            if (!(msg is IMethodMessage) ||
                !(msgReturn is IMethodReturnMessage))
            {
                return;
            }

            IMethodReturnMessage retMsg = (IMethodReturnMessage)msgReturn;

            Console.Write("PostProcessing: ");
            Exception e = retMsg.Exception;

            if (e != null)
            {
                Console.WriteLine("Exception was thrown: " + e);
                return;
            }

            // Loop through all the [out] parameters
            Console.Write(m_typeAndName + "(");
            if (retMsg.OutArgCount > 0)
            {
                Console.Write("out parameters[");
                for (int i = 0; i < retMsg.OutArgCount; ++i)
                {
                    if (i > 0)
                    {
                        Console.Write(", ");
                    }
                    Console.Write(retMsg.GetOutArgName(i) + " = " +
                                  retMsg.GetOutArg(i));
                }
                Console.Write("]");
            }
            if (retMsg.ReturnValue.GetType() != typeof(void))
            {
                Console.Write(" returned [" + retMsg.ReturnValue + "]");
            }

            Console.WriteLine(")\n");
        }
        // used by the server
        internal object BuildSoapMessageFromMethodResponse(IMethodReturnMessage mrm, out ITransportHeaders responseHeaders)
        {
            responseHeaders = new TransportHeaders();

            if (mrm.Exception == null)
            {
                // *normal* function return

                SoapMessage soapMessage = new SoapMessage();

                // fill the transport headers
                responseHeaders["Content-Type"] = "text/xml; charset=\"utf-8\"";

                // build the SoapMessage
                ArrayList paramNames  = new ArrayList();
                ArrayList paramValues = new ArrayList();
                ArrayList paramTypes  = new ArrayList();
                soapMessage.MethodName = mrm.MethodName + "Response";

                Type retType = ((MethodInfo)mrm.MethodBase).ReturnType;

                if (retType != typeof(void))
                {
                    paramNames.Add("return");
                    paramValues.Add(mrm.ReturnValue);
                    if (mrm.ReturnValue != null)
                    {
                        paramTypes.Add(mrm.ReturnValue.GetType());
                    }
                    else
                    {
                        paramTypes.Add(retType);
                    }
                }

                for (int i = 0; i < mrm.OutArgCount; i++)
                {
                    paramNames.Add(mrm.GetOutArgName(i));
                    paramValues.Add(mrm.GetOutArg(i));
                    if (mrm.GetOutArg(i) != null)
                    {
                        paramTypes.Add(mrm.GetOutArg(i).GetType());
                    }
                }
                soapMessage.ParamNames   = (string[])paramNames.ToArray(typeof(string));
                soapMessage.ParamValues  = (object[])paramValues.ToArray(typeof(object));
                soapMessage.ParamTypes   = (Type[])paramTypes.ToArray(typeof(Type));
                soapMessage.XmlNameSpace = _xmlNamespace;
                soapMessage.Headers      = BuildMessageHeaders(mrm);
                return(soapMessage);
            }
            else
            {
                // an Exception was thrown while executing the function
                responseHeaders["__HttpStatusCode"]   = "500";
                responseHeaders["__HttpReasonPhrase"] = "Bad Request";
                // fill the transport headers
                responseHeaders["Content-Type"] = "text/xml; charset=\"utf-8\"";
                ServerFault serverFault = CreateServerFault(mrm.Exception);
                return(new SoapFault("Server", String.Format(" **** {0} - {1}", mrm.Exception.GetType().ToString(), mrm.Exception.Message), null, serverFault));
            }
        }
Example #8
0
 public virtual String GetOutArgName(int index)
 {
     return(mrm.GetOutArgName(index));
 }
Example #9
0
        public override IMessage Invoke(IMessage myIMessage)
        {
            Console.WriteLine("**********************************************************************************");

            IDictionary myIDictionary = myIMessage.Properties;

            // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
            myIDictionary["__Uri"] = stringUri;
            IDictionaryEnumerator myIDictionaryEnumerator = (IDictionaryEnumerator)myIDictionary.GetEnumerator();

            while (myIDictionaryEnumerator.MoveNext())
            {
                Object myKey     = myIDictionaryEnumerator.Key;
                String myKeyName = myKey.ToString();
                Object myValue   = myIDictionaryEnumerator.Value;

                Console.WriteLine("\t{0} : {1}", myKeyName, myIDictionaryEnumerator.Value);
                if (myKeyName == "__Args")
                {
                    Object[] objs = (Object[])myValue;
                    for (int aIndex = 0; aIndex < objs.Length; aIndex++)
                    {
                        Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, objs[aIndex]);
                    }
                }

                if ((myKeyName == "__MethodSignature") && (null != myValue))
                {
                    Object[] objs = (Object[])myValue;
                    for (int aIndex = 0; aIndex < objs.Length; aIndex++)
                    {
                        Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, objs[aIndex]);
                    }
                }
            }

            Console.WriteLine("**********************************************************************************");

            Console.WriteLine("ChannelServices.SyncDispatchMessage");
            IMessage myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);

            // Push return value and OUT parameters back onto stack.
            IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)myReturnMessage;

            Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}", myMethodReturnMessage.ReturnValue);

            Console.WriteLine("**********************************************************************************");
            IMethodCallMessage   myIMethodCallMessage   = (IMethodCallMessage)myIMessage;
            IMethodReturnMessage myIMethodReturnMessage = RemotingServices.ExecuteMessage(myMarshalByRefObject, myIMethodCallMessage);

            Console.WriteLine("Method name : " + myIMethodReturnMessage.MethodName);
            Console.WriteLine("The return value is : " + myIMethodReturnMessage.ReturnValue);

            Console.WriteLine("**********************************************************************************");

            int myArgOutCount = myIMethodReturnMessage.OutArgCount;

            Console.WriteLine("The number of 'ref', 'out' parameters are : " + myIMethodReturnMessage.OutArgCount);
            // Gets name and values of 'ref' and 'out' parameters.
            for (int i = 0; i < myArgOutCount; i++)
            {
                Console.WriteLine("Name of argument {0} is '{1}'.", i, myIMethodReturnMessage.GetOutArgName(i));
                Console.WriteLine("Value of argument {0} is '{1}'.", i, myIMethodReturnMessage.GetOutArg(i));
            }
            Console.WriteLine();
            object[] myObjectArray = myIMethodReturnMessage.OutArgs;
            for (int i = 0; i < myObjectArray.Length; i++)
            {
                Console.WriteLine("Value of argument {0} is '{1}' in OutArgs", i, myObjectArray[i]);
            }

            Console.WriteLine("**********************************************************************************");
            Console.WriteLine("Message is of type 'IMethodCallMessage'.");
            Console.WriteLine("InArgCount is  : " + myIMethodCallMessage.InArgCount);
            foreach (object arg in myIMethodCallMessage.InArgs)
            {
                Console.WriteLine("InArgs is : " + arg);
            }
            for (int i = 0; i < myIMethodCallMessage.InArgCount; i++)
            {
                Console.WriteLine("GetArgName(" + i + ") is : " + myIMethodCallMessage.GetArgName(i));
                Console.WriteLine("GetInArg(" + i + ") is : " + myIMethodCallMessage.GetInArg(i));
            }
            Console.WriteLine("**********************************************************************************");
            var message = new ReturnMessage(5, null, 0, null, (IMethodCallMessage)myIMessage);

            return(message);
        }
		// used by the server
		internal object BuildSoapMessageFromMethodResponse(IMethodReturnMessage mrm, out ITransportHeaders responseHeaders)
		{
			responseHeaders = new TransportHeaders();

			if(mrm.Exception == null) {
				// *normal* function return
				
				SoapMessage soapMessage = new SoapMessage();
				
				// fill the transport headers
				responseHeaders["Content-Type"] = "text/xml; charset=\"utf-8\"";

				// build the SoapMessage
				ArrayList paramNames = new ArrayList();
				ArrayList paramValues = new ArrayList();
				ArrayList paramTypes = new ArrayList();
				soapMessage.MethodName = mrm.MethodName+"Response";
				
				Type retType = ((MethodInfo)mrm.MethodBase).ReturnType;
				
				if(retType != typeof(void)) {
					paramNames.Add("return");
					paramValues.Add(mrm.ReturnValue);
					if (mrm.ReturnValue != null)
						paramTypes.Add(mrm.ReturnValue.GetType());
					else
						paramTypes.Add(retType);
				}
				
				for(int i = 0; i < mrm.OutArgCount; i++){
					paramNames.Add(mrm.GetOutArgName(i));
					paramValues.Add(mrm.GetOutArg(i));
					if(mrm.GetOutArg(i) != null) paramTypes.Add(mrm.GetOutArg(i).GetType());
				}
				soapMessage.ParamNames = (string[]) paramNames.ToArray(typeof(string));
				soapMessage.ParamValues = (object[]) paramValues.ToArray(typeof(object));
				soapMessage.ParamTypes = (Type[]) paramTypes.ToArray(typeof(Type));
				soapMessage.XmlNameSpace = _xmlNamespace;
				soapMessage.Headers = BuildMessageHeaders (mrm);
				return soapMessage;
			}
			else {
				// an Exception was thrown while executing the function
				responseHeaders["__HttpStatusCode"] = "500";
				responseHeaders["__HttpReasonPhrase"] = "Bad Request";
				// fill the transport headers
				responseHeaders["Content-Type"] = "text/xml; charset=\"utf-8\"";
				ServerFault serverFault = CreateServerFault(mrm.Exception);
				return new SoapFault("Server", String.Format(" **** {0} - {1}", mrm.Exception.GetType().ToString(), mrm.Exception.Message), null, serverFault);
			}
		}
Example #11
0
        /// <summary>
        /// Provide trace output for the method return.  It is at this point that
        /// exceptions and return values will be logged (if available), and the
        /// elapsed time for the method to execute is calculated.
        /// </summary>
        /// <param name="msg">The incoming method call</param>
        /// <param name="returnMsg">The method return</param>
        private void TraceMethodReturn(IMessage msg, IMessage returnMsg)
        {
            IMethodReturnMessage methodReturn = returnMsg as IMethodReturnMessage;

            // Handle method returns only
            if ((methodReturn == null) || !(msg is IMethodMessage))
            {
                return;
            }

            // If no tracing is performed, exit.
            if (_traceLevel == TraceLevel.Off)
            {
                return;
            }

            // If the TraceLevel is set to "Info" or "Verbose", the method call
            // was logged and the indent increased.  Decrease it now.
            if (((int)_traceLevel) >= ((int)TraceLevel.Info))
            {
                Trace.Unindent();
            }

            // Calculate the time spent in the method call.
            TimeSpan elapsedTimeInCall = DateTime.Now - _timeOfMethodCall;

            StringBuilder textOutput = new StringBuilder(TRACE_OUTPUT_BUFFER_SIZE);

            // If an exception occurred, log it to the trace log and return.
            Exception e = methodReturn.Exception;

            if (e != null)
            {
                textOutput.Append(Type.GetType(methodReturn.TypeName).Name);
                textOutput.Append(".");
                textOutput.Append(methodReturn.MethodName);
                textOutput.Append("(");
                for (int index = 0; index < methodReturn.ArgCount; index++)
                {
                    if (index > 0)
                    {
                        textOutput.Append(", ");
                    }
                    textOutput.Append(methodReturn.GetArgName(index));
                    textOutput.Append(" = ");
                    textOutput.Append(methodReturn.GetArg(index).ToString());
                }
                textOutput.Append(")\n");
                textOutput.Append("----- EXCEPTION -----\n");
                textOutput.Append(e.ToString());
                textOutput.Append("\n----- EXCEPTION -----");

                Trace.WriteLine(textOutput.ToString(), string.Format(
                                    "Method Return - {0} ms", elapsedTimeInCall.TotalMilliseconds));
                return;
            }

            // If the trace level is set to "Info" or "Verbose", then log the
            // return values from the call as well as the time spent in the call.
            if (((int)_traceLevel) >= ((int)TraceLevel.Info))
            {
                textOutput.Append(Type.GetType(methodReturn.TypeName).Name);
                textOutput.Append(".");
                textOutput.Append(methodReturn.MethodName);
                textOutput.Append("(");
                if (methodReturn.OutArgCount > 0)
                {
                    textOutput.Append("ref/out parameters[");
                    for (int index = 0; index < methodReturn.OutArgCount; index++)
                    {
                        if (index > 0)
                        {
                            textOutput.Append(", ");
                        }
                        textOutput.Append(methodReturn.GetOutArgName(index));
                        textOutput.Append(" = ");
                        textOutput.Append(methodReturn.GetOutArg(index).ToString());
                    }
                    textOutput.Append("]");
                }

                if (methodReturn.ReturnValue.GetType() != typeof(void))
                {
                    if (methodReturn.OutArgCount > 0)
                    {
                        textOutput.Append(", ");
                    }
                    textOutput.Append("returned [");
                    textOutput.Append(methodReturn.ReturnValue.ToString());
                    textOutput.Append("]");
                }
                textOutput.Append(")");

                Trace.WriteLine(textOutput.ToString(), string.Format(
                                    "Method Return - {0} ms", elapsedTimeInCall.TotalMilliseconds));
            }
        }