Tuesday, January 31, 2012

Traditional option in jquery ajax calls

I've started getting into Wcf Web Api, which is really nice tech. One issue I was having though is that in my ajax calls, that used json style data parameters, arrays were ending up being null on the .Net side (both mvc and my web api service). After some investigation I found that jquery used to have a different format for array inputs in their ajax call, which they later changed. It appears microsoft is still expecting that older style. The fix is simply to add traditional: true inside of your ajax calls. I've included some code below.

$.ajax({
                type: "POST",
                url: "your url",
                data: { input1: 10, input2: 1000, array1: [1, 2] },
                dataType: "json",
                traditional: true,
                success: function (data) {
                    // do something
                },
                error: function (data) {
                    alert("failed " + data);
                }
});

Monday, January 30, 2012

Dealing with .Net Date format issues in javascript

I've started a new job doing primarily web development. One thing I've been having to do is create a last updated message along the lines of "last updated x hours ago". My current web application is using Wcf Web Api (its really cool by the way). One issue that I've run into is microsoft's chosen format for date/time. It basically looks like this /Date(xxxxxxxxxxxxx-xxxx)/. Lovely isn't it? This format is the millisecond format for displaying the date in milliseconds. Suffice it to say that standard javascript methods like getTime() don't work with this format due to the additional "-xxxx" at the end. So here's a javascript method that does the job, enjoy.

 function getFormattedLastDateString(dateInput) {
    // setup last used date
    var lastModifiedMsg;
    var milliSecondsInOneDay = 24 * 60 * 60 * 1000;
    var milliSecondsInOneHour = 1 * 60 * 60 * 1000;
    var today = new Date();
    var created = dateInput.replace(/[-]{1}[0-9]{4}/, "");
    created = created.replace(/\/Date\(/, "");
    created = created.replace(/\)\//, "");
    console.log("created: " + created);
    console.log("today: " + today.getTime());
    var daysDiff = Math.round(Math.abs(today.getTime() - created) / milliSecondsInOneDay);
    if (daysDiff <= 1) {
        var hoursDiff = Math.round(Math.abs(today.getTime() - created) / milliSecondsInOneHour);
        if (hoursDiff != 1) {
            lastModifiedMsg = "updated " + hoursDiff + " hours ago.";
        }
        else {
            lastModifiedMsg = "updated " + hoursDiff + " hour ago.";
        }
    }
    else {
        if (daysDiff != 1) {
            lastModifiedMsg = "updated " + daysDiff + " days ago.";
        }
        else {
            lastModifiedMsg = "updated " + daysDiff + " day ago.";
        }
    }
    return lastModifiedMsg;
}

Saturday, January 14, 2012

State vs Strategy Design Patterns

I had to use one of these for a project I'm working on now. At first couldn't understand what the difference was between them. I mean they look identical structurally. So I started digging a little deeper into a book I use for design patterns called Professional Asp.Net Design Patterns by Scott Miller. If I can distill the answer down a bit, to me it seems like its the difference between a static and a dynamic pattern. In the case of state you give your context class the specific instance of a State object upfront (you can do this in the constructor if you like). In the case of Strategy you decide at runtime which Strategy object your context will be using. When you break it down in this way I think it makes the difference very clear. Of course there's lots of ways to do the dynamic runtime decision for which Strategy type you will use, but I chose to use the example from the book--which is to have a factory spit out the appropriate instance depending on a Strategy type enum value. Its clean and works well.

Wednesday, December 22, 2010

Book review Silverlight 4 Business Intelligence Software

I've written a review of this book on amazon. Gist of it is that it's a solid book that focuses on BI issues mostly. It does not go into any Enterprise patterns. Here's the link

Tuesday, December 21, 2010

Really Good MVVM articles on Msdn

Many of you have probably already seen this, but if you haven't these are the best non-video articles I've seen yet on MVVM in general and with Microsoft Prism. This first article is a fantastic introduction, http://msdn.microsoft.com/en-us/library/gg405484(v=PandP.40).aspx, that is specific to MVVM and deliberately omits most of the Prism stuff to keep things clear and simple. This next article, http://msdn.microsoft.com/en-us/library/gg405494(v=PandP.40).aspx, goes deeper into the pattern and gets into specifics where the Prism library can help you implement a better MVVM. I think this quote from this chapter says it all, "The previous chapter described how to implement the basic elements of the Model-View-ViewModel (MVVM) pattern by separating your application's user interface (UI), presentation logic, and business logic into three separate classes (the view, view model, and model), implementing the interactions between those classes (through data binding, commands, and data validation interfaces), and by implementing a strategy to handle construction and wire-up."

Thursday, October 7, 2010

Silveright, Ria Services, and Windows Authentication

Obviously there's a ton of sites on the web showing you how to's for ria services and forms authentication. Unfortunately I have not found one that has complete information in regards to using silverlight, ria services and windows authentication; and ending up with a complete working web site in the end. So that's what I'm going to post about here. Having said that I did find these really good posts that talk about a lot of the core things you need to know: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/07/02/silverlight-and-wcf-ria-services-5-authentication.aspxhttp://ajdotnet.wordpress.com/2010/08/08/silverlight-and-integrated-authenticationhttp://openlightgroup.net/Blog/tabid/58/EntryId/55/RIA-Services-Windows-Authentication-amp-GetUser-Error.aspx. Check those out if you get lost from my instructions or you want to know the why and not just the how.
I'm assuming you've already got your code written and set up. If you have not refer to the links above for help on how to do that. Now let me just get to the point, you need to make sure you set the following settings correctly:


1. Open your web.config file for your ria services project and make certain you have the following entries correctly inserted within your system.web section
<authentication mode="Windows" /> 
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
<profile enabled="false" />
The first entry sets up the web site to use windows authentication as opposed to forms. The second line is needed if you will be making calls to methods like IsInRole. So that those calls will check for AD groups instead of forms based Roles. And finally the third entry is to turn off profiles, which if you're using windows authentication of course you will not use.


2. Next step is to set up Ria Services on your server. It is possible to include the ria services dll's in your project bin folder, but I prefer installing them to the system Gac of the server. To do this simply run this command from the command line: msiexec /i RIAServices.msi SERVER=true. If you want to install to the bin or need more detail go here.

3. Next check your settings on the server and confirm that your IIS server (I'm using win server 2008 and IIS 7 for this example) has windows authentication installed. If not, install the windows authentication module from the features section of the server manager tool. Next make sure you enable the module at the web site level from your IIS administrator console. Note afterwards you may need to reboot or restart IIS. More detail on how to perform these steps can be found here.


4. Now here's the final issue and it's fix. The default requirement for wcf services is to use anonymous authentication. But the problem is that you cannot simultaneously enable both anonymous and windows authentication for this type of site. However it turns out you only need the anonymous authentication in order to provide the mex metadata information to clients that want to access your web service's metadata info so that they can create a proxy. But if you're using Visual Studio it will still be able to create your proxy without it, as explained here. So turn off your mex endpoint by commenting it out or removing it from the services section of your web.config. Also make sure you make the httpTransport node's authentication scheme's value Negotiate like this 
<httpTransport authenticationScheme="Negotiation" />
(this can be found in the customBinding section of your web.config file.


And that's it! Good luck.