Converting BlogEngine.NET 2.0 to Web Application Project (WAP)

by Johnny Nouel 15. abril 2011 17:46

I've been running BlogEngine.NET on this Blog since its birth.  Started on version 1.5 and recently this year the BlogEngine team released version 2.0.

I decided to migrate to this new version and before doing that I needed to convert the source files to the Web Application Project format.  For me, the WAP format makes more sense that the Web Site model introduced with Visual Studio 2005.  Since this is not a post on comparing the two alternatives I'll just let you follow this link that does exactly that.

Moving on.

For version 1.5 I proceeded and converted the solution to WAP also.  In this blog post from a fellow developer I found the instructions  on how to do so.  The instructions were almost perfect and only few things needed attention to have the BlogEngine running as it should.

This time, for version 2.0, the BlogEngine.NET Codeplex site had the instructions to complete the process on this page .  The instructions are now less in number and they even look a lot cleaner that what we had for version 1.5.  At the end of the step by step instructions, in the Conclusion section, it says that "you don't need to make any changes to the code itself as it was a case in earlier versions".   In my case I did have to make changes to the code in order to make the application work.  Here is my list of changes:

  • Open the property page for the BlogEngine.NET project and set the new name for the Assembly and Namespace.  Let's agree on "BlogEngine".

Application Dialog in Web Project Properties

  • In the Utils.cs file in the BlogEngine.Core project locate the line that sets the assemblyName variable and set its value to the Assembly name that was selected in the previous step.  "BlogEngine".   Do the same for the line that sets the Assembly Name when the check for "IsMono" is made.
        public static IEnumerable<Assembly> CodeAssemblies()
        {
            var codeAssemblies = new List<Assembly>();
            CompilationSection s = null;
            //WAP FIX
            //var assemblyName = "__code";
            var assemblyName = "BlogEngine";
            try
            {
                try
                {
                    s = (CompilationSection)WebConfigurationManager.GetSection("system.web/compilation");
                }
                catch (SecurityException)
                {
                    // No read permissions on web.config due to the trust level (must be High or Full)
                }

                if (s != null && s.CodeSubDirectories != null && s.CodeSubDirectories.Count > 0)
                {
                    for (var i = 0; i < s.CodeSubDirectories.Count; i++)
                    {
                        assemblyName = string.Format("App_SubCode_{0}", s.CodeSubDirectories[i].DirectoryName);
                        codeAssemblies.Add(Assembly.Load(assemblyName));
                    }
                }
                else
                {
                    if (!IsMono)
                    {
                        //WAP FIX
                        //assemblyName = "App_Code";
                        assemblyName = "BlogEngine";
                    }

                    codeAssemblies.Add(Assembly.Load(assemblyName));
                }

                codeAssemblies.AddRange(GetCompiledExtensions());
            }
            catch (FileNotFoundException)
            {
                /*ignore - code directory has no files*/
            }

            return codeAssemblies;
        }
  • Please note that I include the "WAP FIX" comment anywhere I make a fix in order to make this WAP work.   In the Web.Config file make the following addition of the assembly name in the Pages section:
    <pages enableSessionState="false" enableViewStateMac="true" enableEventValidation="true" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
      <controls>
        <!--WAP FIX-->
        <!--<add namespace="App_Code.Controls" tagPrefix="blog"/>-->
        <add assembly="BlogEngine" namespace="App_Code.Controls" tagPrefix="blog"/>
      </controls>
    </pages>
  • In order to make the extensions work again you need to make a change in the Editor.ascx.cs file in the "admin\Extension Manager" folder.   Since the App_Code folder had to be renamed to something else you need to let the engine know where to find the extensions now.  In my case I named the folder "Classes".  Neat huh?  Here's the change in the file:
        protected static string GetExtFileName()
        {
            var fileName = HttpContext.Current.Request.PhysicalApplicationPath;
            var codeAssemblies = Utils.CodeAssemblies();
            foreach (Assembly a in codeAssemblies)
            {
                var types = a.GetTypes();
                foreach (var type in types.Where(type => type.Name == ExtensionName))
                {
                    var assemblyName = type.Assembly.FullName.Split(".".ToCharArray())[0];
                    assemblyName = assemblyName.Replace("App_SubCode_", "App_Code\\");
                    var fileExt = assemblyName.Contains("VB_Code") ? ".vb" : ".cs";
                    //WAP FIX
                    //fileName += Path.Combine(Path.Combine(assemblyName, "Extensions"), ExtensionName + fileExt);
                    fileName += Path.Combine("Classes\\Extensions", ExtensionName + fileExt);
                }
            }

            return fileName;
        }
  • Now, that should do it.  But no. we're not done yet.  Somehow, the classes and files in the admin folder have the classes names and namespaces messed up!  For example, the menu.ascx.cs file had the namespace set to "admin.Settings" when it should be "Admin.Posts".  Note the casing there as well.  Even the casing in the naming was confusing.    Check the namespaces, classes names, and the inherits value in the page directive of the ascx files also.  They should match the directory where they are at least.

Menu.ascx.cs file under Admin/Posts folder:

//WAP FIX
namespace Admin.Posts // Namespaces were a mess.  This was "admin.Settings".  Everything inside the admin folder
    // had to be checked and corrected.
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Menu : System.Web.UI.UserControl

Comments.ascx.cs file under Admin/Settings folder:

//WAP FIX
namespace Admin.Settings //This was "Admin.Comments".  I won't comment the other changes in namespaces in the admin folder.
{
    using System;
    using System.Data;
    using System.Web.UI.WebControls;
    using BlogEngine.Core;
    using BlogEngine.Core.Web.Extensions;
    using App_Code;

    //WAP FIX -> Fixed Class Name which was 'Settings'.
    public partial class Comments : System.Web.UI.Page

And that's it! That's all we need to make the WAP Project for BlogEngine.NET.  But I'm a lot nicer.  Please download the whole BlogEngine.NET Project in WAP Format below.  The Web.Config in the project is the one for SQL Server.  The original one I named "Web ORIGINAL.config".  See what I did there?

Update: Forgot to mention that the download below has the BlogEngine.NET Project now targeting .NET Framework 4.0 instead of 3.5.  Enjoy!

Download BlogEngine.NET 2.0 WAP Source Code

Cheers!

Tags: , , , ,

ASP.NET | BlogEngine.NET

Comentarios (7) -

pratish
pratish India
26/04/2011 14:01:41 #

Thanks johnny for the very useful tutorial. The source code you provided is very helpful for me to prepare a project for my brother to submit it in his school

Johnny
Johnny Dominican Republic
28/04/2011 18:15:51 #

Glad to be of help.
Cheers!

Ricky Gill
Ricky Gill United Kingdom
04/05/2011 15:19:34 #

Thanks for the source code Johnny great help!

Anthony
Anthony United States
08/09/2011 11:25:20 #

Hi Johnny,

Thanks for the link!

I also have just migrated to 2.0 and I'm curious: what was happening that prompted you to make this changes initially? The reason I ask is that I made no code changes and everything seems to be working fine...

Rgds,
Anthony Smile

Johnny
Johnny United States
09/09/2011 16:06:26 #

Hi Anthony,

Because of the way WAP works I needed an Assembly name and modify the code where needed.  Also, I had to make a change to have the extensions working again since the directory structure was not the same.

Regarding fixing the namespaces in the admin folder I don't recall exactly the details but the source I download from codeplex had these issues and they were begging to be fixed.  I should probably see if they're still around in codeplex.

If you did not make any code changes please make sure the extensions work and that the admin pages are also rocking as they should.

Thanks for your comment.

Cheers.

Johnny

Rich Czyzewski
Rich Czyzewski United States
31/03/2012 6:55:37 #

Thanks for the code Johnny.  I just converted my BlogEngine.NET 2.5 to a WAP and this post helped out a lot.  I've put together a similar post to yours with instructions for converting 2.5 to a WAP.  Check out www.richonsoftware.com/.../...Web-Application.aspx

Thanks again!

Johnny
Johnny Dominican Republic
31/03/2012 11:29:42 #

Hi Rich,

Glad it helped.  Just went over to your web site.  Great post!
I'll follow your guide now to convert version 2.5 of BlogEngine. Wink

Cheers.

Johnny

Pingbacks and trackbacks (1)+

Agregar Comentario




  Country flag
biuquote
  • Comentario
  • Vista Previa
Loading


Recent Posts

Sobre mi

Johnny Nouel Mi nombre es Johnny Nouel. Profesional de Tecnología.  Experto en SAP FI CO.  Especialista en SAP BW, BI y BO.  Desarrollador ASP.NET.  Amante de los Videojuegos y la Tecnología.  Aguilucho!

Contactame?