Monday, September 8, 2008

ServiceController class, LINQ and Extension Methods

I ran across a nice article here on how to determine if a Windows Service is installed. I thought this would be better as an extension method. Previously, you would need to catch the InvalidOperationException and ignore it if you tried to use the ServiceController class to get information on a service that is not installed.

public static class ServiceControllerExtensions

{

    public static bool IsInstalled(this ServiceController controller, string serviceName)

    {

        return (from sc in ServiceController.GetServices()

                   where sc.ServiceName.EqualsIgnoreCase(serviceName)

                   select sc).Count() > 0;

    }

}

Sunday, September 7, 2008

Two birds with one stone (0xC0000005 failure for Google Chrome and SQLProfiler 2008)

In my previous blog entry on Google’s Chrome and Symantec’s EndPoint and provided a solution, which was to disable Chrome’s sandboxing. That’s really not a good idea. I noticed last week after installing SQL Server 2008 that it also was giving the same failure.

I went and re-googled for a solution and found a little further down in the article about using the —no-sandbox a link to Symantec’s site. You need to tweak a value in your Registry, reboot, and then both applications can run!

Here’s the link.

Thursday, September 4, 2008

Google's Chrome and Symantec's Endpoint Protection

I installed Google’s Chrome only to discover that our corporate anti-virus program was preventing its execution:

chrome-fail-a

And then you get the nice:

chrome-fail-b

Awww… reminds me of the old Sad Mac. I wonder if it plays the tones? Here’s a link to a temporary solution here.

chrome-fail-c


And now the love…

chrome-fail-d

Wednesday, September 3, 2008

Checking/Asserting for not null with a Generic Method

In my code I like to check parameters for entry conditions and assert where the parameters do not match with the contract for the method. I prefer to have a quick to type and one-line method to do so. My initial code to do this looked like the following:



public static void IsNotNull(object obj, string message)


{


    if (obj == null) throw new NullReferenceException(message);


}


That was great and it allowed me to check for null parameters inside of a method easily with some code that looks like this:



Assert.IsNotNull(account, "account was null");


Assert.IsNotNull(account != null, "account was null");


But notice that the signature of the method allows for the erroneous account != null to be used. To prevent this I changed the signature of method to:



public static void IsNotNull<T>(T obj, string message)


    where T : class


{


    if (obj == null) throw new NullReferenceException(message);


}


Now when you inadvertently put in a value type, it will correctly report this as an error. If you are new to Generics, then you might assume that you must identify the type. Either of the two usages below are equivalent thanks to the compiler inferring the type of the template ‘T’.


Assert.IsNotNull(account, "account was null");

Assert.IsNotNull<Account>(account, "account was null");


If you use Resharper, it will show you that you can safely remove the <Account> from the method call reporting that ‘Type argument specification is redundant.