Getting better performance in web apps with a few lines of script
Saturday, June 27, 2009 8:53 PM

We had a situation where a web application was utilising near 100% of CPU all the time under light load and it was proving very hard to reduce this significantly through code optimisation. We were performance testing and profiling, but only seeing marginal gains.

The infrastructure that was hosting the web application was as follows:

  • Virtual Server (Hosted via VMWare Server) running Windows Server 2003 64 bit
  • .Net 3.5
  • Dual Virtual CPUs
  • 2 Gb of memory.

Now I am not a big fan of virtualisation for hosting high performance web sites but we were stuck with this. At any rate, we had consistently high CPU utilisation for seemingly no real reason. Some of the most simple requests were generating high CPU usage.

After some investigation, and also the help of a friend and very smart dude, Scott Forsyth (who works for Orcsweb a high profile web hosting company) we decided to try setting Internet Information Services to 32 bit mode to see if any difference was seen.

To cut a long story short, we changed Internet Information Services to run in 32 bit mode. This simple switch, yielded significant lower CPU utilisation and in some case, almost cut the CPU utilisation in half!

Why would this be so? Well, it turns out there are a few reasons for this. Probably the primary reason is the fact the we get less information within the level 1 cache of the processor because of the increased structure size (pointers are bigger) within 64 bit systems. The same information in a 64 bit system occupies more memory than its 32 bit counterpart, therefore we can fit less into the processor cache. More information on this can be found here.

Its really easy to change IIS to 32 bit mode. The steps are:

  1. To enable 32 bit mode on IIS in 64 bit windows, at a command prompt type:
    cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1
  2. Once this is done, 32 bit mode is enabled but we need to ensure that the 32 bit DLL’s are used as IIS will still have the 64 bit DLL’s defined for the ISAPI handler. To do this, all we have to do is re-register ASP.NET. At a command prompt, simply type:
    %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe –i
  3. Finally, just make sure the status ASP.NET version 2.0.50727 (32-bit)  in the web services extension, within IIS, is set to allowed.
    (More information on this process can be found here http://support.microsoft.com/kb/894435)

And that’s it. A few lines of script, and we had effectively reduced CPU utilisation by somewhere in between 30% and 50%!

Conclusion

So my rule is this, If you are:

  • Running Windows server in 64 bit mode.
  • Running a standard .Net web application
  • Do not need a large memory address space (ie. over 4 gigabytes)
  • Have no special 64 bit optimisations

Then this change will yield immediate performance improvements in your application, and in some cases (dependent on what your application does), yield significant performance improvements.

by Glav | 9 comment(s)
Filed under: , , ,
FromTheGrey – Arts Site
Sunday, May 10, 2009 1:08 PM

Recently, I have been working on creating an arts based website called “FromTheGrey” at http://FromTheGrey.com

Its a site devoted to showcasing the pictures and artwork created by my wife Michele. Previously, Michele has been very reluctant to showcase these pieces, but I really like them, as do many others I know, so it took some time and coaxing to convince Michele to show them to the world.

However, I finally managed to do. As a result, “FromTheGrey” was born. Its a relatively simple site but I hope you like the art on the site and if so, would really appreciate you recommending it to others.

Its currently simple but functional, and will have many more pieces of work from Michele on there, as well as enhanced styling and functionality.

For now though, we would love your feedback which can be submitted via the Contact Form on the site.

For the technically inclined, it utilises ASP.NET 3.5 Webforms, jQuery 1.3.2, and jQuery plugins for the dialog box and gallery effects, Thickbox and Galleria respectively.

by Glav | with no comments
AjaxView – Javascript profiling
Sunday, May 10, 2009 11:25 AM

A little while ago, I posted about a tool called AjaxView to allow you to profile javascript code within your web applications.

Well, I was contacted by Adam who is working on this tool about its latest incarnation. It appears had a lot of work done to it, and now looks quite slick and is really nice to use. Previously, it was quite raw in use and also with the metrics its provides. Now, it acts very similar to one of my favourite profilers, Ants Profiler.

What does it do?

This tool injects profiling code into your javascript that enables it to analyse your javascript code and provide accurate metrics around all the operations being performed, including anonymous functions. It then provides these metrics to the Visual Studio performance tools for you to view in a very simple and familiar manner.

How do you use it?

You need to be using IIS7 in Integrated mode for this tool to work. A full set of install instructions is listed here along with the installation files. It installs pretty easily.

What do you get?

In short, you get detailed metrics on each page and javascript file in your site. When access a site with the AjaxView profile tools installed, all metric information is automatically being recorded. You can then access a web site on your local machine that is installed when AjaxView is installed. (Something like http://localhost/AjaxView).

When you do that, you can view the session profiles you have created (or the default one that is created for you on install). Something like this:

image

Select any of the files on the right which constitute all the files access by your web app (either a page or javascript file). Select the ‘Get profile data’ button and it launches Visual Studio and provides the profile metric data so that you can view it within Visual Studio. You end up getting access to detailed multiple views like the ones shown below:

image

image

image

All in all, a pretty useful tool to look at your Javascript performance. I recommend you take a look. Get it from here.

Regaining results from a performance test run
Wednesday, April 29, 2009 11:48 PM

I have been doing a lot of performance testing lately, and one of the worst things that can happen during a performance run, is when you don’t get any results. Performance testing is expensive, from a time and resources perspective, so getting results from a run is paramount.

In my scenario, I have everything automated to kick off a performance run, at 7.00pm every night, run for 12 hours, then terminate. I collect metrics via Visual Studio Team Suite which is used to drive my performance tests, but I also have automated the collection of performance metrics on all related servers (web, sql etc…)

On one of my recent runs, I came into the office in the morning, and checked on the test rig. The test appeared to be still running, even well beyond the termination time. VSTS reported that it was trying to abort the test, but things were still running. Some of the test agents refused to respond.

I pressed the stop button to stop the test in VSTS and while I was waiting for that, loaded up the test results so far. The business were eagerly awaiting the results of the tests and it looked promising so far.

Visual Studio began to refuse to respond after a while and was continuing to try and stop the test. After a long wait, I simply killed off the devenv.exe process, and restarted the Visual Studio Test controller service.

I then went to load my test data from the run. Oh dear, the test run that I was looking at not more than half an hour ago, did not appear. Nothing listed on the ‘Completed Tests’ tab, and nothing within the ‘Manage Test Results’ option either.

Crap. A business person walks by and asks how the test run went. “Still analysing the data” was my response. Double crap. What to do.

After looking at the SQL database where the load results are stored, it was apparent there was a bunch of data in there, that simply was not showing. Looking through the raw data did not appeal to me very much.

I had a look in the ‘LoadTest’ table in the LoadTest database I was using to see if there was any data.

image

I noticed there was no ‘End Time’ defined for my run. It was simply set to NULL

image

. The Start Date was correct, so I simply copied the StartTime to the EndTime column, changed the date to the next day, and set the time to AM instead of PM (thus setting my 12 hour run).

image

And now I have normal access to all my test results. Whew…. crisis averted.

Hope this helps someone.

by Glav | with no comments
Animated SiteMap Ajax Control
Thursday, February 12, 2009 9:18 PM

Today, I finally released on Codeplex a control that I had been working on over a year ago, called the Animated SIteMap Control. Its an AJAX Control toolkit control and it has been prototype form for a very long time now.

I had planned on releasing it into the AJAX Control toolkit but I got sidetracked and haven’t touched this in over a year. It works fine and probably needs a little refinement but has configurable animation speeds, full support for CSS styling and various other options.

Basically, it creates an ‘exploding’ like circular animation of the nodes defined in your web.sitemap file. You can drill down into nodes that have subnodes, or step back from the selected node, and the nodes of the same level are all animated in a circular fashion around the main or home node.

You can view a very small and brief example of this here: http://www.theglavs.com/ControlTestPage.aspx. Feel free to download it and use it if you like it. Hopefully, you may get the time to contribute to it and make it better. I know my time is short and I just didn’t want to waste it. Perhaps even a jQuery port might come of it.

At any rate, if you end up using it, changing it or whatever, I would love to know. Hope you like it.

EOAST #4 – Evolution of a software thingy Part 4
Monday, January 26, 2009 11:45 AM

So its been ages since my last post on this pet project of mine. Its time to get down to the nitty gritty on this and see if it is actually working the way I want it to. Hopefully, you have read parts one, two and three in previous posts to get a good idea on what I am trying to achieve.

Basically, its a collect, store and display mechanism to store almost any information in a SQL database. You only need concentrate on implementing collection logic, or only display logic or both if you so choose. Its up to you. The storage and querying of this information is done for you, as is the scheduling of collection. Additionally, its gotta be real robust. If someone writes a crappy information provider, the system should not die. Also, I want to be able to map an information provider to one or more display providers, all easily.

I have dubbed this the InformationAggregator.

So I am happy to say its actually working reasonably well at the moment. Since its a pet project of mine, the fact it is actually up to a reasonable level of functionality is quite an achievement. Normally I kinda let things go once I have satisfied my initial curiosity, or whatever it was that got me interested in doing it. What I haven’t really spent much time with is the user interface. Its really just a set of buttons on a form to exercise the API.

At any rate, the basic details are as follows.

There are 2 main assemblies: InformationAggregator.Core.dll and Information.Aggregator.Engine.dll.

If you want to implement an information collection provider or an information display module, you only need to reference the Core assembly. The Engine assembly contains all the functionality to start the hosting and collection engine.

Note: Since this codebase is always being revised, its prone to change so there maybe much refactoring in the future. In fact, part of it will be happening after this post ":-)

Writing an Information Collection provider

Say you want to write a module which collects email, accepts user input, reads a users twitter entries, or some other information collection mechanism. You only need to Implement 2 interfaces: IPluginModule, & IInformationProvider.

 IPluginModule has only 1 method,

QueryPluginResponse QueryPluginModule()

which acts to provide some metadata around itself. Here is an example:

public QueryPluginResponse QueryPluginModule()
{
    QueryPluginResponse response = new QueryPluginResponse();
    response.MajorVersion = 1;
    response.MinorVersion = 0;
    response.PluginDescription = "Test Manual Collection. Only sends information to engine once collected from keyboard entry";
    response.PluginName = "ManualCollector";
    response.Publisher = "Glav";
    response.PublisherLink = "
http://www.theglavs.com";
    response.RevisionNumber = 0;
    response.SupportsCollectionNotification = false;
    response.TypeOfService = ServiceType.InformationProvider;

    return response;
}

IInformationProvider also requires the implementation of only 1 method,

List<GenericInformationItem> GetAllInformation()

Here is an example:

public List<GenericInformationItem> GetAllInformation()
{
    List<GenericInformationItem> list = new List<GenericInformationItem>();

    Random rnd = new Random(DateTime.Now.Millisecond);
    for (int i = 0; i < 3; i++)
    {
        GenericInformationItem item = new GenericInformationItem();
        item.Author = "Paul Glavich";

        item.BodyTextData = string.Format("This is dummy body entry ID:# {0}. The current date is {1} and the time is {2}", rnd.Next(1,200),DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
        item.InformationSource = "Unit Test";
        item.ReceivedDateTime = DateTime.Now;
        item.Originator = "TestInformationProvider";
        item.PublishDate = DateTime.Now;
        item.SourceProvider = PROVIDER_NAME;
        item.Summary = "Just a test item";

        item.Title = string.Format("Test Item #{0}", i);
        list.Add(item);
    }

    return list;
}

This implementation simply sends 3 items of information to the InformationAggregator engine when its asked to do so. This particular implementation is well suited to periodic, or scheduled collection. The engine can be asked to collect information from modules every minute, hour, day, week etc.

image

The engine also supports manual collection where it does not schedule collection at specific intervals, but waits for the provider to send it information. A third option is where the engine instructs the module to begin collection, then returns to its business, allowing that module to take its sweet time about collection (it may be very time consuming) and then send it the collected items. Status events of the collection process is also supported.

Note that the implementation is very simple. It would be easy to write some logic to query some RSS feeds, twitter feeds, or whatever. All the scheduling and storage is performed via the engine through configuration. Currently I have a very crude user interface (and a I mean very crude) to control all this. Basically some buttons to exercise the functionality of the API:

image

Only collection modules that have been registered, have their items accepted and stored via the engine in the database. When registering, the IQueryModule interface is invoked to gather and display some metadata to display for confirmation when registering:

image

 

 

Writing an Information Display provider

To display information, again we implement only 2 interfaces: IPluginModule, & IInformationDiplay.

IPluginModule was already discussed above and is a pre-requisite for any module, either display or collection.

IInformationDiplay has 2 methods to implement:

void PushInformation(List<GenericInformationItem> infoItems );

void PushStatus(QueryPluginResponse plugin, int percentCollectionCompleted, CollectionStatus statusIndicator, string statusMessage);

The ‘PushInformation’ method has the information ‘pushed’ to it, and you can do what you want with the info from there. (Note: Not really happy with these method names, but I’ll leave them for now)

The ‘PushStatus’ method gets called repeatedly during the collection phase, if the information provider is providing this information.

The display provider can be a simply console app, or more typically, a WPF application. These all need to be registered as described above.

image

However, one more pre-requisite is required if its a WPF application. A parameterised constructor must be provided that accepts the WPF window to be used for display. The engine will search the WPF application for this constructor and construct an instance of this window when creating this module, and maintain this, so that display items can be sent to the module, and threading affinity can be easily achieved.

Linking providers to display modules.

So we can easily create an information collection provider. We can also easily create a display provider. How do we get information from collection provider to display provider. We simply map one or more collection modules to a display provider. Here is an example of the (again very crude) UI to do this:

image

This UI shows 2 information providers, ManualCollector and TestInformationProvider. Currently, only the WPFDisplayProvider#2 is mapped to the TestInformationProvider, with the BalloonPopup display provider available, but not mapped to this module. This means that any information that the TestInformationProvider collects, is currently automatically sent to the WPFDisplayProvider#2. If we want that information to be sent to 2 display providers, we can simply click on the BalloonPopup display provider and move it into the mapped display providers.

image

Now when the engine starts, all information collected by the TestInformationProvider is automatically sent to both the WPFDisplayProvider#2 and the BalloonPopup display provider. Basically you can pick and choose how your information is collected, and how it is displayed.

So thats the concept. Control the collection of your information, and choose a display method that matches what you want. Additionally, have the information stored in an easily queryable store.

There is actually much more, but I’ll leave it for now. Next step will be a demo, so look for a short video on how it works and specifically the WPF implementation of the display providers.

by Glav | 1 comment(s)
Filed under: , , , ,
Some interesting links for ASP.NET
Friday, January 09, 2009 4:29 PM

Its been a while since I have done any technical stuff as I have been on holidays, relaxing, and generally a lot of nothing. Certainly nothing technical related and have really enjoyed spending time with friends and family.

Over this period, Wally has produced some good material around ASP.NET which I feel obligated to share with you….. (ok, so maybe Wally coerced me a bit….)

Firstly, Wally has done a new ASP.NET Podcast:

ASP.NET Podcast Show #134 - General Thoughts on Windows Azure - Audio only

Some good discussion around Cloud computing and Windows Azure in there.

Also, Wally has produced an e-Book on some of the ASP.NET 3.5 SP1 features and its located here.

Go check em out. Make Wally a happy Wally.

New ASP.NET Podcast on jQuery support
Saturday, December 06, 2008 3:08 PM

Wally has done another good podcast which can be found here on jQuery support in Visual Studio 2008 and how it compares to ASP.NET AJAX by way of some simple examples.

jQuery is a great AJAX library and well worth investing some time in getting familiar with. In fact, you can start right now by listening to the podcast :-)

Debugging Silverlight apps using WinDbg
Wednesday, December 03, 2008 10:29 AM

While working on a silverlight application for my current engagement, we were seeing some weird memory issues and application crashes. I won't bore you with the initial investigation of the issue, but it turns out we need to delve deep into what was going on behind the scenes. A fellow colleague and friend, Miguel Madero, was also encountering similar issues. He also did some work in debugging the issues he was seeing,  and because they were similar to mine, I had a little headstart in where to look. Note: I know that Miguel will also be publishing some debugging tips around Silverlight and he is a Silverlight gun so keep an eye on his blog for more details.

At any rate, our silverlight apps, after opening certain pages many times (> 8), the application would crash. No real information why, and the Visual Studio debugger wasn't helping a lot as the exception it was faulting on was just the final exception when things got too much.

In Visual Studio, turning on Catch all first chance exceptions wasn't helping either as there were too many other first chance exceptions that were confusing things (and yes, many framework related ones).

So, using WinDbg, we decided to dig deeper. The rest of this post shows how we debugged some silverlight issues using WinDbg using some general examples, and a few specific ones.

Getting Started.

You can create crash dumps to examine using a tool like adplus (part of the debugging tools) which I did, but I also prefer to attach to the running process itself where possible so I shall use that here.

1. Loaded up WinDbg

2. Loaded the SOS module for silverlight which is the managed debugging extension.

image

3. Set the symbol path to a local directory on my system as well as the microsoft symbol servers when the tool needs to download/resolve symbols.

You can do this via the command line

image

or via the user interface

image

4. Fire up your SIlverlight application in a standard Internet Explorer browser window. Do not invoke debugging within Visual Studio (ie. press the 'Play' button or hit 'F5' to start the app) as this will attach its own debugger. Then attach WinDbg to the 'iexplore.exe' process.

image

image

5. Now we are ready to begin actual debugging. Typing 'g' into the command line gets the debugee running, and WinDbg monitors the application for first chance exceptions.

image

One thing that is not so apparent within Silverlight, particularly when using the MVVM (Model-View-ViewModel) pattern which typically uses DataBinding extensively, is when data binding errors occur. The framework tends to hide these little gems away, and sometimes, it just appears like its not working. Note that these will also appear in the Visual Studio debug output window but I find it actually easier to have the WinDbg window running side by side with the browser to watch all exceptions as they occur.

By way of example, lets imagine the view model has this property, SelectedThing.

image

Now lets also imagine we have a ComboBox databound to this using the following XAML.

<ComboBox x:Name="DropList" Grid.Row="1" Height="30"  ItemsSource="{Binding SomeData}" DisplayMemberPath="Name" SelectedIndex="{Binding SelectedThing, Mode=TwoWay}" />

Finally, when we fill up/gather our data, we set the SelectedThing to an initial value:

SelectedThing = SomeData[0];

The astute among you can probably already see where the error is, but lets persist for the sake of example. We load the app and see that the ComboBox is not having its selected item bound to anything.

image

At this stage, no errors are prevalent, other than we have no SelectedItem in the ComboBox. Hpwever, looking at the WinDbg window, shows us that there were some exceptions occurring.

image

Specifically, we can see that an exception occurred with a BindingExpression, in which a converter failed to convert a value for 'SomeItem'. The entire message is shown below, and its pretty verbose about what failed.

(Again, this is the same output as the Visual Studio debug output window)

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'TestSLCrap.SomeItem' (type 'TestSLCrap.SomeItem'); BindingExpression: Path='SelectedThing' DataItem='TestSLCrap.ViewModel' (HashCode=64479624); target element is 'System.Windows.Controls.ComboBox' (Name='DropList'); target property is 'SelectedIndex' (type 'System.Int32').. System.InvalidOperationException: Can't convert type TestSLCrap.SomeItem to type System.Int32.
   at MS.Internal.Data.DefaultValueConverter.Create(Type sourceType, Type targetType, Boolean targetToSource)
   at MS.Internal.Data.DynamicValueConverter.EnsureConverter(Type sourceType, Type targetType)
   at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertToTarget(Object value).

It tells us the value to convert, the binding path and the property. Pretty easy to fix. In the XAML we simply change this:

SelectedIndex="{Binding SelectedThing, Mode=TwoWay}"

to this:

SelectedItem="{Binding SelectedThing, Mode=TwoWay}"

Again, pretty simple and actually an easy error to do if you are going fast and accepting the Intellisense suggested property without really looking. To bring this back to my problematic silverlight app I mentioned, while debugging I found numerous instances of binding issues where properties were NULL when not expected and a few others. Eliminating these certainly made the app run better but it was still crashing.

So, I gotta dig deeper. I had a look at some of the exceptions on the heap.

0:005> !dumpheap -type Exception
Address MT Size
03a21024 038c72d0 72
03a2106c 038c7398 72
03a210b4 038c7458 72
03a210fc 038c750c 72
03a21144 038c750c 72
03a24edc 060a5668 32
03a25a4c 060d51e4 32
03a25a6c 060d52c0 32
03a2dac4 060dd240 32
03a3d254 0321f48c 12
03a3d26c 0321f510 12
total 11 objects
Statistics:
MT Count TotalSize Class Name
0321f510 1 12 System.Text.DecoderExceptionFallback
0321f48c 1 12 System.Text.EncoderExceptionFallback
060dd240 1 32 System.EventHandler`1[[System.Windows.ApplicationUnhandledExceptionEventArgs, System.Windows]]
060d52c0 1 32 MS.Internal.Error+GetExceptionTextDelegate
060d51e4 1 32 MS.Internal.Error+ClearExceptionDelegate
060a5668 1 32 System.UnhandledExceptionEventHandler
038c7458 1 72 System.ExecutionEngineException
038c7398 1 72 System.StackOverflowException
038c72d0 1 72 System.OutOfMemoryException
038c750c 2 144 System.Threading.ThreadAbortException
Total 11 objects

Hmmm, not too much interesting here as these exceptions. Lets dump the current stack objects to see what that yields:

0:005> !dso
OS Thread Id: 0xa54 (5)
ESP/REG  Object   Name
0179E85C 04084278 Views.DataDisplayView
0179E880 03a82684 System.Windows.CoreDependencyProperty
0179E884 04084278 Views.DataDisplayView
0179E89C 04084278 Views.DataDisplayView
0179E8BC 03a82684 System.Windows.CoreDependencyProperty
0179E8C0 04084278 Views.DataDisplayView
0179E8CC 0450f6d8 System.Windows.Controls.RadioButton
0179E8E0 03a82684 System.Windows.CoreDependencyProperty
0179E8E4 04084278 Views.DataDisplayView
0179E8F0 03a82684 System.Windows.CoreDependencyProperty
0179E8F4 04084278 Views.DataDisplayView
0179E904 04084278 Views.DataDisplayView
0179E910 04084278 Views.DataDisplayView
0179E914 04084278 Views.DataDisplayView
0179E918 04084278 Views.DataDisplayView
0179E91C 04088ca0 System.Windows.Controls.RadioButton
0179E938 04088ca0 System.Windows.Controls.RadioButton

. . . majority of listing omitted for brevity

0179E980 0450f6d8 System.Windows.Controls.RadioButton
0179E990 0455641c System.Windows.RoutedEventArgs
0179E994 0450f6d8 System.Windows.Controls.RadioButton
0179F7C4 03a6f9d0 Ninject.Core.Injection.DynamicMethodInjector
0179F7C8 0425fcd8 System.Object[]    (System.Object[])
179F9BC 03a92720 PresentationLayer.UI.UserControls.MenuItem
0179F9E4 03a92c00 System.Windows.Input.MouseButtonEventHandler
0179FA04 03a92720 PresentationLayer.UI.UserControls.MenuItem
0179FA08 0425f898 System.Windows.Input.MouseButtonEventArgs
0179FA44 0425f87c System.String    M@21
0179FB24 03a2526c MS.Internal.FrameworkCallbacks+FireEventDelegate
0179FBC8 03a2526c MS.Internal.FrameworkCallbacks+FireEventDelegate

Now this is much more interesting, as you can see multiple references to Views.DataDisplayView where only 1, should be present. I poked around the objects themselves and found similar patterns. Using the 'gcroot' command, I could see what objects were 'hanging around'.

0:005> !gcroot 04084278
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Scan Thread 5 OSTHread a54
ESP:179e85c:Root:  04084278(Views.DataDisplayView)->
ESP:179e884:Root:  04084278(Views.DataDisplayView)->
ESP:179e89c:Root:  04084278(Views.DataDisplayView)->
ESP:179e8c0:Root:  04084278(Views.DataDisplayView)->
ESP:179e8e4:Root:  04084278(Views.DataDisplayView)->
ESP:179e8f4:Root:  04084278(Views.DataDisplayView)->
ESP:179e904:Root:  04084278(Views.DataDisplayView)->
ESP:179e910:Root:  04084278(Views.DataDisplayView)->
ESP:179e914:Root:  04084278(Views.DataDisplayView)->
ESP:179e918:Root:  04084278(Views.DataDisplayView)->
ESP:179e91c:Root:  04088ca0(System.Windows.Controls.RadioButton)->
  0408900c(System.Windows.DataContextChangedEventHandler)->
  04088fd0(System.Windows.Data.BindingExpression)->
  04076c74(Views.DataDisplayViewViewModel)->
  04076eb8(System.Collections.ObjectModel.ObservableCollection`1[[MyApp.DataEntities.WeekCommence,  
MyApp.DataEntities]])->
  0409dbb0(System.Collections.Specialized.NotifyCollectionChangedEventHandler)->
  04085a34(System.Windows.Controls.ComboBox)->
  04085c84(System.Windows.Controls.SelectionChangedEventHandler)->
  04084278(Views.DataDisplayView)->
ESP:179e938:Root:  04088ca0(System.Windows.Controls.RadioButton)->
  0408900c(System.Windows.DataContextChangedEventHandler)->
  04088fd0(System.Windows.Data.BindingExpression)->
  04076c74(Views.DataDisplayViewViewModel)->
  04076eb8(System.Collections.ObjectModel.ObservableCollection`1[[MyApp.DataEntities.WeekCommence,  
MyApp.DataEntities]])->
  0409dbb0(System.Collections.Specialized.NotifyCollectionChangedEventHandler)->
  04085a34(System.Windows.Controls.ComboBox)->
  04085c84(System.Windows.Controls.SelectionChangedEventHandler)->
  04084278(Views.DataDisplayView)->
Scan Thread 13 OSTHread 16b4
Scan Thread 14 OSTHread 1274
Scan Thread 15 OSTHread 15a8
Scan Thread 16 OSTHread 1330

I could see many instances of references to properties of type ObservableCollection<>. Looking at the pinned/rooted objects for a view other instances of the 'Views.DataDisplayView' objects, revealed similar results, some showing many instances of references to properties that implemented ObservableCollection<>.

Obviously, something was subscribing to the collection changed events of the ObservableCollections, and simply not letting go. Whether this be the DataBinding infrastructure of Silverlight or a result of some of the dodgy code in the application, its currently hard to say as the code is quite convoluted.

I can say this however. Changing the properties listed in the WinDbg sessions from ObservableCollections to simple generic List<> types completely solved my crashing issue. Simply changing these types involved a bit of work to now manually raise the appropriate property changed events when these properties are changed, but at least my application doesn't crash.

As a result, I now keep WinDbg loaded during most of my silverlight development, with the symbol path and symbols loaded and ready to go. its easy to just attach to the iexplore.exe process, make sure its all good, and be on my way. I know that WinDbg can save all this workspace information, but for some reason, sometimes it doesn't so I generally keep it loaded and its small enough anyway.

Hope this proves informative for someone.

WCF, IErrorHandler and propagating faults
Thursday, October 16, 2008 9:12 PM

On a recent project, I have needed to set up a generic error handler just above the service layer which ensured that only known exceptions were propagated up to the client in a controlled manner. This post contains information about how to write an IErrorHandler, but more importantly, it does contain an important point at the end about its implementation that I wanted to share, as well as save for my own reference on this blog, in this wonderful inter-web thingy.

Update: The code for this article is here

Using FaultContracts is a good way of achieving this WCF, but I also wanted to be able to:

  • Catch generic CLR exceptions and propagate them to the client as a generic fault exceptions.
  • Catch some NHibernate specific exceptions (the concurrency ones in particular) and propagate these to the client as a general concurrency fault.

This was all the client needed to know, and also provided an opportunity to log any unhandled exceptions that may sneak there way up to the services. WCF provides great exception shielding by default, but we wanted a bit more control.

Enter the IErrorHandler interface that allows you to intercept any faults or exceptions before they are sent back to the client. To use this properly, you still need to define your FaultContract as required. My simple example contract is as follows:

image

So to start the implementation of my custom error handler, I define a class that  will implement an IErrorHandler as well as IServiceBehavior.

image

I do this because not only do I want to create an IErrorHandler implementation, but I need it to participate as a behavior within the WCF pipeline, that is, I want to add it in as a ServiceBehavior when I create the host so that my custom error handler implementation can be plugged in.

I first provide implementations for the IErrorHandler.

image

HandleError returning true simply means I have handled this error and don't need other error handlers to deal with it.

ProvideFault does a few things. First it checks if its a genuine FaultException. If so, it does nothing but let it pass through. If it isn't, then a FaultException<ApplicationFault> is created, and a message fault is generated from that. Then a new message is created, using the message fault is its content, and that message is assigned to the return fault parameter.

If I wanted to catch another type of exception, I might check for it something like:

if (error is NHibernate.StaleObjectException) .....

and optionally create a different type of fault. The ApplicationFault used to construct the FaultException is just a standard DataContract.

image

Implementing the IServiceBehavior is pretty easy as you see below:

image

If you wanted to, in the Validate method, you could iterate through each operation and ensure its got your required FaultContract applied like this:

image

As a nice touch, I wanted this error handler to be able to added via configuration and play nicely with the WCF Service Configuration editor. To do this, you need a class that inherits from BehaviorExtensionElement and implements 2 simple methods like this:

image

So, finally we are at the point that caught me up for a while. In the implementation of the ProvideFault method of my error handler, you need to specify an action for the returned Fault as shown below:

image

Now its important that this Action is a known action identifier that your client is expecting. In other words, if you generate a proxy (via SvcUtil) for this service and peek into the reference.cs file for the 'Action' attribute on your services fault contract, make sure they are the same. Otherwise, when your error handler generates a fault with an unknown fault action, the client wont accept it as a known fault and it will look like just a generic FaultException, not the specific FaultContract<YourType> you were after.

When I was developing this error handler, I simply fudged this value while I got on with the task of implementing the handler. I forgot about it, then spend some time wondering why my faults were not being propagated and caught properly on the client. So make sure you supply a well known action for your fault in the contract attribute and in the error handler like this:

First on your FaultContract attribute:

image

Then in your ProvideFault implementation:

image

So finally, thats it. Don't forget about matching up those actions, otherwise you'll waste your time.

Hope this has proved helpful and informative.

Update: The code for this article is here 

More Posts Next page »

This Blog

Syndication