Pragmatic Developer

Ali Özgür

Bookmark Blog

Add to Technorati Favorites

Google Talk

Chat with Ali Özgür

Purchase PragmaSQL from

Calendar

«  November 2008  »
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
View posts in large calendar

Tag Cloud

Don't show

    Authors

    Recent Comments

    Banners




    Here is aother strange problem related with NHibernate

    The Problem

    I have a Parent class and two child classes Child1 and Child2 mapped to different tables on the database.
    Lets assume that we specified cascade='all' for child bags defined on Parent.hbm.xml. Sample workflow of instantiating parent and child objects is as the following

    - Create a parent object.
    - Insert 2 Child1 instances to child1 bag
    - Insert 3 Child2 instances to child2 bag.
    - Flush the session
    - Refresh parent object
    - We get 6 instances for each child bag (child1 and child2). But we expect 2 Child1 instances in child1 bag and 3 Child2 instances in child2 bag.

    Ther problem is : NHibernate performs left outer join on Child1 and Child2 tables when Refresh is called for the parent object. This is unaccaptable, I think NHibernate should initialize child collections with seperate selects commited to the database, or may be distinguish the duplicated child instances automatically in the collections. (using idbag instead of bag is not an option)

    Download the test case

    Requirements

    Watch This


    Posted in: .NET Development , C# , NHibernate  Tags:

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    I discovered this problem while developing a new system with the latest NHibernate.Burrow distribution (which in turn uses the latest NHibernate distribution). I spent some time Googling around to check if anyboy else met the same problem and found some entries but none of them specified exactly why this failure was happening and how we can solve this problem.

    The Problem

    My code simply executes the following steps

    1. Call a factory method
    2. Create a Curve
    3. Create CurveInterval objects
    4. Put CurveInterval objects inside the Curve.Intervals collection
    5. Call the same factory function to create another Curve instance
    6. Before creating Curve instance I perform some checks with HQL , this causes session to be flushed automaticaly
    7. I get the error when session is about to be flushed automatically

    001 <?xml version='1.0' encoding='utf-8'?> 
    002<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'> 
    003<class 
    004name='GradeEntry.Domain.Curve, GradeEntry.Domain' 
    005      table= 'LetterGrade'> 
    006<id name='Id' 
    007column='LG_ID' 
    008unsaved-value='0'> 
    009<generator class='native'/> 
    010</id> 
    011
    012<version name='ManagedVersion' column='NhVersion' type="System.Int64" unsaved-value="0"/> 
    013
    014<property name='DateCreated'/> 
    015<property name='CreatedByPersonId'/> 
    016<property name='Locked'/> 
    017<property name='LockChangedById' column ='LockChangedByID'/> 
    018<property name='LockChangedOn'/> 
    019<property name='LockMessage'/> 
    020<property name='LogIdx'/> 
    021<property name='LogCnt'/> 
    022<property name='IsStable'/> 
    023
    024<bag name='Intervals' 
    025order-by='LetterID asc' 
    026table='LetterGradeList' 
    027access='field.camelcase-underscore' 
    028inverse='true' 
    029cascade='delete-orphan, save-update' 
    030lazy='true'> 
    031<key column='LG_ID'/> 
    032<one-to-many class='GradeEntry.Domain.CurveInterval, GradeEntry.Domain' /> 
    033</bag> 
    034
    035<bag name='Log' 
    036order-by='Idx asc' 
    037table='LetterGradeLog' 
    038access='field.camelcase-underscore' 
    039inverse='true' 
    040cascade='delete-orphan,save-update' 
    041lazy='true'> 
    042<key column='LG_ID'/> 
    043<one-to-many class='GradeEntry.Domain.CurveLog, GradeEntry.Domain' /> 
    044</bag> 
    045
    046</class> 
    047</hibernate-mapping>
    048
    049
    050

     

    First Attempt To Reporoduce The Problem

    I have prepared a sample project but I was not able to recreate the bug, and I know it is hard to tell what is the problem under these conditions. But I have some more findings about this issue.

    - When I replace cascade of the bags to 'all-delete-orphan' from 'delete-orphan,save-update' everything is ok
    - When I replace cascade of the bags to 'all, delete-orphan' from 'all-delete-orphan' I still get the assertion failure.

    I think the problem is with the new implementation of cascade which is intended to support comma seperated list of cascade options.

    Second Attempt To Reporoduce The Problem

    After spending some more time trying to diagonise the problem I finally succeeded to reprouce the problem with atest case. I attached the test case.

    Simply the problem resolves to something like this.

    NOTE: All classes utilize managed versioning by defining ManagedVersion.

    1. I create 2 parent objects save them , flush the session and refresh them
    2. Then for each parent object I create some child objects of type Child1
    3. I perform HQL to get all Child3 instances. Child3 class does not have any association or cascade relation with Parent, Child1, Child2 and ParentRef classes. The HQL I performed causes the session to auto flush thus inserting Child1 instances to the database. But somehow while auto flushing the session versions of the parent objects are not updated. I expect two updates to be submitted to the database for parent objects since we associated Child1 instances with the parents and that must cause a version increment on parent objects.
    4. Then I try to perform another HQL query to get list of ParentRef objects and I get assertion failure. Note that ParentRef class is associated with Parent class which in turn defines cascade for Children1 and Children2 collections.

    If we do not perform the HQL in step 3 Child1 instances will be inserted to the database and parent objects will be updated while performing the HQL in step 4 and we will not get the assertion failure.

    I covered both cases in the attached test case.

    Database scripts are included under DBScript folder.

    Download test case

    Requirements

    Watch This

    See Also


    Posted in: .NET Development , C# , NHibernate  Tags:

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    I've already developed some projects with NHibernate and I can say that NHibernate is a real time saver. But I must admit that it takes some time to get used to NHibernate. As an ORM NHibernate has fantastic features but if you want to develop successfull projects with NHibernate you must know that NHibernate is only one part (ORM) of a successfull layered architecture. In the attached solution I tried to give you the basic idea of a layered architecture and how NHibernate can be used within such an architecture

    Attached solution includes the following projects

    • Core: Our domain objects (entities) reside in this layer. Also DAO (Data Access Objects) interfaces are defined in this layer.
    • DAL (Data Access Layer): Default implementation of DAO interfaces reside in this layer.
    • BLL (Business Logic Layer): Business logic code resides in this layer.
    • Common: Some utility calsses reside in this project
    • Tests.Disconnected: Includes unit tests for domain objects and business logic. Notice that unit tests in this project do not require domain objects to be persisted (NHibernate behaviour will not be tested), so the need for a database connection is eliminated which in turn makes our unit tests fast. Notice how we replaced our default DAL with mocked one.
    • Tests.Connected: This project includes unit tests too, but this time we want to test how our domain objects and bussiness logic perform NHibernate. NHibernate provides very cool features like native sql, lazy loading and cascading and we will likely want to test how our domain objects behave when armored with these cool features of NHibernate. It is also very likely that we will have some mapping errors (typos likely) in our Core, these tests will help us catch these errors. Performance bottlenecks possibly caused by our NHibernate mappings (for example we may discover that we need to make a child collection to be lazy loaded) can also be identified with help of these tests.

    In the sample solution you can also find a simple usage of Castle Windsor Inversion of Control (IoC) container. We use IoC to be able to load different implementations of our DAL (DAO implementations). 

    Another very important point you need to understand really well is session management of NHibernate when used in web applications. Thanks to Burrow contribution project this task is made very simple, you do not even need to write single line of NHibernate session management code. You only need to inherit your DAO implementation classes from GenericDao<T> class and you are ready to go. 

    Additional Notes

    • Database script is included in Tests.Connected project under DBScript folder. 
    • Sample project uses NHibernate  2.0 Aplha1 and NHibernate.Burrow is also Alpha1.
    • ASP .NET MVC Preview 3 to run Sales.MVC sample
    • TestDriven .NET to run NUnt tests

    Suggested Readings

    Downloads

    Update History

    • 09 June 2008
      • Castle files under Libs folder updated to Castle RC3
      • SQLite references removed
      • TestFixtureTearDown override of CustomTestBase class in Sales.Tests.Connected assembly commented out

    Posted in: NHibernate  Tags:

    Currently rated 4.8 by 4 people

    • Currently 4.75/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    aliozgur posted on February 18, 2008 16:24

    Introduction

    Cuyahoga framework has very nice approach to Web development. It has bunch of built-in modules and you can develop your own modules in a
    couple of hours. If you have some experience with NHibernate and/or some other web framework your module development
    may even take less than an hour. My product site PragmaSQL Online runs on top of Cuyahoga framework and it took
    me just a couple of hours to bring this site up and running. Although Cuyahoga is a very nice framework and I love Cuyahoga development
    I shall admit that you may experience some problems while applying some advanced topics like Ajax to Cuyahoga. In this article
    I will show you a simple and structured way to add Ajax support to your Cuyahoga web site.

     

    Background

    I previously shared my module development experience with an article titled Developing Simple Issue Tracker Module here on CodeProject.
    In my issue tracker module I used AjaxToolkit ModalPopupExtender control. But this was an unstructured approach it was a kind of hack,
    I simply placed ScriptManager on my ASPX page and moved injection code of GeneralPage class to OnPreInit method from OnInit.
    This was the right choice and it saved the day, this module is still online and is working very well.

    Nowadays I am working on another web site BenimOdam.com (The web site is in Turkish and is not online yet due to domain name transfer issues).
    In BenimOdam.com we only use Forum and ContactUs built-in modules and much of the functionality is embedded in our own modules. These modules
    are mainly functioning as list and record editing modules. We used MultiView and View controls to provide tabbed browsing functionality and
    I guess much of you know that MultiView does a post back while switching between views and this post back may be annoying from the users point of view.
    In such uncomfortable situations UpdatePanel control included with Microsoft Ajax distribution(previously known as Atlas) provides a nice and easy to apply solution.
    You simply put your controls, MultiView in our case, inside an UpdatePanel and you are done. Your users will experience much more smooth
    navigation and probably they will be happier.

    Cuyahoga Internals

    Cuyahoga has some principles we must keep in mind before attempting to extend the framework for Ajax support. These are

    • Template user control with placeholders is used to identify different parts of your pages
    • Custom HttpHandler (PageHandler) is used to process requests and custom UrlWriter to rewrite raw urls.
    • Page structure(sections) and modules contained within the sections are resolved from the database
    • Injection is used to build the resulting pages.
    • Modules are designed as user Controls and you must inherit your module control from BaseModuleControl
    • You can use GeneralPage base class to build custom ASPX pages not releated to any Cuyahoga node that use the
      default site template.

    Ajax Support Preperation

    UpdatePanel included within Microsoft Ajax distirbution and AjaxToolkit controls all require a ScriptManager control placed
    as the first control in your ASPX page. But Cuyahoga does not handle your modules as seperate ASPX pages and injects
    your module inside the template you are using. As i mentioned above a template has placeholder controls that make the
    different parts of your pages. As a result it is not guaranteed that the ScripManager control you placed as the first
    control in your module will also be placed as the first control in the rendered page. Being the first control in the resulting
    page is a very tight constraint emposed by ASP.NET. Solution to this situation is placing a default ScriptManager control inside your
    template user controls as the first control. Here is a sample template user control code

     
    <%@ Control Language="c#" AutoEventWireup="false" Inherits="Cuyahoga.Web.UI.BaseTemplate" %> 
    <%@ Register TagPrefix="cc1" Namespace="Cuyahoga.ServerControls.Navigation" 
       Assembly="Cuyahoga.ServerControls.Navigation" %> 
    <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      Namespace="System.Web.UI" TagPrefix="asp" %> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
        <title> <asp:literal id="PageTitle" runat="server"> </asp:literal> </title> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <asp:literal id="MetaTags" runat="server" /> 
        <asp:literal id="Stylesheets" runat="server" /> 
        <!--[if IE]> 
        <style type="text/css" media="screen"> 
            body { behavior: url(<%= Page.ResolveUrl("~/csshover.htc") %> ); /* call hover behaviour file */ } 
        </style> 
        <![endif]--> 
    </head> 
    <body> 
        <form id="t" method="post" runat="server"> 
            <asp:ScriptManager ID="DefaultScriptManager" runat="server"> </asp:ScriptManager> 
            <div id="container">             
                <div id="header">     
                    <div id="logo"> 
                     <img width="120px" height="100px"  alt="BenimOdam.com" 
                    src="http://www.mydomain.com/Templates/Bo/Images/bo_logosmaller.gif%22/> 
                    </div> 
                    <div> 
                        <span id="titletext"> Ev arkadaşı ve ev arayanların buluşma noktası.</span> 
                    </div>         
                    <div id="searcharea"> 
                        <asp:placeholder id="searchinput" runat="server"> </asp:placeholder> 
                    </div> 
                </div> 
                <div id="nav"> 
                    <cc1:menu id="mnuMain" runat="server" MenuCss = "~/Templates/Bo/Css/Menu.css"> </cc1:menu> 
                </div>     
    
    
                <!-- shadow divs --> 
                <div id="containerleft"> 
                <div id="containertopleft"> 
                <div id="containerright"> 
                <div id="containertopright"> 
                <!-- main --> 
                <div id="main"> 
                    <!--
                    <div id="globalmenu"> 
                        <asp:placeholder id="globalMenu" runat="server"> </asp:placeholder> 
                    </div> 
                    -->
    

    In the sample code above you can see that we added a ScriptManager named DefaultScriptManager as the first control
    of the form. That means DefaultScriptManager will be injected to all our Cuyahoga pages using this template. As a result we
    met the constraint saying that "Ajax controls need a ScriptManager and this script manager must be the first control in the ASPX page.".

    NOTE: Do not forget to register System.Web.Extensions.dll, else you will get an exception telling you that ScriptManager type can not be resolved.

    Adding Ajax Support To Your Modules

    In theory we do not have to add ScriptManager to our module code, since we added a default ScriptManager to our template which will
    automatically be injected to all of our Cuyahoga pages. But in practice we will probably want designer support while developing our modules
    and if you do not include ScriptManager in your module control the designer will complain and refuse to render the AJAX control
    which can make us feel uncomfortable (you can still design your module markup without designer support). When you add a ScriptManager
    to your module the resulting Cuyahoga page will contain more than one ScriptManager one from the template and one or many from your modules.
    Another constraint about ScriptManager says that "Only one ScriptManager can be used in a page" and we have to find a way to
    remove additional ScriptManager instances and leave only one ScriptManager in the resulting page. The solution here is straight forward
    we will remove ScriptManagers placed in our modules and leave only the DefaultScriptManager placed in our template control.

    LIMITATIONS If you want to use custom JavaScript code for AJAX handling in your module you have to register
    your scripts to your ScriptManager's (one included in your module code) Scripts collection. In this case you will have to rethink
    the solution I proposed. May be you will have to invent some interaction that places your custom scripts in the DefaultScriptManager
    before removing the ScriptManager from your module.

    As i mentioned above all of your Cuyahoga modules must be inherited from BaseModuleControl.But we have to find a way to remove
    ScriptManagers from our module code before they are injected to the template we are using. The solution is creating another base class ( AjaxBaseModuleControl)
    which supports ScripManager removal functionality. AjaxBaseModuleControl is inherited from BaseModuleControl and overrides AddedControl method.
    In the AddedControl function we try to catch the ScriptManager control after it is added to the Controls collection of our module and remove it from
    from the colllection so that multiple ScriptManagers problem is avoided.

    NOTE: We could prefer to modify PageEngine class so that we would inspect all controls and remove the ScriptManagers from the modules.
    But that would probably cause performance problems since we would have to loop with foreach on modules' Controls collection.May be some modules
    would not even use AJAX and that would be waste of time inspecting these modules for a ScriptManager control.
    ( Marker interfaces could be used to identify AJAX modules but still that would be wast of time to loop)

    Here is the AjaxBaseModuleControl code

     
    namespace Cuyahoga.Web.UI
    {
      public class AjaxBaseModuleControl:BaseModuleControl
      {
        protected override void AddedControl(Control control, int index)
        {
          if (control.GetType() == typeof(ScriptManager))
            this.Controls.RemoveAt(index);
          else
            base.AddedControl(control, index);
        }
      }
    }
    


    AjaxBaseModuleControl is simple and straightforward, we simply catch the ScriptManager after it is added to Controls collection
    and remove it from the collection, which enables us to avoid multiple ScriptManagers problem.

    IMPORTANT: Cuyahoga PageEngine class applies the template and injects your modules in the overriden OnInit function.
    I would recommend you to move OnInit code to the overriden OnPreInit function. That is not necessary for module level Ajax support
    but the reason will be more clear when I explain page level Ajax support.

    Adding Ajax Support To Your Nodeless Pages

    Modules are the primary mains of Cuyahoga development. But it is obvious that only modules may not meet all your requirements.
    For example you would list records with a module and deploy a seperate nodeless page for record editing. We call the record editing page
    nodeless because this page is not attached to any node in our site structure.For such cases Cuyahoga framework provides us a base
    class named GeneralPage. You inherit your nodeless page from GeneralPage and Cuyahoga automatically applies
    the default site template (CSS styles and structure of the page based on the default template) to your page. Actually
    your page code is injected not rendered.

    For nodeless page example please go to Pragma Issue Tracker and try to view an issue from the issue list. The page
    used to view a specific issue is a nodeless page and it is not included in the site structure we simply redirect to
    this page from our module and Cuyahoga injects the page code automatically.

    In order to add Ajax support to our nodeless pages we have to apply the same ideas.

    • Add default ScriptManager as the first control to the resulting page.
    • Automatically remove the ScriptManager, added during design time, before Cuyahoga injects our page's source


    First item was already applied by putting a default script manager to our template control. For the second item
    we create another base class named AjaxGeneralPage which is inherited from GeneralPage and
    override the AddedControl method to intercept and catch the ScriptManager included in our nodeless page.
    Here is the code:

     
    namespace Cuyahoga.Web.UI
    {
      public class AjaxGeneralPage:GeneralPage
      {
        protected override void AddedControl(Control control, int index)
        {
          if (control.GetType() == typeof(HtmlForm))
            TryToRemoveScriptManager(control as HtmlForm);
          else
            base.AddedControl(control, index);
        } 
        private void TryToRemoveScriptManager(HtmlForm frm)
        {
          int idx = -1;
          for (int i = 0; i <frm.Controls.Count; i++)
          {
            if (frm.Controls[i] is ScriptManager)
            {
              idx = i;
              break;
            }
          } 
          if (idx >= 0)
            frm.Controls.RemoveAt(idx);
        }
      }
    }  

    Please be warned that we do not catch the ScriptManager directly as that was the case in AjaxBaseModuleControl. We catch the
    HtmlForm control included within the page and search for ScriptManager in the form's Controls collection.

    IMPORTANT NOTE: Original version of GeneralPage class (base class of our AjaxGeneralPage) handles content
    loading in the overriden OnInit function (I think this was the only place in .NET 1.1 version to perform content loading).
    If you leave content loading code inside this function you will not be able to properly remove the ScriptManager control
    from your page. For details why this is not possible see ASP.NET Page Lifecylcle article on MSDN. To solve this problem
    we simply move the code in overriden OnInit function to OnPreInit override.That is the right place for content
    loading and dynamic control creation in .NET version 2.0.

    Our OnPreIniti function in GeneralPage class looks like this


     
    protected override void OnPreInit(EventArgs e)
    {
        // The GeneralPage loads it's own content. No need for the PageEngine to do that.
        base.ShouldLoadContent = false;
        
        //Init the PageEngine.
        //NOTE: We replaced base.OnInit with base.OnPreIniti
        base.OnPreInit(e);
        
        // Build page.
        ControlCollection col = this.Controls; 
    
        this._currentSite = base.RootNode.Site;
        if (this._currentSite.DefaultTemplate != null 
            && this._currentSite.DefaultPlaceholder != null 
            && this._currentSite.DefaultPlaceholder != String.Empty)
        {
            // Load the template
            this.TemplateControl = (BaseTemplate)this.LoadControl(UrlHelper.GetApplicationPath() 
                + this._currentSite.DefaultTemplate.Path); 
    
    
            // Register css
            string css = UrlHelper.GetApplicationPath() 
                + this._currentSite.DefaultTemplate.BasePath
                + "/Css/" + this._currentSite.DefaultTemplate.Css;
            RegisterStylesheet("maincss", css); 
    
    
            if (this._title != null)
            {
                this.TemplateControl.Title = this._title;
            } 
    
    
            // Add the pagecontrol on top of the control collection of the page
            this.TemplateControl.ID = "p";
            col.AddAt(0, this.TemplateControl); 
    
    
            // Get the Content placeholder
            this._contentPlaceHolder = this.TemplateControl.FindControl(this._currentSite.DefaultPlaceholder) as PlaceHolder;
            if (this._contentPlaceHolder != null)
            {
                // Iterate through the controls in the page to find the form control.
                foreach (Control control in col)
                {
                    if (control is HtmlForm)
                    {
                        // We've found the form control. Now move all child controls into the placeholder.
                        HtmlForm formControl = (HtmlForm)control;
                        while (formControl.Controls.Count > 0)
                        {    
                            this._contentPlaceHolder.Controls.Add(formControl.Controls[0]);                        
                        }
                    }
                } 
    
    
    
                // throw away all controls in the page, except the page control 
                while (col.Count > 1)
                {
                    col.Remove(col[1]);                
                }
            } 
    
    
            #region // Ali Ozgur (07-02-2008): Load sections that are related to the template
            foreach (DictionaryEntry sectionEntry in _currentSite.DefaultTemplate.Sections)
            {
                string placeholder = sectionEntry.Key.ToString();
                Section section = sectionEntry.Value as Section;
                if (section != null)
                {
                    BaseModuleControl moduleControl = CreateModuleControlForSection(section);
                    if (moduleControl != null)
                    {
                        ((PlaceHolder)this._templateControl.Containers[placeholder]).Controls.Add(moduleControl);
                    }
                }
            }
            #endregion 
        }
        else
        {
            // The default template and placeholders are not correctly configured.
            throw new Exception("Unable to display page because the default template is not configured.");
        }
    } 
    
    
    #region // Ali Ozgür 07-02-2008 : Load sections that are related to the template
    private BaseModuleControl CreateModuleControlForSection(Section section)
    {
        // Check view permissions before adding the section to the page.
        if (section.ViewAllowed(this.User.Identity))
        {
            // Create the module that is connected to the section.
            ModuleBase module = _moduleLoader.GetModuleFromSection(section); 
    
    
            if (module != null)
            {
                if (Context.Request.PathInfo.Length > 0 && section == this._activeSection)
                {
                    // Parse the PathInfo of the request because they can be the parameters 
                    // for the module that is connected to the active section.
                    module.ModulePathInfo = Context.Request.PathInfo;
                }
                return LoadModuleControl(module);
            }
        }
        return null;
    } 
    
    
    private BaseModuleControl LoadModuleControl(ModuleBase module)
    {
        BaseModuleControl ctrl = (BaseModuleControl)this.LoadControl(UrlHelper.GetApplicationPath() + module.CurrentViewControlPath);
        ctrl.Module = module;
        return ctrl;
    }
    #endregion 
    
    

    In the generalPage code snippet presented above you will notice regions of code that adds support for loading sections
    attached to the default site template. This code has nothing to do with AJAX support it was an improvement
    neede for BenimOdam.com

    Modifying the HttpHandler for AJAX support

    As I mentioned in Cuyahoga Internals section of the article, Cuyahoga framework registers a custom HttpHandler class named
    PageHandler to handle page requests. This handler is needed as the result of injection practice used
    in the framework. Cuyahoga does not actually renders physical pages or user controls. Page structure is
    retreived from the database (sections and modules within these sections)and modules are instantiated during runtime
    and the final page is constructed by Cuyahoga with injecting module code and page template to a resulting page.
    Since there is only one physical page called Default.aspx (actually there some more physical pages as Error.aspx and Install.aspx)
    all page request must be handled by a custom HttpHandler and resolved so that proper page with proper sections and modules
    can be constructed in runtime.

    PageHandler class implements IHttpHandler interface and IRequiresSessionState marker interface.
    PageHandler utilizes Cuyahoga'a custom UrlRewriter class which is used to rewrite requested URLs. UrlRewriter produces
    URLs that are meaningful for the framework and used for building the right reult page. But unfortunatelly http requests
    caused by AJAX calls can not be handled properly by PageHandler because Cuyahoga's UrlRewriter can not rewrite the right
    URL for AJAX calls which in turn results in reource not found exception thrown by the handler. To overcomde this problem
    we have to slightly modify PageHandler class's ProcessRequest function. Here is the code

     
    
    
    public void ProcessRequest(HttpContext context)
    {
        string rawUrl = context.Request.RawUrl;
        log.Info("Starting request for " + rawUrl);
        DateTime startTime = DateTime.Now; 
    
    
        string aspxPagePath = String.Empty;
        // Rewrite url
        UrlRewriter urlRewriter = new UrlRewriter(context);
        string rewrittenUrl = urlRewriter.RewriteUrl(rawUrl); 
    
    
        
        #region //Ali Ozgur: This is an ajax request, so we have to realign the rewritten url.
        if (context.Request["HTTP_X_MICROSOFTAJAX"] != null)
        {
            int idx = rewrittenUrl.ToLowerInvariant().IndexOf("/default.aspx");
            if (idx >= 0)
            {
                rewrittenUrl = rewrittenUrl.Substring(idx, rewrittenUrl.Length - idx);
            }
        }
        #endregion
        
        
        // Obtain the handler for the current page
        aspxPagePath = rewrittenUrl.Substring(0, rewrittenUrl.IndexOf(".aspx") + 5);
      
        IHttpHandler handler = PageParser.GetCompiledPageInstance(aspxPagePath, null, context); 
    
    
        // Process the page just like any other aspx page
        handler.ProcessRequest(context); 
    
    
        // Release loaded modules. These modules are added to the HttpContext.Items collection by the ModuleLoader.
        ReleaseModules(); 
    
    
        // Log duration
        TimeSpan duration = DateTime.Now - startTime;
        log.Info(String.Format("Request finshed. Total duration: {0} ms.", duration.Milliseconds));
    } 
    
    
    

    Installation

    We have to modify our Web.config file to enable Ajax support. If we do not add the following configuration information it is likely that
    we will get "Sys not defined" error when our module tries to execute Ajax related code.

    
    
    <system.web > 
     <httpHandlers > 
      <remove verb="*"  path="*.asmx" /> 
      <add verb="*"  path=" Error.aspx"  type=" System.Web.UI.PageHandlerFactory"  /> 
      <add verb="*"  path=" *.aspx"  type=" Cuyahoga.Web.HttpHandlers.PageHandler, Cuyahoga.Web"  /> 
      <add verb="*"  path=" *.asmx"  validate=" false"  
          type=" System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
      <add verb="*"  path=" *_AppService.axd"  validate=" false"  
         type=" System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
      <add verb=" GET,HEAD"  path=" ScriptResource.axd"  type=" System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  validate=" false" /> 
     </httpHandlers> 
     <httpModules> 
      <add type=" Cuyahoga.Web.HttpModules.AuthenticationModule, Cuyahoga.Web"  
        name=" AuthenticationModule"  /> 
      <add type=" Cuyahoga.Web.HttpModules.CoreRepositoryModule, Cuyahoga.Web"  
        name=" CoreRepositoryModule"  /> 
      <add name=" NHibernateSessionWebModule"  
       type=" Castle.Facilities.NHibernateIntegration.Components.SessionWebModule, Castle.Facilities.NHibernateIntegration"  /> 
                
      <!--Ajax toolkit support--> 
      <add name=" ScriptModule"  
       type=" System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
    
    
    </httpModules> 
    </system.web>  
    
    
    

    History

    18 February 2008: Initiali version published


    Posted in: .NET Development , CodeProject , Cuyahoga , NHibernate  Tags:

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5