Friday 8 July 2011

Visual Studio Project is complied even though I've set it not to complie

I have a solution in Visual Studio 2010 with numerous projects and 1 of these projects must not be compiled when building the solution. I know you can set this in the Configuration Manager by un-ticking the Build checkbox.

But, there is another place as well that I over looked when working with a website project.

Right click on the website project
Select Property Pages > Build
Un-tick the Build solution action option Build Web site as part of solution


Simple enough but easily over looked.

Nant script error in TeamCity - SGEN An attempt was made to load an assembly with an incorrect format

I'm using TeamCity to pull from a Mecurial repository and then execute a Nant script to build a Visual Studio solution file. The compilation fails with the following error:

"SGEN An attempt was made to load an assembly with an incorrect format"

I read various posts while using Google to find a solution. One option was to change the Generate Serialization Assembly flag to Off in the project properties build section. This did stop the error for that particular dll but it cropped up again in another project.

This was not the best solution because we are just hiding the actual error and SGEN is needed to improve performance on first run by serializing the dll.

So after a lot of head scratching I opened up the TeamCity Visual Studio solution and had a look at the Configuration Manager for my Release build. Sure enough the configuration differed in my dev Release build which targeted Any CPU and on the TeamCity server it targeted Mixed Platforms.

Resolution:

  1. Open up your dev solution
  2. Replace all lines that match this regular expression ^.*|Mixed Platforms.*$ with nothing - I use NotePad++ for this
  3. Save the solution
  4. Commit and then Push to Mecurial
  5. Run the TeamCity build once again

The reason I'm hacking the actual solution file and not changing it in Visual Studio is because there is some UI error when editing in VS that keeps the Mixed Platforms line in the solution file. BY hacking the actual file we avoid this problem.

Tuesday 5 July 2011

Monday 4 July 2011

Syntax highlighting in Blogger

I have started using this syntax highlighter on Blogger! It's great!

SyntaxHighlighter

How to use SyntaxHighlighter in Blogger?

Simple jQuery AJAX example calling a MVC controller

If you can't see the syntax highlighting in your feed reader (i.e. Google reader) please open the link in your browser.

In this post I will outline a very simple AJAX call to an MVC controller object using jQuery and JSON.

Lets start with the front-end code. Below is a simple HTML input and anchor tag:



Find


When a user enters an image ID in the in the text box and clicks on the find link we will send an AJAX request using jQuery and sending JSON data to the MVC controller. The JSON helper object below is explained here. Below is the JavaScript code:

$(".findLink").click(onFindClick);

function onFindClick(e) {
var jsonData = { imageId: escape($("#ImageId").val()) };

$.ajax({
        type: "POST",
        url: "/Controllers/ImageManager/FindImage",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(jsonData), // Convert JavaScript object into a JSON object string.
        dataType: "text json",
        success: function (XMLHttpRequest, textStatus, data) {
            var jsObj = jQuery.parseJSON(data.responseText); // Convert JSON object into a JavaScript object.

            if (jsObj != null) {
                alert("JSON result = " + data.responseText);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("There has been an error: " + errorThrown + " - " + textStatus + " - " + XMLHttpRequest.statusText);
        }
    });
}


Now for the MVC controller:

public class ImageManagerController : Controller
    {
        public string FindImage(string imageId)
        {
            // Do some code to get the image object...
// Assume image object has 2 properties: Id and Url.
            return SerializeNode(image);            
        }
}

        private static string SerializeNode(ImageReference imageRef)
        {
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var simple = new { Exists = imageRef != null, Id = imageRef != null ? imageRef.id : 0, Url = imageRef != null ? imageRef.Url : "" };
            return serializer.Serialize(simple);
        }


Hope this helps. A very brief example but should outline the major points.

Friday 1 July 2011

Cross-browser JSON Serialization in JavaScript

This is a great site that helps serialize JSON to string and to a JavaScript object.

Link
http://www.sitepoint.com/javascript-json-serialization/