In this article, we will use Visual Studio 2010 to build a HelloWorld WCF service by carrying out the following steps:
- Create the solution and project
- Create the WCF service contract interface
- Implement the WCF service
- Host the WCF service in the ASP.NET Development Server
- Create a client application to consume this WCF service
We will create the WCF service manually from scratch, meaning we will not use any Visual Studio template to create the service. We will also create the host application and the test client application manually, including generating the proxy and config files manually with the tool svcutils.exe. In your real project, you can and should utilize Visual Studio to help with these tasks, but manually doing the actual work is a great way for you to understand what WCF is really like under the hood. This will help you to better understand the why of those WCF templates within Visual Studio.
Creating the HelloWorld solution and project
Before we can build the WCF service, we need to create a solution for our service projects. We also need a directory in which to save all the files. Throughout this book, we will save our project source codes in the C:\SOAwithWCFandLINQ\Projects directory. We will have a subfolder for each solution we create, and under this solution folder, we will have one subfolder for each project.
For this HelloWorld solution, the final directory structure is shown in the following image:

You don't need to manually create these directories via Windows Explorer; Visual Studio will create them automatically when you create the solutions and projects.
Now, follow these steps to create our first solution and the HelloWorld project:
- Start Visual Studio 2010. If the Open Project dialog box pops up, click Cancel to close it.
- Go to menu File | New | Project. The New Project dialog window will appear.

- From the left-hand side of the window (Installed Templates), expand Other Project Types and then select Visual Studio Solutions as the template. From the middle side of the window, select Blank Solution.
- At the bottom of the window, type HelloWorld as the Name, and C:\SOAwithWCFandLINQ\Projects\ as the Location. Note that you should not enter HelloWorld within the location, because Visual Studio will automatically create a folder for a new solution.
- Click the OK button to close this window and your screen should look like the following image, with an empty solution.

- Depending on your settings, the layout may be different. But you should still have an empty solution in your Solution Explorer. If you don't see Solution Explorer, go to menu View | Solution Explorer, or press Ctrl+Alt+L to bring it up.
- In the Solution Explorer, right-click on the solution, and select Add | New Project... from the context menu. You can also go to menu File | Add | New Project... to get the same result. The following image shows the context menu for adding a new project.

- The Add New Project window should now appear on your screen. In the left-hand side of this window (Installed Templates), select Visual C# as the template, and on the middle side of the window, select Class Library.
- At the bottom of the window, type HelloWorldService as the Name. Leave C:\SOAwithWCFandLINQ\Projects\HelloWorld as the Location. Again, don't add HelloWorldService to the location, as Visual Studio will create a subfolder for this new project (Visual Studio will use the solution folder as the default base folder for all the new projects added to the solution).

You may have noticed that there is already a template for WCF Service Application in Visual Studio 2010. For this very first example, we will not use this template. Instead, we will create everything by ourselves so you know what the purpose of each template is. This is an excellent way for you to understand and master this new technology. - Now, you can click the OK button to close this window.
Once you click the OK button, Visual Studio will create several files for you. The first file is the project file. This is an XML file under the project directory, and it is called HelloWorldService.csproj.
Visual Studio also creates an empty class file, called Class1.cs. Later, we will change this default name to a more meaningful one, and change its namespace to our own one.
Three directories are created automatically under the project folder-one to hold the binary files, another to hold the object files, and a third one for the properties files of the project.
The window on your screen should now look like the following image:
We now have a new solution and project created. Next, we will develop and build this service. But before we go any further, we need to do two things to this project:
- Click the Show All Files button on the Solution Explorer toolbar. It is the second button from the left, just above the word Solution inside the Solution Explorer. If you allow your mouse to hover above this button, you will see the hint Show All Files, as shown in above diagram. Clicking this button will show all files and directories in your hard disk under the project folder-even those items that are not included in the project. Make sure that you don't have the solution item selected. Otherwise, you can't see the Show All Files button.
- Change the default namespace of the project. From the Solution Explorer, right-click on the HelloWorldService project, select Properties from the context menu, or go to menu item Project | HelloWorldService Properties.... You will see the project properties dialog window. On the Application tab, change the Default namespace to MyWCFServices.
Lastly, in order to develop a WCF service, we need to add a reference to the System.ServiceModel namespace.
- On the Solution Explorer window, right-click on the HelloWorldService project, and select Add Reference... from the context menu. You can also go to the menu item Project | Add Reference... to do this. The Add Reference dialog window should appear on your screen.

- Select System.ServiceModel from the .NET tab, and click OK.
Now, on the Solution Explorer, if you expand the references of the HelloWorldService project, you will see that System.ServiceModel has been added. Also note that System.Xml.Linq is added by default. We will use this later when we query a database.
Creating the HelloWorldService service contract interface
In the previous section, we created the solution and the project for the HelloWorld WCF Service. From this section on, we will start building the HelloWorld WCF service. First, we need to create the service contract interface.
- In the Solution Explorer, right-click on the HelloWorldService project, and select Add | New Item.... from the context menu. The following Add New Item - HelloWorldService dialog window should appear on your screen.

- On the left-hand side of the window (Installed Templates), select Visual C# Items as the template, and on the middle side of the window, select Interface.
- At the bottom of the window, change the Name from Interface1.cs to IHelloWorldService.cs.
- Click the Add button.
Now, an empty service interface file has been added to the project. Follow the steps below to customize it.
- Add a using statement:
- using System.ServiceModel;
- Add a ServiceContract attribute to the interface. This will designate the interface as a WCF service contract interface.
[ServiceContract]
- Add a GetMessage method to the interface. This method will take a string as the input, and return another string as the result. It also has an attribute, OperationContract.
- [OperationContract]
- String GetMessage(String name);
- Change the interface to public.
The final content of the file IHelloWorldService.cs should look like the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- namespace MyWCFServices
- {
- [ServiceContract]
- public interface IHelloWorldService
- {
- [OperationContract]
- String GetMessage(String name);
- }
- }
Implementing the HelloWorldService service contract
Now that we have defined a service contract interface, we need to implement it. For this purpose, we will re-use the empty class file that Visual Studio created for us earlier, and modify this to make it the implementation class of our service.
Before we modify this file, we need to rename it. In the Solution Explorer window, right-click on the file Class1.cs, select rename from the context menu, and rename it to HelloWorldService.cs.
Visual Studio is smart enough to change all related files, references to use this new name. You can also select the file, and change its name from the Properties window.
Next, follow the steps below to customize this class file.
- Change its namespace from HelloWorldService to MyWCFServices. This is because this file was added before we changed the default namespace of the project.
- Make it inherit from the interface IHelloWorldService.
- public class HelloWorldService : IHelloWorldService
- Add a GetMessage method to the class. This is an ordinary C# method that returns a string.
The final content of the file HelloWorldService.cs should look like the following:- public String GetMessage(String name)
- {
- return "Hello world from " + name + "!";
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MyWCFServices
- {
- public class HelloWorldService : IHelloWorldService
- {
- public String GetMessage(String name)
- {
- return "Hello world from " + name + "!";
- }
- }
- }
Now, build the project. If there is no build error, it means that you have successfully created your first WCF service. If you see a compilation error, such as "'ServiceModel' does not exist in the namespace 'System'", this is probably because you didn't add the System.ServiceModel namespace reference correctly. Revisit the previous section to add this reference, and you are all set.
Next, we will host this WCF service in an environment and create a client application to consume it.
Hosting the WCF service in ASP.NET Development Server
The HelloWorldService is a class library. It has to be hosted in an environment so that client applications may access it. In this section, we will explain how to host it using the ASP.NET Development Server. Later, in the next chapter, we will discuss more hosting options for a WCF service.
Creating the host application
There are several built-in host applications for WCF services within Visual Studio 2010. However, in this section, we will manually create the host application so that you can have a better understanding of what a hosting application is really like under the hood. In subsequent chapters, we will explain and use the built-in hosting application.
To host the library using the ASP.NET Development Server, we need to add a new web site to the solution. Follow these steps to create this web site:
- In the Solution Explorer, right-click on the solution file, and select Add | New Web Site... from the context menu. The Add New Web Site dialog window should pop up.
- Select Visual C# | Empty Web Site as the template, and leave the Location set as File System. Change the web site name from WebSite1 to C:\SOAwithWCFandLINQ\Projects\HelloWorld\HostDevServer, and click OK.

- Now in the Solution Explorer, you have one more item (HostDevServer) within the solution. It will look like the following:

- Next, we need to set the website as the startup project. In the Solution Explorer, right-click on the web site C:\...\HostDevServer, select Set as StartUp Project from the context menu (or you can first select the web site from the Solution Explorer, and then select menu item Website | Set as StartUp Project). The web site C:\...\HostDevServer should be highlighted in the Solution Explorer indicating that it is now the startup project.
- Because we will host the HelloWorldService from this web site, we need to add a HelloWorldService reference to the web site. In the Solution Explorer, right-click on the web site C:\...\HostDevServer, and select Add Reference... from the context menu. The following Add Reference dialog box should appear:

- In the Add Reference dialog box, click on the Projects tab, select HelloWorldService project, and then click OK. You will see that a new directory (bin) has been created under the HostDevServer web site, and two files from HelloWorldService project have been copied to this new directory. Later on, when this web site is accessed, the web server (either ASP.NET Development Server or IIS) will look for executable code in this bin directory.



Peter MortensenPosted Jul 26, 2015, 10:25 AM
Your article has been plagiarised (leaving out the author information) at http://mycodeXXXXlogic.blogspot.dk/2011/07/implementing-basic-hello-world-wcf.html (I have inserted four "X"s in the URL to prevent any link juice).
MrDoucheChill .Posted Jul 22, 2015, 2:14 PM
Hey Mike, great article! You can find a completely plagiarized copy of this whole page here: http://mycodelogic.blogspot.dk/2011/07/implementing-basic-hello-world-wcf.html Please smack them for all web content creators.
Md Eqbal AnsariPosted Dec 18, 2012, 6:01 AM
Good Post...
Nishantha HettigodaPosted Dec 18, 2012, 1:41 AM
Thanks for sharing such a nice article.
siddu lakkarsuPosted Jul 6, 2012, 8:43 AM
Thank u very much 4 u r post
Salah MohamedPosted Mar 28, 2012, 10:39 AM
Hello Mike, this is best Thank Thank you Salah
Shashi SinghPosted Nov 6, 2010, 1:29 PM
Thanks, Mike...Really its very well composed article.
Vijay KumarPosted Sep 23, 2010, 12:15 AM
Dear Mike., Thanks for sharing such a nice article.
robert paulPosted Sep 3, 2010, 2:43 AM
Thanks for sharing this nice and useful information...call center services
Muhammad IrfanPosted Jul 31, 2010, 5:46 AM
Thank you for such a nice article....
Ganesh DevadharPosted Jul 12, 2010, 3:15 AM
Thanks Mike. This helped a lot. Ganesh
Dinesh BeniwalPosted Jul 9, 2010, 10:19 AM
Dear Mike, Thanx for the article, in this article beginner can learn about WCF Service. Dinesh