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 projects. We also need a directory in which to save all the files. Throughout this article, we will save our project source codes in the D: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 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:
You may have noticed that there is already a template for WCF Service Application in Visual Studio 2008. For the 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.
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:
Lastly, in order to develop a WCF service, we need to add a reference to the ServiceModel namespace.
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.
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.
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]
[OperationContract] String GetMessage(String name);
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);
}
}
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…