Next, add a new console application to the solution. This will be the host application. You'll instantiate a ServiceHost instance for the service type and configure a single endpoint.

  1. Go to the Solution Explorer and add a new Console Application project to the solution. Name the new project Example.Host, as shown in Figure 7 - Creating the Example.Host Console Application project.
  2. Add a reference to the System.ServiceModel assembly, and add the following using statement to Program.cs:

using System.ServiceModel;

  1. You will be writing code to host the NewService type. Before you can do this, you must add a reference to the Example.Service project.
  2. Create a ServiceHost instance and endpoint for the service. Open Program.cs in the code window and modify the Main() entry point, adding the code show in Figure 8 - Example.Host implementation. This code initialises a ServiceHost instance specifying the service type and a base address where relative service endpoints can be located. Is also adds a single relative endpoint for the service. In this case, a base address is provided for HTTP protocol, and the relative endpoint uses one of the standard bindings, BasicHttpBinding, based on HTTP protocol.
  3. Compile and run the host to verify that it works. From Solution Explorer, right-click on the Example.Host project node and select "Set as Startup Project." Run the project (F5), and you should see console output similar to that shown in Figure 9 - Example.Host running the service. Stop debugging and return to Visual Studio.



Figure 7 - Creating the Example.Host Console Application project

Figure 8 - Example.Host implementation

Figure 9 - Example.Host running the service
You now have a host application for the service. When it is running clients will be able to communicate with the service. The next step is to create a client application.

  • No labels