Friday, February 22, 2008

Replacing mailSettings with Web Deployment Projects

Up until this morning, I've been able to avoid deciding how to handle environment specific settings for a new application I am working on. By using the hosts file, I was able to create a logical name for my SQL server for example which I could replicate accross my environments. This worked fine until this morning when I added email sending capabilities into the web application.

I decided to go and implement web deployment projects which provide a way to replace portions of the configuration files. I set everything up and then following along with this article from Microsoft.

I click build and I get the following error: Error 106 web.config(1): error WDP00002: missing section system.net/mailSettings.

It turns out WDP's configuration replacement tool cannot replace SectionGroups. It can only replace sections. Both System.Net and mailSettings are SectionGroups and thus cannot be replaced out.

If you want to replace the mailSettings you will need to target the smtp node so you'll want to use the following settings:
system.net/mailSettings/smtp=<your config file>

Wednesday, February 20, 2008

Wicked

I am going to try and keep my personal life out of this blog, but last night I took my wife to see Wicked for the second time. It is a really great show and I recommend anyone who has ever seen the Wizard of Oz (and if you've never seen the Wizard of Oz then I wonder what deep dark hole you crawled out of) to go see it.

Josh

Thursday, February 14, 2008

Javascript, Scope Context ohh my

Ok,

So my last post started to dive into the scope, and context around javascript exection. I have since come accross a really good post by Mike West. This article showed another solution to the issue of loosing scope.

I ran into a problem where I had a javascript object and I wanted a functon in the object to be called. I am a big fan of composing my methods into smaller methods, so like many functions I write this function made calls to other functions that were within in the same object. So for example we had something like:
function world(){    
 this.spin=function(degrees){        
  //do something    
 }    
 this.handleClick=function(){        
  this.spin(90);    
 }
}



When I attached the handleClick function to the event handler of a button for example. I would loose the ability to call the rotate method. This was due to the context "this" (See the article I referenced above for a good explanation).

The soltion Mike West highlighted (which appears was borrowed out of prototype Javascript which is referenced in his article). Is to extend the function object itself. We can define a bind function which allows us to change the call context prior to calling our method.

Function.prototype.bind = function(obj) { 
 var method = this, temp = function() { 
  return method.apply(obj, arguments); 
  }; 
 return temp; 
} 


Using this solution is extremly easy. You can simply say object.event=myWorld.handleClick.bind(myWorld);

Javascript's setInterval / setTimeout

Kicking off asynchronous tasks is a common problem in programing languages, and while I have become very accostumed to managing threads and using timers in .Net, I found that two of the mechanism in Javascript for executing code asynchronously at specified intervals to have some odd pecularites.

If you google setInterval and setTimeout, you will learn that these are two methods that can be used delay execution of code by a certain number of milliseconds. setInterval will execute some javascript code every x milliseconds. I am going to focus on setTimeout (setInterval has the exact same semantics).

setTimeout takes two parameters, the first is either a string or a function pointer, and the second is an integer which is the number of milliseconds to wait before executing the code. For example setTimeout("alert('hello world')",10000); Will cause an alert box to be displayed after 10 seconds.

While this might be useful in some cases the more powerful form is to pass a function pointer to be executed. There are a couple key points to keep in mind when using the function pointers:

  • First is that you are to pass a reference to a function so the following will not work as expected:
    setTimeout(myObj.sayHello(var1),5000);

    This will result in the code being executiung immediatley. You will need to instead use: setTimeout(myObj.sayHello,5000);

  • Since you are limited to using a function pointer you cannot pass any arguments (There is a workaround for firefox but it is not supported in ie).


The question then becomes how do you pass in arguments. You might be tempted to use the string overload; however, when setTimeout evaluates a string the current context becomes window. This causes all references to this to evaluate to the window object.

To get around this you can create a javascript object which is nothing more then a function. Imagine the following code



function world(numPeople)
{

this.numberofPeople=numPeople;

this.sayHello=function(){alert("Hello to all " + this.numberofPeople + " people in the world");}

}

myObj=new world(1000);
setTimeout(myObj.sayHello,1000);


When we run this code we should get an alert box saying hello to 1000 people; however, we get an alert indicating that this.numberofPeople is undefined. The this keyword is pointing to the window object which doesn't have a definition for numberofPeople.

A small change to this object will make it work. And I am curious to the opinion of anyone who comes accross this blog as to if this is an ok solution.
function world(numPeople)
{

var numberofPeople=numPeople;

this.sayHello=function()
{
alert("Hello to all " + numberofPeople + " people in the world");
}

this.get_numberofPeople=function()
{
return numberofPeople;
}

}

myObj=new world(1000);
setTimeout(myObj.sayHello,1000);

alert(myObj.get_numberofPeople());


Here are changing the numberofPeople to a variable within the scope of the function world. This ensures that when the sayHello method is executed that the variable is still within scope; however, if you needed to also access the numberofPeople variable from outside of the world function you will need to add an accessor such as get_numberofPeople.

Sunday, February 10, 2008

Windows Vista 64 & Virtual CD/DVD

So, I start a new job on Monday, at a brand new company (I am the first employee). Because of this I had to run out and buy a temporary laptop while my real one was being shipped from Dell. I ended up finding a nice deal on an HP at Compusa which is being liquidated.

The problem I have run into is finding a virtual cd/dvd drive to load the images from MSDN for Windows Vista x64. Microsoft has a great little tool for this Virtual CD-ROM Control Panel for Windows XP. This tool sort of works on Vista but not on Vista x64 at all.

I tried Magic ISO; however, whenever I went to open up my computer, with a drive mounted windows explorer would crash.

I am still in search of a working tool and should I find one I will update this post. For now I am just waiting anxiously for my real laptop with good old WindowsXP on it.

My message to Microsoft would be that if you release new OS's you should release updated versions of your tools. If you want to drive adoption of Vista you do need to ensure the tools are available either from you or from a third party.