Sunday, January 23, 2011

Using NUnit to explore static field initialization

Given the following code:



class Constants
{
public static readonly Constants Instance = new Constants();
public static readonly string ApplicationName = "ig2600";
public readonly string CachedClassName;

public Constants()
{
CachedClassName = GetQualifiedClassName("Constants");
}

public static string GetQualifiedClassName(string className)
{
return string.Format("{0}::{1}", ApplicationName, className);
}
}


Which of the below tests pass?

[TestFixture]
public class TestConstants
{
[TestFixture]
public class TestConstants
{
[Test]
public void TestOne()
{
Assert.That(Constants.Instance.CachedClassName, Is.EqualTo("ig2600::Constants"));
}

[Test]
public void TestTwo()
{
Assert.That(Constants.Instance.CachedClassName, Is.EqualTo("::Constants"));
}
}
}


Not sure? Download the code with mercurial from  https://bitbucket.org/idvorkin/ig2600_blog, then fire up VS and NUnit, and look at the StaticFieldMystery project.

Saturday, January 22, 2011

Four Visual Studio Extensions You Need To Be Using

I spend a lot of time in Visual Studio and these extensions  make visual studio even more awesome.  If you haven't used VS to install  extensions, it's  trivial.  Follow along by doing:



Tools -> Extension Manager -> Online Gallery -> Search



Here are the extensions  I use, and strongly recommend you try. I have seen no adverse problems from running any of these extensions.  Here are the extensions I use:



Productivity Power Tools (by Microsoft; free):

Adds a Solution Navigator,  a better Solution Explorer,  and cool tabs features like pinning and color coding by project.





PowerCommmands for Visual Studio (by Microsoft, free): 


Several random features, the ones I really enjoy are Open Commmand Prompt here and Open Containing Folder.





NuGet Package Manager (by Micosoft, free): 


A package manager for .net. Say you want to use an open source library in your VS project, you open package manager and say Install-Package . With that the project you requested along with it's dependancies are downloaded and liked to your project. Must have when working with popular .net projects.





Resharper (by Jetbrains, 199$) This is "the" extension for visual studio. If you haven't used it, get the trial, I suspect you'll conclude it's worth paying for.  Resharper has several strong refactorings, plugins to fix stylecop errors, and first class support for running NUnit. 





VSVim (by Jared Parsons, free)

If you don't love VI you skip this.  If you love VIM you get a fifth must use extension.  VsVim gives VIM keybindings to visual studio. It  still has some warts, but it's being actively developed and gets better every release. 



Again, I strongly recommend if you use Visual Studio that you try out these extensions right now, trust me, it’s worth the effort.





Monday, January 17, 2011

SSDs rock - but not for the reasons you think

I bit the bullet and picked up an SSD. It's expensive, but it had some surprising benefits. The normally quoted benefit is speed - which I really don't notice. Most operations went from some annoying amount of time, to some other annoying amount of time. I did notice two things:



1) Silence:  I'm so used to hearing my laptop harddrive I couldn't figure out what I was missing - there is no more annoying grindings; clicking or whirring.



2) Coolness: SSD's run cooler - which means less spinning fan, which means more silence



In theory the SSD should also help with the dropping (my last HDD got some bad sectors after a short fall) - so far very true.



(Update: A short,  highly entertaining talk about SSDs - mostly for servers here)

Monday, December 27, 2010

Delayed Push Notification Service for Windows Phone

This is a multi part blog post the other posts in this series are:

  • Overview – you’re reading this now.

  • The push notification service – how to implement the delayed push notification service

  • The client library – not yet written.

In this post I discuss push notifications, and share my web service which implements a delayed push notification.

Push notifications are a web service that MS provides that can send a message to the phone. There are 3 flavors of push notifications:

  1. Raw notification: Raw bytes - ignored if your app isn't running, sent to your app if it's running. 

  2. Tile notification: Manipulate application tile, worth it's own blog post. 

  3. Toast notifications: If your app isn't running pop up a toast giving a message to the user. 

An interesting detail about the push notification web service is how the web service is addressed, and how the phone is identified. I'd expected something like this: But instead you get the more elegant:



// doesn't work like this.
proxy = CreateProxy("http://well-known-push-notification-service")
proxy.push-toast(id-for-running-app-phone-taken-from-phone, "toast text")
proxy.push-toast(id-for-running-app-phone-taken-from-phone, "toast text2")
// works like this.
proxy = CreateProxy(id-for-running-app-phone-taken-from-phone-with-built-in-service-url)
proxy.push-toast("toast text") // no need to specify phone/app id
proxy.push-toast("toast text2") // no need to specify phone/app id
An annoying limitiation of push notifications is you can't schedule them at arbitrary times. From MSDN:

sendNotificationRequest.Headers.Add("X-NotificationClass", ""); 

// Possible batching interval values:
// 3: The message is delivered by the Push Notification Service immediately.
// 13: The message is delivered by the Push Notification Service within 450 seconds.
// 23: The message is delivered by the Push Notification Service within 900 seconds.
As a result I decided to write a simple web service which will allow you to send your app push notifications.  The sequence diagram for this service is:



The API for this web service is:

http://{root}/push-toast
POST a query string with the following parameters:
{'url:'push notification url',
'message':'text of the toast'
'seconds':'time delay in seconds'
}

RETURN: a json object with:
{
'key' : 'an opaque value used when requesting the toast be cancelled '
'secret' : 'an opaque value used when requesting the toast be cancelled '
}

http://{root}/cancel-push
POST a query string with the following parameters:
{
'key' : 'an opaque value returned from post'
'secret' : 'an opaque value returned from post'
}
RETURN: not defined
If you want to use this web service you can ping me and I'll give you access. In future blog posts I'll talk about the code for the web service as well as the client library.  Leave a comment if you'd like to use this service and we can talk about whatever features you require.

Tuesday, December 14, 2010

PsychicDebugging - You can browse to www.google.com, but you can't ping it.

In my last message to the world I though I'd fixed my networking, but turns out I hadn't. Here's the state of affairs:

  • IE; Chrome and Firefox work perfectly.

  • ping www.google.com fails instantly with hostname not found

  • nslookup www.google.com works

  • From python socket.gethostaddr ('www.google.com',80) fails instantly.

This had me stumped, I assumed the web browsers must be calling different apis from ping.  A few invocations of cdb, and I realized the apps that worked are all 32 bit. That was the trick, my 64 bit NSP catalog was also corrupted; and thus all 64bit apps couldn’t resolve networking.   Luckily from yesterdays shenanigans I had exported the catalog from the registry and re-loaded it. 

To look at the catalog use the powershell:

PS HKLM:\> dir HKLM:\SYSTEM\CurrentControlSet\services\WinSock2\Parameters\NameSpace_Catalog5\Catalog_Entries64 | % {$_.GetValue("LibraryPath")}
c:\Program Files (x86)\Microsoft Firewall Client 2004\FwcWsp64.dll
%SystemRoot%\system32\NLAapi.dll
%SystemRoot%\System32\mswsock.dll
%SystemRoot%\System32\winrnr.dll
%SystemRoot%\system32\napinsp.dll
%SystemRoot%\system32\pnrpnsp.dll
%SystemRoot%\system32\pnrpnsp.dll
%SystemRoot%\system32\wshbth.dll
C:\Program Files\Common Files\Microsoft Shared\Windows Live\WLIDNSP.DLL
C:\Program Files\Common Files\Microsoft Shared\Windows Live\WLIDNSP.DLL
In summary if someone asks how it’s possible the browser works but they can’t ping www.google.com – try checking their 64 bit NSP catalog.

Saturday, December 11, 2010

Set a System Restore Point

Start -> Create a  restore point -> Create

A few days ago my event viewer service failed to start, and I went on a crazy goose chase that was most unpleasant.  I suspect my life would have been better if I had a working restore point, so I'm sharing my wisdom with you: create a restore point now.

Some surprising things I found on my journey:

  • Eventlog not starting because The Authentication Service is Unknown (Error:1747) => Winsock Catalog corruption.
  • netsh winsock reset should fix said corruption but didn't in my case.
  • Rumor is when netsh winsock reset doesn't work and you need to get tcp to reinstall to rebuild the catalog.
  • Honestly I don't know how I got this fixed, but in theory a restore point would have fixed it.

Summary: Go set a restore point now :)

Tuesday, November 23, 2010

The Scunthorpe problem

I ran across an obscure reference to Scunthorpe problem and had to investigate what this “problem” is.  It turns out Scunthorpe is a town in England, as are the following towns which share the same problem: Penistone, Lightwater.

No idea? Perhaps this interview question will make it clear: Design a simple system that can determine if a user name is obscene.