Rpc Call Proxy

mybwu_com發表於2014-03-11

When communicate with Another System , for some reason need to :


1. log the time (calling time , etc)

2.Exception handling


This is a RPC caller which handles exception in each call


/// <summary>
    /// Remote Procedure Call Proxy 
    /// Add log(timeline,etc) before/after RPC 
    /// Handle Remoting Call Exception 
    /// </summary>
    /// <typeparam name="TIn"></typeparam>
    public class EdasRpcProxy<TIn>
    {
        public delegate void ActionRef<TIn>(ref TIn parameter);

        public static EdasResult SyncCallRef(ActionRef<TIn> func, ref TIn parameter)
        {
            return new EdasRpcProxy<TIn>().SynCRef(func, ref parameter);
        }
        public static EdasResult SyncCall(Action<TIn> action, TIn parameter)
        {
            return new EdasRpcProxy<TIn>().SynC(action, parameter);
        }

        private EdasResult SynCRef(ActionRef<TIn> func, ref TIn parameter)
        {
            try
            {
                func(ref parameter);
                return EdasResult.Create(ResultCode.Ok);
            }
            catch (Exception ex)
            {
                return EdasResult.Create(ResultCode.RemoteServerErr,
                    string.Format(eDASConstants.Messages.RpcCallError, func.Method.Name));
            }
        }

        private EdasResult SynC(Action<TIn> action, TIn parameter)
        {
            try
            {
                action(parameter);
                return EdasResult.Create(ResultCode.Ok);
            }
            catch (Exception ex)
            {
                return EdasResult.Create(ResultCode.RemoteServerErr,
                    string.Format(eDASConstants.Messages.RpcCallError, action.Method.Name));
            }
        }

        public void AsyncC<TOut>(Func<object, TOut> func, object parameter,
      Action<EdasResult<TOut>> callback)
        {
            Task<TOut>.Factory.StartNew(func, parameter).ContinueWith(
                (t =>
                {
                    var entity = t.Result;

                    if (t.Exception != null)
                        callback(EdasResult<TOut>.CreateResult(ResultCode.RemoteServerErr, default(TOut),
                                                                 string.Format

(eDASConstants.Messages.RpcCallError,
                                                                 func.Method.Name)));
                    else
                        callback(EdasResult<TOut>.CreateResult(ResultCode.Ok, entity));
                }));

        }

    }

    public class EdasRpcProxy<TIn, TOut>
    {
        public delegate TOut FuncRef<TIn, TOut>(ref TIn parameter);

        public static EdasResult<TOut> SyncCallRef(FuncRef<TIn, TOut> func, ref TIn parameter)
        {
            return new EdasRpcProxy<TIn, TOut>().SynCRef(func, ref parameter);
        }

        public static EdasResult<TOut> SyncCall(Func<TIn, TOut> func, TIn parameter)
        {
            return new EdasRpcProxy<TIn, TOut>().SynC(func, parameter);
        }

        private EdasResult<TOut> SynCRef(FuncRef<TIn, TOut> func, ref TIn parameter)
        {
            try
            {
                var ret = func(ref parameter);
                return EdasResult<TOut>.CreateResult(ResultCode.Ok, ret);
            }
            catch (Exception ex)
            {
                return EdasResult<TOut>.CreateResult(ResultCode.RemoteServerErr, default(TOut),
                    string.Format(eDASConstants.Messages.RpcCallError, func.Method.Name));
            }
        }

        private EdasResult<TOut> SynC(Func<TIn, TOut> func, TIn parameter)
        {
            try
            {
                var ret = func(parameter);
                return EdasResult<TOut>.CreateResult(ResultCode.Ok, ret);
            }
            catch (Exception ex)
            {
                return EdasResult<TOut>.CreateResult(ResultCode.RemoteServerErr, default(TOut),
                    string.Format(eDASConstants.Messages.RpcCallError, func.Method.Name));
            }
        }



    }