Exemplo n.º 1
0
 /// <summary>
 /// Generates a controller context to be passed to the controller.
 /// </summary>
 /// <param name="attr">The controller's class attribute.</param>
 /// <returns>The ControllerContext to be passed to the controller.</returns>
 private ControllerContext GetControllerContext(RestQueryControllerAttribute attr)
 {
     return(new ControllerContext()
     {
         Log = this.log,
         RestClient = this.restClient,
         ControllerBaseAddress = $"{this.restQueryConfig.BaseAddress}{attr.Name}"
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// Initiate the controller list. Use reflection to capture controllers and add them to the dictionary.
        /// A Controller must have the class attribute RestQueryController.
        /// </summary>
        private void InitControllers()
        {
            Assembly[] assarr = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly ass in assarr)
            {
                try
                {
                    Type[] typearr = ass.GetTypes();
                    foreach (Type type in typearr)
                    {
                        if (!type.IsClass)
                        {
                            continue;
                        }

                        IEnumerable <Attribute> attrArr = type.GetCustomAttributes();

                        if (attrArr == null)
                        {
                            continue;
                        }

                        foreach (Attribute attr in attrArr)
                        {
                            RestQueryControllerAttribute item = attr as RestQueryControllerAttribute;

                            if (item == null)
                            {
                                continue;
                            }

                            item.ControllerType = type;
                            this.controllers.Add(item);
                        }
                    }
                }
                catch
                {
                    // In some situation an exception is raised which is not harmfull.
                    this.log.Warn("Error while browsing assemblies for controllers (harmless)");
                }
            }
        }