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
using System; using System.ServiceModel; namespace ConsoleServer { class Program { static void Main(string[] args) { Console.Write("Server..."); serviceHost.AddServiceEndpoint( ""); serviceHost.Open(); Console.WriteLine("Started"); Console.ReadLine(); } } //operation contract [ServiceContract] public interface IHelloWorldService { [OperationContract] string HelloWorld(); } public class HelloWorldService : IHelloWorldService { public void HelloWorld() { System.Threading.Thread.Sleep(1000); //sleep for one second return "Hello World Service"; } } }
And the client:
ConsoleClient.cs
using System; using System.ServiceModel; using ConsoleServer; namespace Client { class Program { static void Main(string[] args) { Console.WriteLine("Client"); ChannelFactory<IHelloWorldService> myChannelFactory = new ChannelFactory<IHelloWorldService>( new EndpointAddress ("http://localhost:9000/TestService")); IHelloWorldService wcfClient = myChannelFactory.CreateChannel(); System.Threading.Thread.Sleep(3000); Console.WriteLine("Calling Hello World"); for (int i = 0; i < 5; i++) { Console.WriteLine(wcfClient.HelloWorld()); Console.WriteLine("- Called HelloWorld"); } Console.WriteLine("Done"); Console.ReadLine(); } } }
The code is fairly straight forward as far as services go, running it however I began getting the following error:
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).
And *BAM* everything magically works. Cool huh?
– Paul Rohde
meh. People complain that MSFT adds security and people complain when they don’t.
It’s a lose lose for sure.
Good writeup. It’s seems to me that more often than not, people complain about MSFT’s lack of security or bad implementation of it.
The way its done is not necessarily a bad way of going about it (In this particular instance), most of the time people don’t go mucking around in the code trying to do things by hand when everything is already done for you, so this is more of a specific case. If they left it unsecured I could potentially see a lot of room for viruses, Trojans, Spyware etc… to take advantage of it. Still, the fact that there is not an official documented workaround is what bothers me and why I wrote this up, both for myself for future reference, and for others that may or may not have the same problem as me.
I would consider MSDN blogs an unofficial official source.
There is this one,
http://blogs.msdn.com/anirbanc/archive/2008/05/14/wcf-error-http-could-not-register-url-http-8000-your-process-does-not-have-access-rights-to-this-namespace.aspx