Exemplo n.º 1
0
        /// <summary>
        /// Log a Message
        /// </summary>
        /// <param name="msg"></param>

        public static void Message(
            string msg,
            string logFileName = null)
        {
            if (logFileName == null)
            {
                logFileName = LogFileName;
            }

            if (IDebugLog != null)
            {
                IDebugLog.Message(msg, logFileName);                 // redirect to other object
                return;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Call native method & process any exception
        /// </summary>
        /// <param name="nativeClient"></param>
        /// <param name="serviceCode"></param>
        /// <param name="opCode"></param>
        /// <param name="argObject"></param>
        /// <returns></returns>

        public NativeMethodTransportObject InvokeNativeMethod(
            INativeSession nativeClient,
            int serviceCodeInt,
            int opCodeInt,
            NativeMethodTransportObject transportObject)
        {
            NativeMethodTransportObject resultObj = null;
            string servicesExceptionHeader        = "*** InvokeNativeMethod Exception ***\r\n";      // this exception header is added by the services upon an exception
            bool   isException = false;

            int    t0 = 0;
            string classAndMethod = "", msg = "", exceptionType = "";

            if (LogServiceCalls)
            {
                t0             = ComOps.TimeOfDay.Milliseconds();
                classAndMethod = DebugLog.GetCallingClassAndMethodPrefix();
                if (IDebugLog != null)
                {
                    IDebugLog.Message("[ServiceCallBegin]" + classAndMethod + "Begin");
                }
            }

            DateTime callStart = DateTime.Now;

            if (ServiceCallCount == 0)
            {
                FirstServiceCallTime = DateTime.Now;
            }
            LastServiceCallTime = DateTime.Now;

            NativeMethodCaller nmc = new NativeMethodCaller();

            InServiceCall = true;
            nmc.InvokeNativeMethod(nativeClient, serviceCodeInt, opCodeInt, transportObject);
            resultObj     = nmc.InvokeNativeMethodResult;         // any result returned
            InServiceCall = false;

            ServiceCallCount++;             // count it as complete

            // Regular exception thown on the client?

            if (nmc.InvokeNativeMethodException != null)
            {
                isException = true;

                msg = DebugLog.FormatExceptionMessage(nmc.InvokeNativeMethodException);

                // Check if link to services no longer exists (e.g. Server process killed or Mobius server restarted)

                if (Lex.Contains(msg, "There was no endpoint listening") ||
                    Lex.Contains(msg, "Count not connect") ||
                    Lex.Contains(msg, "A connection attempt failed"))                           // && ServiceCallCount > 1)
                {
                    msg =
                        @"The connection to Mobius services has been lost. 
Session start time: {0:G}, Service Calls: {1}, Last service call: {2:G})

Do you want to restart the Mobius client?";

                    msg = String.Format(msg, FirstServiceCallTime, ServiceCallCount, LastServiceCallTime);

                    DebugLog.Message(msg);
                    DialogResult dr = MessageBox.Show(msg, "No connection to Mobius services", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        Process p = Process.Start(Application.ExecutablePath);
                    }
                    Environment.Exit(-1);
                }
            }

            // Check if exception from services by looking at header in string passed back from services

            else if
            (resultObj != null && resultObj.Value is string && (resultObj.Value as string).StartsWith(servicesExceptionHeader))
            {
                isException = true;

                msg = (resultObj.Value as string).Substring(servicesExceptionHeader.Length);
                int i1 = msg.IndexOf("\n");
                exceptionType = msg.Substring(0, i1 - 1);
                msg           = msg.Substring(i1 + 1);
            }

            if (!isException)
            {
                if (LogServiceCalls)
                {
                    t0 = ComOps.TimeOfDay.Milliseconds() - t0;

                    if (IDebugLog != null)
                    {
                        IDebugLog.Message("[ServiceCallEnd]" + classAndMethod +
                                          " Time: " + t0 + " ms");
                    }
                }
                return(resultObj);                // normal return value
            }

            else             // the call threw an exception
            {
                bool userQueryException = Lex.Eq(exceptionType, "UserQueryException");
                bool queryException     = Lex.Eq(exceptionType, "QueryException");

                if (IDebugLog != null && !userQueryException)
                {
                    IDebugLog.Message(msg);                                                           // log any exceptions except user query exception
                }
                if (userQueryException)
                {
                    throw new UserQueryException(msg);
                }

                else if (queryException)
                {
                    throw new QueryException(msg);
                }

                else
                {
                    throw new Exception(msg);
                }
            }
        }