Creating Services with the WCF - Hosting a service (Page 4 of 4 )
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.
- Go to the Solution Explorer and add a new Console Application project to the solution. Name the new projectHost.
Add a reference to theSystem.ServiceModelassembly, and add the followingusingstatement to Program.cs:
using System.ServiceModel;
- You will be writing code to host theHelloIndigoServicetype. Before you can do this, you must add a reference to theHelloIndigoproject.
- Create aServiceHostinstance and endpoint for the service. Open Program.cs in the code window and modify theMain()entry point, adding the code shown in Example 1-1. This code initializes aServiceHostinstance specifying the service type and a base address where relative service endpoints can be located. It 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.
- Compile and run the host to verify that it works. From Solution Explorer, right-click on theHostproject node and select "Set as Startup Project." Run the project (F5), and you should see console output similar to that shown in Figure 1-17.
- Stop debugging and return to Visual Studio.
Example 1-1. Code to programmatically initialize the ServiceHost
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),
new Uri(http://localhost:8000/HelloIndigo)))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
new BasicHttpBinding(), "HelloIndigoService");
host.Open();
Console.WriteLine("Press <ENTER> to terminate the service host");
Console.ReadLine();
}
}

Figure 1-17. Console output for the host application
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.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 1 of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.
|
|