Beispiel #1
0
 /// <summary>
 /// Convert an incoming string to a given object type of IBlogBase
 /// </summary>
 /// <typeparam name="T">The type to convert to</typeparam>
 /// <param name="data">The data string to convert</param>
 /// <returns></returns>
 public Object FromXmlString(String data)
 {
     try
     {
         // Create a new XmlSerializer instance with the type of the existing type
         XmlSerializer serialiser = new XmlSerializer(this.GetType());
         using (StringReader reader = new StringReader(data))
         {
             // Deserialise and return the object
             return(serialiser.Deserialize(reader));
         }
     }
     catch (Exception ex)
     {
         throw BlogException.Passthrough(ex, new CastObjectBlogException(ex)); // Failed, explain why
     }
 }
        /// <summary>
        /// Set up the blog from the configuration section in the app startup
        /// </summary>
        /// <param name="value">The application builder that this attaches to</param>
        /// <param name="env">The hosting environment supplied from the startup process </param>
        public static IApplicationBuilder UseBlog(this IApplicationBuilder value, IHostingEnvironment env)
        {
            // Set the environment
            Environment = env;

            // Scan all of the controllers for blog controllers so that they can be registered at start ip
            // that way the blogs can be used from other pages etc.
            BlogRegistrationHelper regHelper   = new BlogRegistrationHelper();
            IEnumerable <Type>     controllers =
                BaseClassExtensions.GetEnumerableOfType <BlogControllerBase>(Assembly.GetCallingAssembly());

            // Loop the found controllers
            foreach (Type controller in controllers)
            {
                try
                {
                    // Call the registration process
                    regHelper.Register(controller, ref Blogs.Items);

                    // Create a new instance of the blog controller purely to
                    // fire the customisable initialisation routine
                    BlogControllerBase blogInstance = (BlogControllerBase)Activator.CreateInstance(controller);
                    if (blogInstance == null)
                    {
                        throw new NotInitialisedBlogException($"Could not initialise the blog '{controller.Name}'");
                    }
                    else
                    {
                        // Call the blog initialised method so custom actions can be applied
                        blogInstance.BlogInitialised();
                    }
                }
                catch (Exception ex)
                {
                    throw BlogException.Passthrough(ex, new NotInitialisedBlogException($"Could not initialise the blog '{controller.Name}'"));
                }
            }

            // Allow stacking so it's consistent with the other extension methods
            return(value);
        }