In this module we learn about client applications connecting to WCF Services. We will also learn how to host WCF applications from inside another application that acts as a host for the WCF Service. In this post we will use a Windows Presentation Foundation style application to host our WCF Service.
A WCF service can be hosted in 3 places
(1) Self-hosting in any managed .NET application (2) Hosting in a Windows service (3) Hosting in different versions of IIS.
In this hands-on lab, you will learn how to:
- Host a WCF Service inside of a WPF Application
- Programatically make a WCF Service Operational
- Programatically add Endpoints to a ServiceHost object
- Connect to a running WCF Service from a Console Service Client Application
The following is required to complete this hands-on lab:
- Visual Studio 2010 – 2013
- Finished the steps about my previous post (http://blogs.msdn.com/b/brunoterkaly/archive/2013/10/18/getting-started-with-wcf-windows-communication-foundation-running-and-debugging-quickly.aspx)
In order to execute the exercises in this hands-on lab you need to set up your environment.
- Start Visual Studio and open the previous project here: (http://blogs.msdn.com/b/brunoterkaly/archive/2013/10/18/getting-started-with-wcf-windows-communication-foundation-running-and-debugging-quickly.aspx)
Task 1 – Adding a Windows Presentation Foundation (WPF) style app to host our WCF Service
This post is about hosting a WCF Application.
Hosting a service in a managed application is the most flexible option because it requires the least infrastructure to deploy. However, it is also the least robust hosting option, because managed applications do not provide the advanced hosting and management features of other hosting options in WCF, such as Internet Information Services (IIS) and Windows services.
Hosting in a Windows Service This scenario is enabled by the managed Windows service hosting option that is a long-running WCF service hosted outside of Internet Information Services (IIS) in a secure environment that is not message activated. The lifetime of the service is controlled instead by the operating system. This hosting option is available in all versions of Windows. – Windows services can be managed with the Microsoft.ManagementConsole.SnapIn in Microsoft Management Console (MMC) and can be configured to start up automatically when the system boots up. This hosting option consists of registering the application domain (AppDomain) that hosts a WCF service as a managed Windows service so that the process lifetime of the service is controlled by the Service Control Manager (SCM) for Windows services.
A WCF service that runs in the IIS environment takes full advantage of IIS features, such as process recycling, idle shutdown, process health monitoring, and message-based activation. This hosting option requires that IIS be properly configured, but it does not require that any hosting code be written as part of the application. You can use IIS hosting only with an HTTP transport.
Task 2 – Starting from the previous code base
The screen below is where we left off from the last post. You can download the code here: (http://sdrv.ms/1aOGgVQ).
Let’s begin where we left off from the last post. As you can see in the red box we have a client front-end and a WCF service backend. The client is a console application and the FlipCaseService is a WCF Service Library application.
Starting with the default project
We will now add a third project. It will act as the host application for the WCF service. Right mouse click on the solution and choose add new project.
Adding a WPF project
From the install templates choose Windows, WPF Application. Then provide a name below (WCFHostApplication) .
Adding a WPF Project
We will need to set two references from the WCF host appliHimation. The first reference is to the WCF service we created earlier. The second reference is to the system.servicemodel assembly, which provides us the basic functionality needed to leverage the WCF framework.
Add a new Reference
Selecting appropriate assembly below.
Add a second reference.
Add a new Reference
The second reference is the Visual Studio project for the previously written WCF service.
Setting a Project Reference
The screen below validates that our references are correctly set.
Verifying our References
Task 3 – Building the user interface for our WCF Client
Now we are going to edit our main screen for our WPF Hosting Application. From server Explorer double click on MainWindow.xaml. The designer should appear. Bring the tool box into view and drag a couple of buttons over to MainWindow.xaml.
Dragging some button controls
You can edit the XAML directly (lowest red box) or you can use the Visual Studio tooling (the properties window on the right). Or you can do a combination. Notice set two properties (Content and Name).
Naming the buttons and changing their caption
There’s a few things to notice. (1) were talking about the WCF host application. (2) you could just edit the XAML come directly. (3) it’s probably easier to right-click on the button in the designer and select properties. (4) this is where we provide the name for the control. (5) this is the text visible to the user (content).
Five things to realize
Screen below shows in the finished two buttons both in terms of the designer and the lower window containing xaml.
Verifying our changes to the UI of our WPF Application
Task 4 – Adding Code to make the WCF Service Operational
Let’s now turn our attention to the WCF Host application. Notice for the command button event cmdStartService_Click we have code that instantiates a new ServiceHost object. We will add Endpoints programmatically. The constructor for an endpoint is passed three arguments: (1) the contract or interface (2) the Binding (3) the URI that represents the endpoint into which the client connects. There are a few things to notice. Once is that the ServiceHost object has an Open() method. Also note we have both the using syntax as well as try/throw/catch.
Add some code to the WPF Application (code that makes our WCF Service Operational)
The configuration code below is showing what the client wants to connect to. It is better that you don’t try to create this file by hand. You can get it generated for you in 2 ways. The first way is with Adding a Service Reference from the client application to the WCF Service. This was explained in a previous post. The second way is with the svcutil.exe utility (explained previously). Note the red box on the right shows the endpoint configuration information for the client to use to connect to the WCF Service. For example, the client will connect by issuing at net.tcp://localhost:8081/FlipCaseNetTcp. The reason this is being discussed is that the WCF service in this post that we are creating must match the endpoint definition used by clients that are connecting to the WCF service.
Looking at the configuration file of the client (making sure it matches the WCF Service)
Let us now start the WPF Application that is hosting the WCF Service. Right mouse click on the WCFHostApplication project as seen below.
Starting the WPF Application (which makes the WCF Service operational and available to clients to connect to)
Task 5 – Starting our WCF Service
Note that the app is running. For the WCF Service to be operational, this command button in red must be clicked. It executes the code we just discussed.
Click on the WPF Application button to start the WCF Service programmatically
Task 6 – Connecting to the service from the client
By now the WCF Service is running. It is waiting for clients to connect to it. So now we will turn our attention to the client. Right mouse click on the WCF client application called ConsoleServiceClient and follow the debug commands.
Because the WCF Service is now available, we can start the client application to connect to the WCF Service
Notice that it worked. The client successful passed a parameter to the WCF Service and had that service flip the case of the string passed to it by the client.
In this post, you learned a few things:
- In this module we learned about client applications connecting to WCF Services.
- We illustrated how a host application can make a WCF Service operational.
- We could have chosen the console application template, but instead we chose a Windows Presentation Foundation (WPF) application template.
- You could extend a WPF style application much more than a console style application, providing a dashboard of an interface to enable a developer to watch what is happening with the service.