Well, I’m in beautiful sunny downtown Rayong, Thailand, and WCF is not high on my agenda right now, so this update is going to be a quick and simple, right-down-to-business tutorial on something a lot of you have been asking for: combining WCF and .NET 2.0.
WCF really is nice to use. The problem, however, is that .NET 3.5 is truly a beast to deploy. In many cases, requiring .NET 3.5 on a client’s machine is simply not on. Luckily, WCF is a server technology, and you don’t actually need .NET 3.5 on the client machine to access it. I’m going to show you how.
Let’s start with the WCF client. Create a new WCF Service Application (found under the Web category), targeting .NET 3.5. I’ll call mine WCFService.
In previous articles, I’ve written about a lot of the work involved with getting a WCF service built and running under IIS. The nice thing about this VS template is that most of that is already done for you. As soon as the project is created, run it. Here’s what you’ll get:
As you can see, you’re browsing the service application you wrote. This isn’t actually IIS, it’s a stripped down web server that ships with VS2008 called ASP.NET Development Server. Don’t worry, though: it’s almost entirely compatible with the real IIS. Except for a couple issues I’ve discussed previously regarding security and configuration settings, you won’t have any trouble making the transition.
So, our web service is running at http://localhost:49180. You’ll see a file in the listing you get called Service1.svc. This is the default WCF service VS added to your new project. Click it, and you’ll browse to http://localhost:49180/Service1.svc, which will give you a page telling you that you’ve found a WCF service. It also includes a link the the WSDL, which is what’s needed to consume this WCF service from non-WCF platforms, such as .NET 2.0. Note that the port number may be different on your machine; be sure to use the address Visual Studio provides, not the example address I’m using through this tutorial.
There is just one more change you must make to your WCF Service to allow older clients to use it: you must use the basicHttpBinding rather than the default wsHttpBinding. Open web.config and find the following line (likely line 154 or so):
<endpoint address="" binding="wsHttpBinding" contract="WCFService.IService1">
Change it to:
<endpoint address="" binding="basicHttpBinding" contract="WCFService.IService1">
Your service is now finished.
Now, on to the client. I’ll use Visual Studio 2005 for this, even though you could just as easily use VS2008 and target .NET 2.0. Create a new Windows Forms Application. I’ll call mine WinFormsClient:
Once your project is created, you’ll need to add a reference to the WCF service you just built. Click the Project menu and select Add Web Reference. You’ll get a window asking you for an address. Type the URL to your WCF Service (for now, it should be http://localhost:49180/Service1.svc, although the port number may be different) and click Go. Visual Studio will find your WCF Service, although it will refer to it as a Web Reference:
I would suggest giving your reference a more meaningful name, such as WCFService. Once you click Add Reference, VS will automatically set up everything needed to call the WCF Service. Now, let’s write some code. Double-click your new empty Form1, and VS will open up the Form Load method. Type this:
Dim wcfService As New WCFService.Service1()
Me.Text = wcfService.GetData(Now.Second, True)
Your WCF Service should still be running, so now you just need to run your client. When you do, the title bar should display the text returned by the call to your WCF Service:
Just like that, you’ve consumed a WCF service from .NET 2.0! Obviously, there are a lot of places to go from here. Debugging code is very nice; you’ll be able to jump from breakpoint to breakpoint right across the WCF boundary. In real life, you’d want to adjust the URL used to access your web service. But to just get up and running, that’s about it!
Hope this helps. Time for me to head back to the beach. Good luck!