Yesterday I wanted to add a WCF service to a simple prototype project I was building. Unfortunately what started off as a simple task ended up in taking much longer than expected (in fact in the end I cheated and went back to a simple asmx web service). This morning, Dimaz pointed me in the right direction.
Essentially the issue is something to do with the changes in the security model. I haven’t tracked down exactly what the change in behaviour is but essentially the way to get a WCF service up and running under Windows 7 (at least initially – don’t push this into production until you understand the repercussions) is to set the security mode to None.
Here’s a quick walkthrough of how to reproduce the scenario:
- Open Visual Studio and create a new WCF Service Application (under the Web node in the New Project dialog)
- Force a rebuild on this new project – this is so that you can use the Add Service Reference wizard in Visual Studio later on.
- Add a new Windows Forms project (or WPF if you choose) to your application
- Right-click the WinForms project node in Solution Explorer and select Add Service Reference.
- Hit Discover
- Select the service that is found.
- Enter the following code either in a button event handler or in the form load event handler for the WinForms project
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
var test = client.GetData(5);
- Set the WinForms project as the startup project and then hit Run (F5).
- When the GetData method is invoked either the vshost.exe process will crash (happens on my computer) or you will get an unhandled ProtocolException. The error that is provided is “The remote server returned an unexpected response: (400) Bad Request.”
Now here’s what you can do to get this running
- In the web.config file of the WCF Service you need to add a custom binding configuration
<security mode="None" />
- You then need to adjust your endpoint declaration to use this binding configuration.
<endpoint binding="wsHttpBinding" bindingConfiguration="wsHttp" … >
- The last adjustment to be made is to set the Security mode in the WinForms app.config file
That’s it, you should be good to go. Just remember, you’ve set the security mode to None which is not good for production unless you are going over SSL or another secure channel.