Code 4004

Edit; 26th May 2010; This is my most favorite post! Check out related Silverlight posts here…

Just getting started in Silverlight 4 WCF RIA Services (which is astonishing, by the way – check out Brad Abram's walkthrough here) but I was getting stuck at the validation part.

When I exercise the validation (you know, red bits in the browser because you're failing metadata requirements) and SubmitChanges, it was throwing up a horrible error;

An unhandled exception ('Unhandled Error in Silverlight Application

Code: 4004

Category: ManagedRunTimError

Message: System.ServiceModel.DomainServices.Client.DomainException: And error occurred while submitting changes…

The only funny bit about this was the typo; "And error occurred"… The dialog is shown here;

4004

 

OK – I got an exception, let's debug… So, I click on "OK" and get the message "Unable to attach to the crashing process. A debugger is already attached"

already attached

Which it is, of course. The one question I have through this is why the heck does the running debugger not catch this?

OK, so let's re-run and this time, detach the debugger by going to the Debug menu and select "Detach all". Then I made it crash by entering an invalid value and trying to save again and, this time, I get this;

decent error

At least now I know what the issue is. Notice that, no surprise, the error text still begins "And error occurred…"

 

So here's what I learned;

  1. The debugger does not appear to catch all exceptions
  2. I need to handle the validation error situation properly in the _SubmittedChanges event

This all comes down to two issues. One, you need to explicitly check whether the _SubmittedChanges() succeeded and 2) Visual Studio explodes if you don't do this.

OK, let's fix it

The trick is to let poor old SilverLight know that you're happy with the validation exceptions being handled in the framework.

Right-click the datasource, go to the events pane in properties, and add an event handler for "SubmittedChanges".

If you're UI-averse, then add the event handler manually in the XAML;

 <riaControls:DomainDataSource AutoLoad="True" LoadedData="machineDomainDataSource_LoadedData" Name="machineDomainDataSource" QueryName="GetMachinesQuery" Width="0" SubmittedChanges="machineDomainDataSource_SubmittedChanges">

The key is the SubmittedChanges tag that calls machineDomainDataSource_SubmittedChanges when we submit changes.

Looking at the machineDomainDataSource_SubmittedChanges method, all we need is;

        private void machineDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            if (e.HasError)
            {
                System.Diagnostics.Debug.WriteLine(e.Error.ToString());
                e.MarkErrorAsHandled();
            }
        }

 

The [ System.Diagnostics.Debug.WriteLine(e.Error.ToString()); ] is completely optional. The absolute killer point is recognising that we have an error and then telling Silverlight that we handled it. Without this code, an exception is thrown.

With the code in place then validation happens as you would expect, within the browser and the application continues as if nothing happened.

I've no idea why the standard VS2010 Debugger doesn't catch that exception but at least we know what it is, now.

Edit; 26th May 2010; This is my most favorite post! Check out related Silverlight posts here…

8 comments to Code 4004 “Unhandled error in silverlight application”

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">