How To: Create a “Hello World” WCF Service Using Visual Studio
Here's a quick step through of using WCF in Visual Studio 2005. In this case I used a local machine, running Windows 2003, for the service and the client.
There's lot of possible paths, and this is just one path through. I focused on "Hello World" to run through the basic mechanics, but chose a path to touch enough things that might be interesting to explore another day.
Scenario
Use Visual Studio 2005 to do a dry run of creating a WCF service hosted in IIS and calling it from a console application on your local development workstation. (Note that you don't need to host WCF in IIS; for example, you could use a surrogate console application.)
Preparation
In my case, I needed the .NET 3.0 components and the WCF extensions for Visual Studio:
1. .NET 3.0 Runtime Components
2. WCF and WPF extensions for Visual studio
Summary of Steps
- Step 1. Create the test service
- Step 2. Add a Hello World Method
- Step 3. Test your WCF service
- Step 4. Enable meta-data for your WCF Service.
- Step 5. Create the test client.
- Step 6. Add a Web Services reference to your WCF Service.
- Step 7. Add the namespace to your
- Step 8. Call your WCF service
Step 1. Create the test service
In this step, we'll create a WCF service that uses HTTP bindings, for backward compatibility (non-.NET 3.0 clients)
- In Visual Studio, click File -> New Web Site
- Select WCF Service
- Browse to a directory to store your project: (e.g. D:\Dev\WCF\Test1\Serve )
- Enable wsHttpBinding. To do so, right-click Web.config and click Edit WCF Configuration ... Expand Services > expand MyService -> expand Endpoints. Click (Empty Name) . Change wsHttpBinding to basicHttpBinding
- Create the virtual directory. In your File Manager, right-click your Server folder (i.e. D:\Dev\WCF\Test3\Server) and click Properties, then Web Sharing, and click Share this folder, then click OK.
Step 2. Add a Hello World Method
In Service.cs, you'll add your Hello World method:
- Add the HelloWorld operation contract below public interface: [ServiceContract()]
public interface IMyService
{
[OperationContract]
string MyOperation1(string myValue1);
[OperationContract]
string MyOperation2(DataContract1 dataContractValue);
[OperationContract]
string HelloWorld();
} - Add your HelloWorld method below public class MyService : public class MyService : IMyService
{
public string MyOperation1(string myValue1)
{
return "Hello: " + myValue1;
}
public string MyOperation2(DataContract1 dataContractValue)
{
return "Hello: " + dataContractValue.FirstName;
}
public string HelloWorld()
{
return "Hello World";
}
}
Compile and debug any errors.
Step 3. Test your WCF service
- In IIS Manager, under Default Web Site, right-click expand Server (the virtual directory you just created)
- Right-click Service.svc and click Browse.
There's two issues you might hit here:
- You don't have ASP.NET installed/enabled. To fix this, first run aspnet_regiis /i from your .NET installation directory (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) Then, allow ASP.NET in your IIS Manager. To do so, in IIS Manager, expand Web Service Extensions, select ASP.NET v.2.0.50727 and click Allow.
- You might see "Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service." To fix this, first enable anonymous access. In IIS Manager, right click your v-dir (Server), click Properties, click Directory Security, click Edit under Authentication and Access control, click Enable Anonymous Access, then OK your way out of the dialogues. Next, recycle IIS. In a command prompt, type IISreset. If successful, when you browse your Service.svc file from IIS Manager (e.g. https://localhost/service/service.svc). you'll get a message that starts with the following:
This is a Windows© Communication Foundation service.
Metadata publishing for this service is currently disabled.
Step 4. Enable meta-data for your WCF Service.
- In VSTS, right-click Web.config and click Edit WCF Configuration. In WCF Configuration, expand Advanced, then expand Service Behaviors, then
right-click returnFaults and click Add Service Behavior Element Extension. Select serviceMetadata and click Add. - In WCF configuration, select serviceMetadata and change HttpGetEnabled to True. Close the dialogue and save changes.
- Test your service again. Browse to https://localhost/Server/Service.svc and this time you should see your service (e.g. MyService Service). You
will see a message that starts with the following:
"You have created a service."
Step 5. Create the test client.
In this step, we'll create a quick console app to call the WCF service:
- In Visual Studio, click File -> New -> Project
- Select Console Application.
- Browse to a directory to store your test client: (e.g. D:\Dev\WCF\Test1\WCFClient)
Step 6. Add a Web Services reference to your WCF Service.
In this step, we'll add a Web Services reference.
- Right-click References
- Click Add Web Reference ...
- In the Add Web Reference dialogue, add the path to your WCF Service (https://localhost/Server/Service.svc)
- Click Add Reference to close the dialogue. You should then see your Web Service reference show up under Web References.
Step 7. Add the namespace to your
- Browse to your Reference.cs file. You'll find the file below Reference.map under your Web service reference. You might need to click the Show All Files button on the Solution Explorer so you can see the files under your Web service reference.
- Find the namespace. You'll see a line similar to the following:
namespace ConsoleApplication1.localhost - Add the using statement to your Program.cs file.
using ConsoleApplication1.localhost;
Step 8. Call your WCF service
In your test client, call your WCF service:
static void Main(string[] args)
{
MyService service = new MyService();
string foo;
foo = service.HelloWorld();
Console.WriteLine(foo);
}
When you compile and run your console application, you should see the following:
Hello World
Press any key to continue . . .
Additional Resources
Hopefully this step through helps you quickly see some of the bits and pieces you can play with.
Comments
Anonymous
October 15, 2007
JD Meier walks through the creation of simple WCF services to be hosted in IIS 6.0. Summary of StepsAnonymous
October 23, 2007
An example of a simple WCF service WITHOUT IIS would be helpful. It would need to be self hosted someway.Anonymous
November 12, 2007
Superb .. simply superb .. set of instructions. Even a absolute newbie can configure this and get this application working. We would really appreciated If you could throw some more light on the concepts of what happens at each step. I am sure it would allow us to cement the understanding of the entire project.Anonymous
November 13, 2007
I think I have some more work to do. In the future, I'll look to having some examples hosting outside of IIS and what happens at each step.Anonymous
April 17, 2008
This is a digest of WCF Security resources I was collecting for some time. Drop me a comment in caseAnonymous
May 11, 2008
This is a digest of WCF Security resources I was collecting for some time. Drop me a comment in case it is useful.