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)

  1. In Visual Studio, click File -> New Web Site
  2. Select WCF Service
  3. Browse to a directory to store your project: (e.g. D:\Dev\WCF\Test1\Serve )
  4. 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
  5. 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:

  1. 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();
    }
  2. 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

  1. In IIS Manager, under Default Web Site, right-click expand Server (the virtual directory you just created)
  2. Right-click Service.svc and click Browse

There's two issues you might hit here:

  1. 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.
  2. 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.

  1. 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
  2. In WCF configuration, select serviceMetadata and change HttpGetEnabled to True.  Close the dialogue and save changes.
  3. 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:

  1. In Visual Studio, click File -> New -> Project
  2. Select Console Application.
  3. 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. 

  1. Right-click References      
  2. Click Add Web Reference ...
  3. In the Add Web Reference dialogue, add the path to your WCF Service (https://localhost/Server/Service.svc)
  4. 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   

  1. 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. 
  2. Find the namespace.  You'll see a line similar to the following:
            namespace ConsoleApplication1.localhost
  3. 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.