Example #1
0
 protected RpcClient()
 {
     _exceptionTypeResolution = RpcErrorTypeBehavior.OnlyUseLoadedAssemblies;
     _extensions          = ExtensionRegistry.CreateInstance();
     _callContext         = RpcCallContext.CreateBuilder();
     _authenticatedAs     = RpcAuthenticationType.None;
     _serverPrincipalName = null;
 }
        /// <summary>
        ///   Constructs the exception for the client to raise
        /// </summary>
        private Exception CreateException(RpcErrorTypeBehavior typeResolution, bool top)
        {
            Exception baseEx;
            try
            {
                Type tException = GetExceptionType(typeResolution);
                if (tException == null || !typeof (Exception).IsAssignableFrom(tException))
                {
                    tException = typeof (ApplicationException);
                }

                if (!HasMessage)
                {
                    return (Exception) Activator.CreateInstance(tException);
                }

                ConstructorInfo ci;
                SerializationInfo si = new SerializationInfo(tException, FormatterConverter);
                si.AddValue("ClassName", !HasClassName ? null : ClassName);
                si.AddValue("Message", !HasMessage ? null : Message);
                si.AddValue("InnerException",
                            !HasInnerException ? null : InnerException.CreateException(typeResolution, false));
                si.AddValue("HelpURL", !HasHelpUrl ? null : HelpUrl);
                si.AddValue("StackTraceString", !HasStackTraceString ? null : StackTraceString);
                si.AddValue("RemoteStackTraceString",
                            top
                                ? (!HasStackTraceString ? null : StackTraceString)
                                : (!HasRemoteStackTraceString ? null : RemoteStackTraceString));
                si.AddValue("RemoteStackIndex", !HasRemoteStackIndex ? 0 : RemoteStackIndex);
                si.AddValue("ExceptionMethod", !HasExceptionMethod ? null : ExceptionMethod);
                si.AddValue("HResult", !HasHResult ? 0 : HResult);
                si.AddValue("Source", !HasSource ? null : Source);

                Exception ex = (Exception) FormatterServices.GetUninitializedObject(tException);
                try
                {
                    foreach (Types.RpcExceptionData data in ExceptionDataList)
                    {
                        Type t = Type.GetType(data.Type.Split(',')[0], true, false);
                        object value = !data.HasValue
                                           ? null
                                           : t == typeof (byte[])
                                                 ? Convert.FromBase64String(data.Value)
                                                 : Convert.ChangeType(data.Value, t);
                        si.AddValue(data.Member, value, t);
                    }

                    if (HasFullDetails)
                    {
                        ci =
                            tException.GetConstructor(
                                BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Instance, null,
                                CallingConventions.Any, new[] {typeof (SerializationInfo), typeof (StreamingContext)},
                                new ParameterModifier[2]);
                        ci.Invoke(ex, new object[] {si, StreamingContext});
                        return ex;
                    }
                }
                catch
                {
                    ex = (Exception) FormatterServices.GetUninitializedObject(tException);
                }

                ci = tException.GetConstructor(new[] {typeof (string)});
                ci.Invoke(ex, new object[] {Message});
                return ex;
            }
            catch
            {
                baseEx = HasMessage ? new ApplicationException(Message) : new ApplicationException();
            }
            return baseEx;
        }
        private Type GetExceptionType(RpcErrorTypeBehavior typeResolution)
        {
            Type tException = typeof (ApplicationException);
            Assembly found = tException.Assembly;

            if (typeResolution == RpcErrorTypeBehavior.NoTypeResolution)
            {
                return tException;
            }

            if (!HasFullTypeName || !HasAssemblyName || String.IsNullOrEmpty(FullTypeName) ||
                String.IsNullOrEmpty(AssemblyName))
            {
                return tException;
            }

            if (!ValidTypeName.IsMatch(FullTypeName))
            {
                return tException;
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyUseMsCorLibTypes)
            {
                return found.GetType(FullTypeName) ?? tException;
            }

            AssemblyName name = new AssemblyName(AssemblyName);
            if (name.CodeBase != null)
            {
                return tException;
            }

            if (null != (found = LoadedAssemblies.Lookup(name)))
            {
                return found.GetType(FullTypeName) ?? tException;
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyUseLoadedAssemblies)
            {
                return tException;
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyLoadStrongNamed && name.GetPublicKeyToken() == null)
            {
                return tException;
            }

            Type test = Type.GetType(String.Format("{0}, {1}", FullTypeName, name));
            if (test != null)
            {
                return test;
            }

            if (typeResolution == RpcErrorTypeBehavior.LoadAnyAssembly)
            {
                return Type.GetType(String.Format("{0}, {1}", FullTypeName, new AssemblyName(name.Name))) ?? tException;
            }

            return tException;
        }
 public void ReThrow(RpcErrorTypeBehavior typeResolution)
 {
     throw CreateException(typeResolution, true);
 }
        /// <summary>
        ///   Constructs the exception for the client to raise
        /// </summary>
        private Exception CreateException(RpcErrorTypeBehavior typeResolution, bool top)
        {
            Exception baseEx;

            try
            {
                Type tException = GetExceptionType(typeResolution);
                if (tException == null || !typeof(Exception).IsAssignableFrom(tException))
                {
                    tException = typeof(ApplicationException);
                }

                if (!HasMessage)
                {
                    return((Exception)Activator.CreateInstance(tException));
                }

                ConstructorInfo   ci;
                SerializationInfo si = new SerializationInfo(tException, FormatterConverter);
                si.AddValue("ClassName", !HasClassName ? null : ClassName);
                si.AddValue("Message", !HasMessage ? null : Message);
                si.AddValue("InnerException",
                            !HasInnerException ? null : InnerException.CreateException(typeResolution, false));
                si.AddValue("HelpURL", !HasHelpUrl ? null : HelpUrl);
                si.AddValue("StackTraceString", !HasStackTraceString ? null : StackTraceString);
                si.AddValue("RemoteStackTraceString",
                            top
                                ? (!HasStackTraceString ? null : StackTraceString)
                                : (!HasRemoteStackTraceString ? null : RemoteStackTraceString));
                si.AddValue("RemoteStackIndex", !HasRemoteStackIndex ? 0 : RemoteStackIndex);
                si.AddValue("ExceptionMethod", !HasExceptionMethod ? null : ExceptionMethod);
                si.AddValue("HResult", !HasHResult ? 0 : HResult);
                si.AddValue("Source", !HasSource ? null : Source);

                Exception ex = (Exception)FormatterServices.GetUninitializedObject(tException);
                try
                {
                    foreach (Types.RpcExceptionData data in ExceptionDataList)
                    {
                        Type   t     = Type.GetType(data.Type.Split(',')[0], true, false);
                        object value = !data.HasValue
                                           ? null
                                           : t == typeof(byte[])
                                                 ? Convert.FromBase64String(data.Value)
                                                 : Convert.ChangeType(data.Value, t);
                        si.AddValue(data.Member, value, t);
                    }

                    if (HasFullDetails)
                    {
                        ci =
                            tException.GetConstructor(
                                BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Instance, null,
                                CallingConventions.Any, new[] { typeof(SerializationInfo), typeof(StreamingContext) },
                                new ParameterModifier[2]);
                        ci.Invoke(ex, new object[] { si, StreamingContext });
                        return(ex);
                    }
                }
                catch
                {
                    ex = (Exception)FormatterServices.GetUninitializedObject(tException);
                }

                ci = tException.GetConstructor(new[] { typeof(string) });
                ci.Invoke(ex, new object[] { Message });
                return(ex);
            }
            catch
            {
                baseEx = HasMessage ? new ApplicationException(Message) : new ApplicationException();
            }
            return(baseEx);
        }
        private Type GetExceptionType(RpcErrorTypeBehavior typeResolution)
        {
            Type     tException = typeof(ApplicationException);
            Assembly found      = tException.Assembly;

            if (typeResolution == RpcErrorTypeBehavior.NoTypeResolution)
            {
                return(tException);
            }

            if (!HasFullTypeName || !HasAssemblyName || String.IsNullOrEmpty(FullTypeName) ||
                String.IsNullOrEmpty(AssemblyName))
            {
                return(tException);
            }

            if (!ValidTypeName.IsMatch(FullTypeName))
            {
                return(tException);
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyUseMsCorLibTypes)
            {
                return(found.GetType(FullTypeName) ?? tException);
            }

            AssemblyName name = new AssemblyName(AssemblyName);

            if (name.CodeBase != null)
            {
                return(tException);
            }

            if (null != (found = LoadedAssemblies.Lookup(name)))
            {
                return(found.GetType(FullTypeName) ?? tException);
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyUseLoadedAssemblies)
            {
                return(tException);
            }

            if (typeResolution == RpcErrorTypeBehavior.OnlyLoadStrongNamed && name.GetPublicKeyToken() == null)
            {
                return(tException);
            }

            Type test = Type.GetType(String.Format("{0}, {1}", FullTypeName, name));

            if (test != null)
            {
                return(test);
            }

            if (typeResolution == RpcErrorTypeBehavior.LoadAnyAssembly)
            {
                return(Type.GetType(String.Format("{0}, {1}", FullTypeName, new AssemblyName(name.Name))) ?? tException);
            }

            return(tException);
        }
 public void ReThrow(RpcErrorTypeBehavior typeResolution)
 {
     throw CreateException(typeResolution, true);
 }