Argh! AddBusinessRules firing inconsistently!

Argh! AddBusinessRules firing inconsistently!

Old forum URL:

david.wendelken posted on Tuesday, January 08, 2008

I'm really confused!

I have a test web page that I'm running.  The usual techniques, session cache for my business objects, etc.

I'm tracking down the assignment of business rules and I'm getting some really strange behavior!

Sometimes, when I run my web app in debug mode, it fires AddBusinessRules for the two business object collections being loaded up on the page and sometimes it doesn't.

I've shut down IIS, it doesn't matter.  Sometimes it fires AddBusinessRules, sometimes it doesn't.

I've changed code in AddBusinessRules() in one of the classes and recompiled, then debugged again.  Same problem.

It should fire once per class that is derived from BusinessBase, and it doesn't always do that.

Anyone got a clue?

boo replied on Tuesday, January 08, 2008

I could pretty much guarantee you it's not the business object (at least the core CSLA part).  Some typical things I've seen done is:

1) Not using IsPostBack checks in PageLoad or other methods where code should only run once the page is initially loaded.

2) Objects being put into session multiple times.

3) Persistent references in pages causes the page not to get garbage collected (for instance unwiring event handlers to static classes)

4) Methods in OnItemDataBound or OnItemCreated not checking for the item type (so thus maybe an object gets created on a header item and a footer item).

Those might be some things to check, but without seeing code to the page and the business object, it would be hard to say anything futher.

david.wendelken replied on Wednesday, January 09, 2008


3) Persistent references in pages causes the page not to get garbage collected (for instance unwiring event handlers to static classes)

Could you expand upon this a bit?  I don't quite understand.

boo replied on Wednesday, January 09, 2008

Sure, but one quick thing...if you use these business objects in a Console app do they perform normally?  This will right away tell you if it's a BO problem or how the BO is being used in ASP project.

Take the following:

public static class Test()
    public event SomeEvent;

    public void RaiseSomeEvent()
        if(SomeEvent != null)
             this.SomeEvent(this, EventArgs.Empty);

Now if in a web page_init you have something like (say the page is TestPage.aspx):

Test.SomeEvent += new EventHandler(SomeEventMethod);

You've created a reference in that TestPage class code behind to the static TestClass.

When ASP.NET creates an instance of that page it can never fully destroy the instance after Page_Unload whenever the garbage collector gets to it because it still maintains a reference to the state Test class.  (Before that page unload you need to have code that removes the call.

So you may have this instead

private EventHandler eh = new EventHandler(SomeEventMethod);

In the page_init method:
Test.SomeEvent += eh;

And maybe in the page_unload method (or probably the page_prerender, because if this event actually fired after the page was rendered it wouldn't do you much good in some cases):
Test.SomeEvent -= eh;

Clear as mud?

Hope you get to the root of it; that's about all I have to offer.

JoeFallon1 replied on Wednesday, January 09, 2008

In my web app the call to AddBusiness rules seems to fire at the right time. Remember it only runs once per appdomain so starting and stopping the web app itself will NOT cause it to fire again. But recycling IIS will.



Copyright (c) Marimer LLC