All posts by jim

Amplify.js & ASP.NET Web API: Getting the error message

I’m still learning my way around ASP.NET Web API and have started using Amplify.js in my web apps to consume my web services. I originally learned about Amplify.js from John Papa’s excellent Pluralsight course, “Single Page Apps with HTML5, Web API, Knockout and jQuery”.

Today I noticed that when a server-side error occurs, I was not getting the JSON response that contains the error information in my client-side Amplify error() function (see “Exception Handling in ASP.NET Web API” for an explanation). Taking a look at the Amplify source code revealed that, “out of the box”, Amplify always sends null to the error callback function’s data parameter.

To override this behavior, I created a custom decoder that would use the XHR responseText to feed the error callback data parameter, like so:

amplify.request.decoders.mydecoder =
        function (data, status, xhr, success, error) {
            if (status === "success") {
                success(data, status);
            } else if (status === "fail" || status === "error") {
                try {
                    error(JSON.parse(xhr.responseText), status);
                } catch(er) {
                    error(xhr.responseText, status);

Now I’m able to read the Message property and any other information returned from Web API when a server-side exception is thrown.

Entity Framework DbContext and mapping decimal properties with more than 2 digits of scale

Posting this mostly as a reminder to myself that when more than two decimal places of scale (i.e. to the right of the decimal point) is needed when mapping an EF entity property, you must specify the property’s precision in your DbContext’s OnModelCreating event like so:

public class MyDbContext : DbContext, IDbContext
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
      modelBuilder.Entity<OutYearPoolRate>().Property(r => r.Rate).HasPrecision(14, 8);

Failure to do so causes EF to truncate the values to 2 decimal places when updating the database.

ASP.NET Wizard Control Oddness

Haven’t posted anything in over a year, but decided to break the silence to write about something that caused me to waste more time than it should have. Hopefully, it will save someone else (or future me) from wasting more time on it.

I’m developing an ASP.NET site using the Wizard control and have code that saves the user’s progress in a database. The idea here is that the user can return to the site and pick up where they left off. Simple enough, but I got caught in what seemed like a never ending debugging session when I couldn’t figure out why the "Previous" button didn’t work when I came back into the site to resume a previous session. The wizard simply would not step backwards when starting on any step in the middle. To make a long story short, turns out the genius at Microsoft who implemented this control decided to make the "previous" button work like the browser "back" button, i.e. clicking previous steps you back in your "history" since you starting using the wizard. In this case, there is no "history" when starting in the middle, so the button does nothing! Not at all intuitive.

I found this discussion enlightening:

The workaround is simple. Handle the PrevousButtonClick event and decrement the ActiveStepIndex property:

protected void MainWizard_PreviousButtonClick(object sender, WizardNavigationEventArgs e) { MainWizard.ActiveStepIndex--; }

Hope this helps.

Flash Player 10 Causing Internet Explorer Hang

Some folks (including me) are experiencing problems with version 10 of the Adobe Flash player and Internet Explorer. Oddly enough, I didn’t seem to have any trouble until after this month’s “patch Tuesday” (Microsoft updates).

For now, I’ve downgraded Flash to version 9 to workaround the problem. Adobe has older versions of Flash available for download here:

Vista Sleep Problem Solved?

I decided to check on the state of Vista fixes last night by installing the latest updates from Windows Update and the latest NVidia drivers.  Unfortunately, my system would still hang when waking from its second slumber.  These kinds of sleep problems with Vista are widely reported on blogs and discussion forums all over the Internet, so I thought I’d post about what appears to fix the problem for me.

I don’t know what made me think to do this, and I think I’ve tried this in the past, but I disabled hybrid sleep (Control Panel, Power Options, Change plan settings, Change advanced power settings, expand Sleep, expand Allow hybrid sleep, change setting to Off, click OK).  Now my PC wakes from sleep every time!

Hybrid sleep is new in Windows Vista and is a cross between the old "standby" mode and "hibernation".  Standby works by powering down just about everything except the RAM, which saves the machine’s state so that it can very quickly resume where you left off when you power it up again.  Hibernation saves the computer’s state to the hard drive and powers down completely.  Resuming from standby is many times faster than resuming from hibernation, since loading the state back from the hard drive takes some time.  Windows Vista’s hybrid sleep mode saves the state to the hard disk but then goes into standby mode, keeping the state in RAM as well.  The idea here is that the state is protected in case the power is lost to the PC (which would wipe out the state in RAM), but most of the time the machine wakes up quickly by using the state stored in RAM.

I don’t know what it is about my hardware that is preventing a wake from hybrid sleep and not "plain old standby" mode.  I have a UPS connected to this machine, so I’m not particularly concerned about it losing power while it’s sleeping.  XP only supports standby and hibernation and that had been working without and problems, so that may be what finally made me think to disable hybrid sleep. 

So now I think I’m going to give Vista another chance.  At least until I run into the next show stopper…