Pages

Windows Communication Foundation (part two)

This is part two of the WCF project that begins here.


Step Three: Host and Run the WCF Service

The following code is added to the mainline of the application. First we will need to configure a base address for the service. A base address for the service is specified by an URI instance:


In order to host the service, we will need to create a ServiceHost instance next. The ServiceHost constructor requires the type that implements the service contract and a base address. We have the latter from above. Our code looks like this:


The next few steps are done inside of a try catch block that will handle any communications errors.


Next we need to define an endpoint that exposes the service. This is the critical point at which a number of pieces come together. We need the contract that this endpoint is exposing, a binding, and a relative address.


Next you may want to enable metadata exchange. There are some security considerations related to metadata that should be considered.


Finally, we need to open the ServiceHost instance so that it can receive messages. I've built this project as a console application, so here is the code to open selfHost and allow the user to terminate the process:


In the next post, I'll be creating the client that will access this service: part three.

Here's a link to the completed application:

No comments:

Post a Comment