In the previous chapter, we learned several WCF concepts and looked at a few code snippets.
In this chapter, we will implement a basic WCF service from scratch. We will build a HelloWorld
WCF service by carrying out the following steps:
Before we can build the WCF service, we need to create a solution for our service project. We also need a directory in which we will save all the files. Throughout this book, we will save our project source codes in the C:SOAWithWCFandLINQProjects
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 screenshot:
Now follow these steps to create our first solution and the HelloWorld
project:
HelloWorld
in the Name field and enter C:SOAWithWCFandLINQ
Projects in the Location field. Note that you should not enter HelloWorld
within the location, because Visual Studio will automatically create a folder for a new solution.HelloWorldService
in the Name field. Enter C:SOAWithWCFandLINQProjectsHelloWorld
in the Location field. 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 2012. For this very first example, we will not use this template. Instead, we will create everything by ourselves to understand the purpose of each template. This is an excellent way for you to understand and master this new technology. In the next chapter, we will use this template to create the project, so we don't need to manually type a lot of code.
Once you click on 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.
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 screenshot:
We have now created a new solution and project. Next, we will develop and build this service. But before we go any further, we need to do two things to this project:
MyWCFServices
in the Default namespace field.Lastly, in order to develop a WCF service, we need to add a reference to the System.ServiceModel
assembly. Perform the following steps:
Now in Solution Explorer, if you expand the references of the HelloWorldService project, you will see that System.ServiceModel has been added under References. Also, note that System.Xml.Linq is added by default. We will use this later when we query a database.