Creating the HelloWorld solution and project
Now that we have a basic understanding of WCF concepts and terminologies, let's start building a simple HelloWorld WCF service. 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:\SOAwithWCFandEF\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.
Note
You don't need to manually create these directories with 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 2013 (you can use Visual Studio Ultimate, Premium, or Professional throughout this book). If the Open Project dialog box pops up, click on Cancel to close it.
- Go to menu FILE | New | Project.... The New Project dialog window will appear, as follows:
- From the left-hand side of the window, expand Installed | Templates | Other Project Types and then select Visual Studio Solutions as the template. From the middle section of the window, select Blank Solution.
- At the bottom of the window, type in
HelloWorld
in the Name field and enterC:\SOAwithWCFandEF\Projects\
in the Location field. Note that you should not enterHelloWorld
within the location, because Visual Studio will automatically create a new folder for us inside theProjects
folder. - Click on the OK button to close this window, and your screen should look like the following screenshot with an empty solution:
- Depending on your settings, the layout might be different. However, you should still have an empty solution in your Solution Explorer. If you don't see the Solution Explorer, navigate to 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 FILE | Add | New Project… to get the same result. The following screenshot shows the context menu to add a new project:
- The New Project window should now appear on your screen. On the left-hand side of this window, select Installed | Visual C# as the template, and in the middle section of the window, select Class Library.
- At the bottom of the window, type in
HelloWorldService
in the Name field. LeaveC:\SOAwithWCFandEF\Projects\HelloWorld
in the Location field. Again, don't addHelloWorldService
to the location, as Visual Studio will create a subfolder for this new project (Visual Studio will use theHelloWorld
folder as the default base folder for all the new projects added to the solution). Refer to the following screenshot:You might have noticed that there is already a template for a WCF Service Application in Visual Studio 2013. 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.
- Now you can click on the OK button to close this window.
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 our project's 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.The window on your screen should now look like the one shown in the following screenshot:
We have now created a new solution and a new project. Next, we will develop and build this project as a new service. However, before we go any further, we need to do one more thing to this project. Click on the Show All Files button on the Solution Explorer toolbar as shown in the preceding screenshot. Clicking on this button will show all files and directories in your hard disk under your project's folder—even those items that are not included in the project. Make sure that you don't have the solution item selected, otherwise you cannot see the Show All Files button.
Lastly, in order to develop a WCF service, we need to add a reference to the System.ServiceModel
assembly. Perform the following steps:
- In the Solution Explorer window, right-click on the HelloWorldService project and select Add | Reference… from the context menu. You can also right-click on References and select Add Reference… or go to PROJECT | Add Reference… to do this. The Reference Manager dialog window will appear on your screen as follows:
- Check the checkbox in front of System.ServiceModel from the Framework tab under Assemblies and click on OK.
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.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.