IBlog<Johan>

This and that in a developer's life in general

News

Follow johandanforth on Twitter

Random Links

Walkthroughs and Tutorials

March 2009 - Posts

WCF Certificates with IIS on Windows 7 Beta

I’ve blogged earlier about the problems with Cassini and WCF on Windows 7 Beta (build 7000) and your best bet is to install IIS locally test your services in there. Now, there might be some problems getting IIS to read your service certificate and my colleague Tomas helped me get things running. I thought I might as well blog it here so that I got it documented…

Open a VS2008 Command Prompt (I ran it as administrator) and create a certificate, then add it to your local store:

makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=localhost -sky exchange -pe

certmgr.exe -add -r LocalMachine -s My -c -n localhost -r CurrentUser -s TrustedPeople

Then you have to give IIS access to the private part of the certificate and Tomas found some sample code to let you do that. The FindPrivateKey.exe source code is available on MSDN. Keep working on the command prompt:

FindPrivateKey.exe My LocalMachine -n "CN=localhost"

Note the output for private key directory and filename, for example:

Private key directory: C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys

Private key file name: 288538e27a2aebe9f77d2506bf6c836a_adf55683-4eae-4544-bbd1-d6844a44e538

Then use them to feed the final call to give the default IIS-user access to the private key, for example:

CACLS.exe C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\288538e27a2aebe9f77d2506bf6c836a_adf55683-4eae-4544-bbd1-d6844a44e538 /G "IIS_IUSRS":R

That should be it, and it worked on our machines.

Posted: Mar 23 2009, 01:29 PM by jdanforth
Filed under: ,
Running ASP.NET MVC on Your Windows Home Server

aspnetRight, so I ran the new Web Platform Installer 2.0 Beta on my WHS and it seems (so far) to have worked out quite well. I created a new MVC website with File->New… and published it over to an MVC-directory on WHS (I have set that up earlier, with a file share and everything).

Now, the version of IIS on Windows Home Server is IIS 6 (because WHS runs on Windows 2003 kind of), so ASP.NET MVC won’t work right out of the box. I was hoping that the Web Platform Installer would have taken care of that, but apparently not.

Phil Haack wrote a long, and very detailed blog post about how to set things up on IIS 6, so I’m following that, adding support for the .mvc extension, changing the routes in Global.asax and so on, and voila it works:

ASP.NET MVC in WHS

Now, I want to set up extension-less URL’s, which is prettier than having to have that .mvc extension in the URL. Phil cover this in his blog post as well, so I’m adding wildcard mappings, and here goes:

 Extension-less URLs

Isn’t that just great? I love my home server, maybe I can host my Writespace Clickonce installer on my own server? Not too worried about the load on the server :)

Watch this space for some sample stuff that will be located on my own server *big grin*

Web Platform Installer 2.0 Beta on Windows Home Server

Web Platform Installer I’ve been thinking of setting up ASP.NET MVC 1.0 on my WHS and also start learning some Silverlight stuff, so I took a risk, went to the Microsoft Web Platform Installer page and clicked on the Beta 2.0 link. Downloaded the installer, marked ASP.NET MVC and the most necessary options and let it go. Should work, right?

It had to reboot once to get Windows Installer 4.5 in, but it continued to chew on and after a few minutes:

Done!

Yay! Now I just have to get an MVC test application on there somehow… brb.

IronRuby 0.3 Released

ironruby Jimmy Schemento just blogged:

I’m pleased to announce a new release of IronRuby: IronRuby 0.3!

Staying true to our “Conference-driven development schedule,” this release is in conjunction with MIX ‘09, where Silverlight 3 Beta and ASP.NET MVC 1.0 were announced, to name a few. John Lam spoke at MIX today at 12:30pm in Las Vegas about using dynamic languages in Silverlight for testing, scripting, and application building. And some ASP.NET MVC with IronRuby is thrown in there too.

You can download IronRuby 0.3 here.

Posted: Mar 21 2009, 09:50 PM by jdanforth
Filed under:
ASP.NET MVC v1 Source Code Released

aspnet Phil twitted that the source code for ASP.NET MVC v1 is available on Codeplex now, so I guess it’ll be released as a proper installer on MSDN any time now.

EDIT: …and a few hours later it was on MSDN :)

Congrats to Phil Haack and his merry men for doing such an awesome job!

Posted: Mar 19 2009, 10:47 AM by jdanforth
Filed under:
Case Switching on CLR Types

As most .NET developers know, you cannot do case/switch on CLR types and one of the reasons for it was explained pretty well years ago by Peter Hallam on the C# team.

But there are many cases where you would like to iterate through a list of objects if mixed types and do specific things depending on it’s type. For fun I started to try out different ways to do it, some are better than others but they all do the same thing, more or less. I’m also exploring method extensions, method chaining and lambdas and some of the samples almost started to become fluent and DLS like.

Note

Oh, I’m as far from a C# language expert as anyone can be, so there are other ways of doing this I’m sure. The random code below is just me playing around for a simple way of doing case/switching on types that worked in the code I’m currently working on.

Also, if you would like a derived class to do something special, you normally override a method, send in some parameters and let that code whatever it so special. That’s basic OOD, see the Eat() method in the sample code below. But there are cases where you for one reason or other would not like to do this. Enough of that, this is just for fun.

A List of Pets

I was working with a class hierarchy of pets like this:

namespace TypeCase

{

    public abstract class Pet

    {

        public string Name { get; set; }

        public abstract void Eat();

 

        public override string ToString()

        {

            return Name;

        }

 

        public Pet Case<T>(Action<T> action) where T : Pet

        {

            if (this is T)

                action.Invoke((T)this);

 

            return this;

        }

    }

 

    public class Dog : Pet

    {

        public override void Eat()

        {

            Console.WriteLine(Name + " eats cats.");

        }

    }

 

    public class Cat : Pet

    {

        public override void Eat()

        {

            Console.WriteLine(Name + " eats mice.");

        }

    }

}

 

We got a Cat and a Dog which are both different types of Pet. They have a Name and they can Eat() which is good enough for testing.

Creating the List

I’m creating a simple typed List like this:

            var pets = new List<Pet>

                           {

                               new Cat { Name = "Morris"},

                               new Dog { Name = "Buster"}

                           };

Now we have something to play with. First do something you often see, especially in .NET 1.x code.

Case Switching on Strings

It’s perfectly fine to switch on strings, so this is quite common:

            foreach (var pet in pets)

            {

                switch (pet.GetType().ToString())

                {

                    case "TypeCase.Cat":

                        Console.WriteLine("A cat called " + pet);

                        break;

                    case "TypeCase.Dog":

                        Console.WriteLine("A dog called " + pet);

                        break;

                }

            }

I’m not too fond of this, because you may rename Cat or Dog in the future, or change namespace of “TypeCase” to something else, and even though renaming stuff with Resharper is powerful, strings are often missed. It would have been nice to:

                    case typeof(Cat):

But that’s not allowed. The case must be a constant.

If Is

A much safer way is to use if … else if … and instead of using string comparing, check the type with the is statement. It’s also faster to type:

            foreach (var pet in pets)

            {

                if (pet is Cat) Console.WriteLine("A cat called " + pet);

                else if (pet is Dog) Console.WriteLine("A dog called " + pet);

            }

This code is perfectly fine and I’ve used it many times. But what if I wanted to have a Case-like syntax?

Method Extension on Type

I’m thinking of a syntax like this one:

                pet.GetType().

                    Case(typeof(Cat), () => Console.WriteLine("A cat called " + pet)).

                    Case(typeof(Dog), () => Console.WriteLine("A dog called " + pet));

 

In this case we’re extending the Type type with a Case method, like this:

    public static class TypeExt

    {

        public static Type Case(this Type t, Type what, Action action)

        {

            if (t == what)

                action.Invoke();

 

            return t;

        }

    }

The Action parameter encapsulate the anonymous method we’re sending in, containing the stuff we want to do with the pet in question. In the Case() extension method we’re testing to see if we’re given the right Type and Invoke() the anonymous method if so.

Important Note: Without going into details, just make sure you don’t fall into a case of “Access to modified closure” when doing for(each) loops around anonymous methods. To be safe, you have to create a local pet-variable outside of the method:

            foreach (var pet in pets)

            {

                //some code

                var safePet = pet;

                pet.GetType().

                    Case(typeof(Cat), () => Console.WriteLine("A cat called " + safePet)).

                    Case(typeof(Dog), () => Console.WriteLine("A dog called " + safePet));

                //some more code

            }

 

Better Method Extension on Type

But I’m not happy with this syntax. If feels more cumbersome than the if…is…if…else… syntax, and whenever you see the use of typeof() in code like this, generics can do some work for you. So I’m going for a syntax like this:

                pet.GetType().

                    Case<Cat>(obj => Console.WriteLine("A cat called " + pet)).

                    Case<Dog>(obj => Console.WriteLine("A dog called " + pet));

This requires a new method extension:

    public static class TypeExt

    {

        public static Type Case<T>(this Type t, Action<Type> action)

        {

            if (t == typeof(T))

                action.Invoke(t);

 

            return t;

        }

    }

Looks better, but you still risk getting into the issues with modified closure above and I would like to work on the “obj” parameter as if it was the Pet objekt itself, not the Type. Let’s make it even better:

Even Better Method Extension on Pet

Now I’m going for a syntax that looks like this:

            foreach (var pet in pets)

            {

                pet.

                    Case<Cat>(c => Console.WriteLine("A cat called " + c)).

                    Case<Dog>(d => Console.WriteLine("A dog called " + d));

 

                pet.Eat();

            }

As you can see, the syntax is cleaner and I can work with the pet object itself as a parameter handled to anonymous method in the lambda statement.

To do this I have to create a method extension which knows about the Pet class:

    public static class PetExt

    {

        public static Pet Case<T>(this Pet pet, Action<T> action) where T : Pet

        {

            if (pet is T)

                action.Invoke((T)pet);

 

            return pet;

        }

    }

It’s not a generic Case Switcher on Type, but it feels good to me and is easy to work with. And you don’t have the issue with access to modified closures with this one.

Refined Method Extension on List of Pets

I’m throwing in a final variant here, adding the Case method to the list itself:

            pets.

                Case((Cat c) =>

                        {

                            Console.WriteLine("A cat called " + c);

                            c.Eat();

                        }).

                Case<Dog>(d =>

                        {

                            Console.WriteLine("A dog called " + d);

                            d.Eat();

                        });

 

            pets.

                Case((Cat c) => Console.WriteLine("A cat called " + c)).

                Case<Dog>(d => Console.WriteLine("A dog called " + d));

 

As you can see, there are two ways syntactically to provide the type and the simple extension method for this variant looks like this:

    public static class PetListExt

    {

        public static List<Pet> Case<T>(this List<Pet> pets, Action<T> action) where T : Pet

        {

            foreach (var pet in pets)

            {

                if (pet is T)

                    action.Invoke((T)pet);

            }

 

            return pets;

        }

    }

 

That’s it. I’ve seen a number of more complex ways to do roughly the same, but I’m not trying to create the ultimate Switch/Case framework, just playing around with c# to create something simple that may make the code easier to read (and fun to code).

A Couple of Good Days at Microsoft Sweden TechDays

It was a pleasant first day at TechDays, held in Västerås by Microsoft Sweden. During the day I met with and talked to people I know from before, people I’ve never met but follow on their blogs and some new acquaintances. Cheers to Samuel Kastberg, Pelle “Pellesoft” Johansson, Tomas Wirén, Johan and the guys at Dropit, Kai Fransson just to mention a few.

I talked to Johan Lindfors, manager of the Microsoft DPE team in Sweden, who had a good presentation (the best I’ve seen so far actually) on Oslo together with Alan Smith. I ran into Daniel Akenine, chapter president of IASA Sweden and CTO of Microsoft Sweden. It’s always nice to talk to Daniel!

Later in the evening I talked to Tess Ferrandez and Fredrik Normén about some common mistakes we’ve seen developers do, especially with ASP.NET. Tess had a good talk on the topic earlier that day together with Mikael Deurell, and Fredrik did a good one on what to expect for Silverlight in the future.

Today I attended two talks with Christian Weyer on WCF and on .NET Services and I got the chance to chat a few minutes about WCF interop and bindings to use with usernamepassword tokens which I will will try out soon.

Earlier today I also listened to Ivar Jacobsen about “what they don’t teach you about software at school” and it was very entertaining. Ivar is so professional and full of experience and good knowledge it’s amazing and I have so much to thank him for, considering how much help I’ve had from his work on RUP, UML and so on. It was great to meet him.

The day ended with a session on .NET Services, also by Christian Weyer, which was very, very cool. I have no idea how many moving parts he had going in his demos, but he was doing federated security, services and workflows in cloud and on his machine with azure and he had both .net clients and java clients going and you name it. I have to watch the presentation again when it’s available on the Internet later on.

Posted: Mar 18 2009, 11:59 PM by jdanforth
Filed under: ,
Bad Request With WCF Service in Cassini On Windows 7 Beta

Windows 7 BetaTrying to run a WCF service in Cassini on your Windows 7 Beta (7000) machine and get this error?

The remote server returned an unexpected response: (400) Bad Request.

Unless you’re running your service with basic http binding or with security specifically set to None in your bindings, you will get this security-related error. This problem was confirmed by Microsoft and will (hopefully) be fixed in next Windows 7 release.

The options you got:

  • Create and run your own service host with code (I’ve been doing this in my integration tests until I tried Cassini and got the error)
  • Use IIS (which I’m going to go for)
  • Self-host it with WcfSvcHost.exe (which I’ve not tried yet)

Note that you need to add quite a few features to get WCF running in IIS running on Windows 7. See my previous post about this.

Richard Fennell had the same problems as well as a few others out there brave enough to try out a beta operating system.

Turn Windows 7 Features On or Off

I’m trying to install/add IIS features to my Windows 7 beta build 7000 laptop. You got pretty fine grained control over these features in Windows 7, and it’s not easy to know what you really need. I need to run WCF services on my machine, so I’m turning most things on, including WCF Activation stuff:

image

Then I’m adding these things that I think I need for your average ASP.NET application:

image

Finally I’m adding the management tools, including the IIS 6 compatibility stuff which I’ve been told is needed to play properly with VS2008:

image

Then restart and make sure you got everything in place.

My Windows 7 Beta Installation is More Stable Now

Windows 7 BetaI think I got most things sorted with my Windows 7 Beta installation now. When I uninstalled the “half new” version of Virtual CloneDrive I had, things started to work way, way better. There is now a newer version of Virtual CloneDrive (5.4.2.3 or later) that is reported to work quite well. Until you really need an ISO mounting tool, I suggest you wait installing one!

Small Issue with Multiple Monitors

I found a new, small issue though when I got my laptop connected to an external monitor. Sometimes when I start it up from Sleep or Hibernation it fails to display the Desktop properly and I have to bring up the “Change display settings” control dialog and re-apply my setting there to have it redraw the Desktop and Taskbar properly. Not a big deal, and it works properly after that until I put it to Sleep and start it up without the external monitor.

This is something that I’ve not seen on Vista before anyway, and it sure seems like Windows 7 has more than a few changes around how external displays are managed.

Posted: Mar 11 2009, 04:03 PM by jdanforth
Filed under:
More Posts Next page »