Example #1
0
 public static IFromJson CreateObjectFromScriptType(string scriptType, ClientRuntimeContext context)
 {
     ScriptTypeMap.EnsureInited();
     foreach (IScriptTypeFactory current in ScriptTypeMap.s_scriptTypeFactories)
     {
         IFromJson fromJson = current.CreateObjectFromScriptType(scriptType, context);
         if (fromJson != null)
         {
             IFromJson result = fromJson;
             return(result);
         }
     }
     ScriptTypeMap.ScriptTypeInfo scriptTypeInfo = null;
     if (ScriptTypeMap.s_clientProxies.TryGetValue(scriptType, out scriptTypeInfo))
     {
         IFromJson result2;
         if (scriptTypeInfo.ValueObject)
         {
             result2 = (Activator.CreateInstance(scriptTypeInfo.Type) as IFromJson);
         }
         else
         {
             Type     arg_86_0 = scriptTypeInfo.Type;
             object[] array    = new object[2];
             array[0] = context;
             result2  = (Activator.CreateInstance(arg_86_0, array) as IFromJson);
         }
         return(result2);
     }
     return(null);
 }
Example #2
0
 private static void LoadClientTypeAssemblies()
 {
     try
     {
         ScriptTypeMap.LoadAssembliesDefinedInConfigFile();
     }
     catch (Exception ex)
     {
         if (ScriptTypeMap.IsFatalException(ex))
         {
             throw;
         }
     }
     try
     {
         ScriptTypeMap.LoadAssembliesDefinedInProgramFiles();
     }
     catch (Exception ex2)
     {
         if (ScriptTypeMap.IsFatalException(ex2))
         {
             throw;
         }
     }
 }
Example #3
0
        public virtual void ExecuteQuery()
        {
            ScriptTypeMap.EnsureInited();
            ClientRequest pendingRequest = this.PendingRequest;

            this.m_request = null;
            pendingRequest.ExecuteQuery();
        }
Example #4
0
 public static void AddClientTypeAssembly(Assembly assembly)
 {
     if (assembly == null)
     {
         throw new ArgumentNullException("assembly");
     }
     ScriptTypeMap.EnsureInited();
     ScriptTypeMap.AddClientProxyAssembly(assembly);
 }
Example #5
0
 public static Type GetTypeFromScriptType(string scriptType)
 {
     ScriptTypeMap.EnsureInited();
     ScriptTypeMap.ScriptTypeInfo scriptTypeInfo = null;
     if (ScriptTypeMap.s_clientProxies.TryGetValue(scriptType, out scriptTypeInfo))
     {
         return(scriptTypeInfo.Type);
     }
     return(null);
 }
Example #6
0
        private static void AppDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
        {
            Assembly loadedAssembly = args.LoadedAssembly;
            //Edited for .NET Core
            //object[] customAttributes = loadedAssembly.GetCustomAttributes(typeof(ClientTypeAssemblyAttribute), false);
            var customAttributes = loadedAssembly.GetCustomAttributes <ClientTypeAssemblyAttribute>();

            if (customAttributes != null && customAttributes.Count() > 0)
            {
                ScriptTypeMap.AddClientProxyAssembly(loadedAssembly);
            }
        }
Example #7
0
 internal static void EnsureInited()
 {
     if (ScriptTypeMap.s_inited)
     {
         return;
     }
     lock (ScriptTypeMap.s_lock)
     {
         if (!ScriptTypeMap.s_inited)
         {
             ScriptTypeMap.Init();
             ScriptTypeMap.s_inited = true;
         }
     }
 }
Example #8
0
        private static void Init()
        {
            ScriptTypeMap.LoadClientTypeAssemblies();
            //Edited for .NET Core
            //Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            //AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ScriptTypeMap.AppDomain_AssemblyLoad);
            //Assembly[] array = assemblies;
            //for (int i = 0; i < array.Length; i++)
            //{
            //    Assembly assembly = array[i];
            //    try
            //    {
            //        object[] customAttributes = assembly.GetCustomAttributes(typeof(ClientTypeAssemblyAttribute), false);
            //        if (customAttributes != null && customAttributes.Length != 0)
            //        {
            //            ScriptTypeMap.AddClientProxyAssembly(assembly);
            //        }
            //    }
            //    catch (Exception)
            //    {
            //    }
            //}

            // The above code seems to loop all loaded assemblies to check for the ClientTypeAssemblyAttribute
            // The below is how I have rewritten it for .NET Core
            // NOTE: I can't find a way to hook to something similiar to AppDomain.CurrentDomain.AssemblyLoad

            var currentAssembly = Assembly.GetEntryAssembly();
            var currentAssemblyClientTypeAttributes = currentAssembly.GetCustomAttribute <ClientTypeAssemblyAttribute>();
            var currentAssemblyReferencedAssemblies = currentAssembly.GetReferencedAssemblies();

            foreach (var assemblyName in currentAssemblyReferencedAssemblies)
            {
                try
                {
                    var assembly = Assembly.Load(assemblyName);

                    var customAttributes = assembly.GetCustomAttributes <ClientTypeAssemblyAttribute>();
                    if (customAttributes != null && customAttributes.Count() != 0)
                    {
                        ScriptTypeMap.AddClientProxyAssembly(assembly);
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        protected internal void UpdateClientObjectPropertyType(string propertyName, object propertyValue, JsonReader reader)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw ClientUtility.CreateArgumentNullException("propertyName");
            }
            if (propertyValue == null)
            {
                throw ClientUtility.CreateArgumentNullException("propertyValue");
            }
            if (reader == null)
            {
                throw ClientUtility.CreateArgumentNullException("reader");
            }
            ClientObject clientObject = propertyValue as ClientObject;

            if (clientObject == null)
            {
                throw ClientUtility.CreateArgumentException("propertyValue");
            }
            if (!this.ObjectData.ClientObjectProperties.ContainsKey(propertyName))
            {
                throw ClientUtility.CreateArgumentException("propertyName");
            }
            string scriptType;

            if (reader.PeekTokenType() == JsonTokenType.ObjectStart && reader.PeekObjectType(out scriptType))
            {
                Type typeFromScriptType = ScriptTypeMap.GetTypeFromScriptType(scriptType);
                //Edited for .NET Core
                //if (typeFromScriptType != null && typeFromScriptType != propertyValue.GetType() && propertyValue.GetType().IsAssignableFrom(typeFromScriptType))
                if (typeFromScriptType != null && typeFromScriptType != propertyValue.GetType())
                {
                    ClientObject clientObject2 = ScriptTypeMap.CreateObjectFromScriptType(scriptType, this.Context) as ClientObject;
                    if (clientObject2 != null)
                    {
                        clientObject.SetTypedObject(clientObject2);
                        this.ObjectData.ClientObjectProperties[propertyName] = clientObject2;
                    }
                }
            }
        }
Example #10
0
 public static IFromJson CreateObjectFromFallbackScriptType(Type fallbackType, ClientRuntimeContext context)
 {
     ScriptTypeMap.EnsureInited();
     ScriptTypeMap.ScriptTypeInfo scriptTypeInfo = null;
     if (ScriptTypeMap.s_typeToScriptTypeMap.TryGetValue(fallbackType, out scriptTypeInfo))
     {
         IFromJson result;
         if (scriptTypeInfo.ValueObject)
         {
             result = (Activator.CreateInstance(fallbackType) as IFromJson);
         }
         else
         {
             object[] array = new object[2];
             array[0] = context;
             result   = (Activator.CreateInstance(fallbackType, array) as IFromJson);
         }
         return(result);
     }
     return(null);
 }
Example #11
0
        private static void LoadAssembliesDefinedInProgramFiles()
        {
            string text = ClientUtility.GetSetupDirectory();

            if (string.IsNullOrEmpty(text))
            {
                return;
            }
            text = Path.Combine(text, "TypeAssemblies");
            if (Directory.Exists(text))
            {
                string[] files = Directory.GetFiles(text, "ClientTypeAssembly.*.xml");
                for (int i = 0; i < files.Length; i++)
                {
                    string path = files[i];
                    try
                    {
                        using (TextReader textReader = System.IO.File.OpenText(path))
                        {
                            //XmlDocument xmlDocument = new XmlDocument();
                            //xmlDocument.Load(XmlReader.Create(textReader));
                            //foreach (XmlNode xmlNode in xmlDocument.DocumentElement.ChildNodes)
                            //{
                            //    if (xmlNode.NodeType == XmlNodeType.Element && xmlNode.Name == "AssemblyName")
                            //    {
                            //        Assembly.Load(xmlNode.InnerText);
                            //    }
                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ScriptTypeMap.IsFatalException(ex))
                        {
                            throw;
                        }
                    }
                }
            }
        }
 private void ProcessResponseStream(Stream responseStream)
 {
     StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
     JsonReader jsonReader = new JsonReader(reader, this.m_context);
     jsonReader.ReadArrayStart();
     Dictionary<string, object> dictionary = jsonReader.ReadObject() as Dictionary<string, object>;
     if (dictionary == null)
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     object obj;
     if (!dictionary.TryGetValue("SchemaVersion", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     string text = obj as string;
     if (string.IsNullOrEmpty(text))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     this.m_context.ServerSchemaVersion = new Version(text);
     if (!dictionary.TryGetValue("LibraryVersion", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     string text2 = obj as string;
     if (string.IsNullOrEmpty(text2))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     this.m_context.ServerLibraryVersion = new Version(text2);
     if (dictionary.TryGetValue("TraceCorrelationId", out obj))
     {
         this.m_context.SetTraceCorrelationId(obj as string);
     }
     if (!dictionary.TryGetValue("ErrorInfo", out obj))
     {
         throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
     }
     object obj2 = obj;
     if (obj2 != null)
     {
         Dictionary<string, object> dictionary2 = obj2 as Dictionary<string, object>;
         if (dictionary2 == null)
         {
             throw new ClientRequestException(Resources.GetString("RequestUnknownResponse"));
         }
         ServerException ex = ServerException.CreateFromErrorInfo(dictionary2);
         throw ex;
     }
     else
     {
         if (!ClientRuntimeContext.CanHandleResponseSchema(this.m_context.ServerSchemaVersion))
         {
             throw new ClientRequestException(Resources.GetString("CannotHandleServerResponseSchema", new object[]
             {
                 text
             }));
         }
         while (jsonReader.PeekTokenType() != JsonTokenType.ArrayEnd)
         {
             long num = jsonReader.ReadInt64();
             obj = null;
             if (this.m_queryIdToObjectMap.TryGetValue(num.ToString(CultureInfo.InvariantCulture), out obj) && obj != null)
             {
                 ClientObject clientObject = obj as ClientObject;
                 string scriptType = null;
                 if (clientObject != null && jsonReader.PeekTokenType() == JsonTokenType.ObjectStart && jsonReader.PeekObjectType(out scriptType))
                 {
                     Type typeFromScriptType = ScriptTypeMap.GetTypeFromScriptType(scriptType);
                     if (typeFromScriptType != null && typeFromScriptType != clientObject.GetType())
                     {
                         ClientObject clientObject2 = ScriptTypeMap.CreateObjectFromScriptType(scriptType, this.Context) as ClientObject;
                         if (clientObject2 != null)
                         {
                             clientObject.SetTypedObject(clientObject2);
                             obj = clientObject2;
                         }
                     }
                 }
                 IFromJson fromJson = obj as IFromJson;
                 if (fromJson != null && !fromJson.CustomFromJson(jsonReader))
                 {
                     fromJson.FromJson(jsonReader);
                 }
             }
             else
             {
                 jsonReader.ReadObject();
             }
         }
         return;
     }
 }