WCF Manual Service Hosing on Windows Vista and Windows 7

With the new security features introduced in Windows Vista and now in Windows 7 there are some, not so obvious, problems that have crept in and cause weird errors in even simple code. I was building a simple web service for the first time a simple HelloWorld in a console application to make it easy to debug and output trace information. Nothing complicated, a little different because I was configuring everything manually in code as opposed to using configuration file. Here’s the code for my simple, stripped down HelloWorld service:

ConsoleServer.cs
  1.  
  2. using System;
  3. using System.ServiceModel;
  4.  
  5. namespace ConsoleServer
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Console.Write("Server...");
  12.  
  13. ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService),
  14. new Uri("http://localhost:9000/TestService"));
  15.  
  16. serviceHost.AddServiceEndpoint(
  17. typeof(IHelloWorldService),
  18. new WSHttpBinding(),
  19. "");
  20.  
  21. serviceHost.Open();
  22.  
  23. Console.WriteLine("Started");
  24.  
  25. Console.ReadLine();
  26. }
  27. }
  28.  
  29. //operation contract
  30. [ServiceContract]
  31. public interface IHelloWorldService
  32. {
  33. [OperationContract]
  34. string HelloWorld();
  35. }
  36.  
  37. public class HelloWorldService : IHelloWorldService
  38. {
  39. public void HelloWorld()
  40. {
  41. System.Threading.Thread.Sleep(1000); //sleep for one second
  42. return "Hello World Service";
  43. }
  44. }
  45. }
  46.  

And the client:

ConsoleClient.cs
  1.  
  2. using System;
  3. using System.ServiceModel;
  4. using ConsoleServer;
  5.  
  6. namespace Client
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Client");
  13.  
  14. ChannelFactory<IHelloWorldService> myChannelFactory =
  15. new ChannelFactory<IHelloWorldService>(
  16. new WSHttpBinding(),
  17. new EndpointAddress
  18. ("http://localhost:9000/TestService"));
  19. IHelloWorldService wcfClient = myChannelFactory.CreateChannel();
  20.  
  21.  
  22. System.Threading.Thread.Sleep(3000);
  23. Console.WriteLine("Calling Hello World");
  24. for (int i = 0; i < 5; i++)
  25. {
  26. Console.WriteLine(wcfClient.HelloWorld());
  27. Console.WriteLine("- Called HelloWorld");
  28. }
  29.  
  30. Console.WriteLine("Done");
  31.  
  32. Console.ReadLine();
  33.  
  34.  
  35. }
  36. }
  37. }
  38.  

The code is fairly straight forward as far as services go, running it however I began getting the following error:

HTTP could not register URL http://localhost:9000/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

Extremely unhelpful, and the link provided by Microsoft offers no solutions (insert eye roll here) so I went off to consult Google… After a bit of research I discovered that it was the security features in Window 7 and Windows Vista that had been causing the problem, mostly because I’m trying to do everything manually in code some of the permission requests or other behind the scenes magic that normally occurs when Visual Studio 2008 creates and hosts a service from an application configuration file don’t happen. (My guess is that when visual studio installs it sets up permissions for Cassini, it’s inbuilt web server, and that when you run your service the ‘normal’ way that it automatically wraps your service in Cassini, effectively bypassing the normal application security measures, but that’s another story). Long and the short of it is that if you want to do this yourself you need to add some permissions to your user (by default even administrator accounts in Windows 7 and I *think* Windows Vista run in a restricted mode) to allow you to register your service with windows. (The original article is here)

Run a Command Prompt with Administrator privileges (Actually right click and select it), type and run the following command (replace the port number with whatever port you are using, 9000 for the code above, and your domain(or computername if your not on a domain) and username).

netsh http add urlacl url=http://+:8000/ user=DOMAIN\UserName

And *BAM* everything magically works. Cool huh?

– Paul Rohde

An Afternoon Installing Windows 7

menuii

widgetsiiWith Microsoft releasing the beta of their new Windows 7 I decided to go ahead, install in see how well it works and runs in school / development as a primary operating system. I’m currently on a Lenovo Z61p laptop that’s provided by our school with the following specs:

  • T7600(2.33GHz)
  • 2GB RAM
  • 100GB 7200rpm HD
  • 15.4in 1920×1200 LCD
  • 256MB ATI FireGL V5200
  • CDRW/DVDRW
  • 802.11n wireless
  • Bluetooth
  • Modem
  • 1Gb Ethernet

Installation took about an hour and 30 minutes off of a burned dvd (x64 bit edition), about 2 restarts and about 5 minutes at the end to setup, provide user name and password, timezone, and so on. I was a bit worried that I would have issues connecting on the wireless but the drivers that came with Windows 7 were able to find all the networks right off the bat. That being said, there seems to be a slight issue when the wireless requires you to accept a security certificate, it continues popping up multiple times before it finally lets you enter your domain username and password (since my computer is not registered on the Neumont domain). Fortunately after just letting it pop up enough times it eventually let me enter my credentials and connected without a problem.

Next step was installing drivers. Lenovo offers a system update utility that runs, checks your system, shows what drivers and updates are available and will download and install the ones you select. Installed the updater, selected all the critical updates and some of the recommended updates, hit go, and it ran and installed everything without a hitch. The most notable update was for my graphics driver which allowed the Areo glass effect, oddly, it didn’t automagicly switch from the flat basic window manager to Areo, I had to go in and switch manually, but after that everything seemed to work fine, I haven’t had any BSOD (Blue Screens of Death) yet after about 4 hours of running it.

As was getting ready to start installing the software I use, I began having some issues installing Alcohol 52% (For those of you who don’t know, Alcohol 52% allows you to create a virtual cd/dvd drive and access .iso and other files as CD’s or DVD’s), after doing a bit of research it seems that Alcohol and Daemon tools both have some issues with Windows 7 and the way they install and register the virtual drive. Currently I’m playing with Virtual Clone drive which seems to working well so far, I did get a BSOD when I tried to restart my computer, but it has worked fine after that. Other than that virtual clone drive has been working fine mounting and demounting all the iso’s I have of my software. Most everything has installed correctly, including SQL server 2008, Visual Studio 2008, Netbeans, Glassfish v2, Java EE, Expression Studio, Microsoft Office, Digsby and so on.

Windows 7 has a large number of interface improvements, seems to be a bit faster and runs with a smaller memory footprint than vista, and is most of all backward compatible with previous vista drivers which is one of the big reasons I believe that it will surpass vista in popularity. I for one am really looking forward to the final release near the end of this year.

– Paul Rohde