Share via


WCF Service Behaviors - How to Generate Service Metadata to simplify WCF Client Application Development

Overview

This post is about extensibility, interms of the behavior of a service. We discussed Bindings in previous posts and in this section we want to discuss service behaviors, which is a runtime that sits on top of the channel layer.

Service Behavior extensions enable the user to customize service or endpoint behaviors. You can use them to validate service configuration or modify runtime behavior in client and service applications.

One of the behaviors we want to control potentially is service metadata. Publishing service metadata makes the metadata available using standardized protocols, such as WS-MetadataExchange (MEX) and HTTP/GET requests.

The term metadata refers to "data about data". The term is ambiguous and in this context it applies to metadata about behavior. This metadata is important because it makes it very easy for a client to connect up to a WCF Service. This metadata takes two forms: a client proxy file any configuration file the client will add into the project using Visual Studio.

We really want learn:

  • How to make it very easy for a client application to connect to a WCF service
  • How to generate two important files for the client to use to simplify communication with a WC of service
    • Client proxy file
    • Client configuration file
  • How to use svcutil.exe to generate these files
  • How to get service metadata from a browser

Objectives

In this hands-on lab, you will learn how to:

Prerequisites

The following is required to complete this hands-on lab:

Setup

In order to execute the exercises in this hands-on lab you need to set up your environment.

  1. Start Visual Studio and open the previous project here: (https://blogs.msdn.com/b/brunoterkaly/archive/2013/10/18/getting-started-with-wcf-windows-communication-foundation-running-and-debugging-quickly.aspx)
  2. Also complete related posts later in my series.

How to modify bindings using the WCF Service Configuration Editor

In the next section we will enable the Reliable Messaging protocol. It is useful where the quality of network connectivity is potentially very poor. The WS-ReliableMessaging (or WS-RM) protocol for creating reliable communication paths over unreliable connections. Very simply, it provides the infrastructure that provides explicit acknowledgement of messages into the communication flow

Task 1 – Starting the WCF Service Configuration Editor

This task is about using the WCF Servcie Configuration Editor to create a new binding configuration.

  1. The WCF project will be modified. A service behavior will be added. We will use the WCF configuration tool to make the changes. We essentially want to allow client applications to be able to generate their own local copies of the configuration file as well as the service proxy to be used by the client to connect the service. We will explore that capability soon. For now right mouse click on the app.config file and choose Edit WCF configuration.

    Image001

    Editing the WCF service configuration

  2. Inside of the WCF configuration editor, right mouse click on the advanced configuration, then service behavior, then new service behavior configuration.

    Image002

    Adding a new service behavior configuration

  3. There are two main steps when adding a service behavior configuration. The first step is to name it. The second step is to act a behavior element extension. Provide a name to the service behavior then click add below.

    Image003

    Naming a service behavior configuration

  4. There are a variety of available elements to choose from. Select from the list below, choosing the one called Service metadata. Adding this behavior extension allows client applications to quickly generate proxies and configuration data, enabling them to easily communicate with the WCF service.

    Image004

    Choosing the correct behavior element extension

  5. As you can see below, both the name and behavior elements have been entered and selected.

    Image005

    Confirming the service behavior

  6. By clicking on serviceMetadata in the configuration section of the WCF configuration editor, you will be allowed to set some general properties for serviceMetadata. HttpGetEnabled should be set to true and the HttpGetUrl should be https://localhost:8080/flipcase/metadata.

    Image006

    Setting the properties for our new service behavior

  7. Once you have defined a new service behavior, it needs to be attached as an attribute to the overall service configuration. This can be done easily through the WCF configuration editor.

    Image007

    _Attaching the service behavior to the WCF service. _

  8. Now that we have modified app.config, it is time to save the changes so that we can run the service and test that it works. Make sure to select File Save or File Closed, answering yes to the save changes dialog box.

    Image008

    Saving the changes

  9. Notice that the WCF configuration editor made the appropriate additions to the app.config file.

    Image009

    Inspecting the app.config file

  10. Notice that the behavior configuration section of the service has been appropriately defined with servicebehaviormetadata. This is the actual part where you attach your service to the behavior previously defined.

    Image010

    Verifying the service behavior

  11. It is now time that test our modifications with the WCF service project whose service behavior has been modified. We will need to start the WCF service to begin this process.

    Image011

    Starting the backend WCF service in the debugger

  12. If your project is properly configured from the default project template, you will see the WCF client appear. Once it appears the service is running and you can execute code to connect to it and communicate with it.

    Image012

    Validating that the WCF test client is starting

  13. Notice that we you can use the browser to validate the service behavior that we added to our project. You simply need to type in the correct metadata endpoint that we defined previously.

    Image013

    Testing our service behavior

  14. The svcutil.exe makes it very straightforward to generate configuration information and proxy information that simplifies the development of the client which is communicating with the WCF service. The service behavior that we added make it possible for this utility to get the necessary information and save for disk. As you can see below, two files are generated and save for disk. One of the files is the configuration file and the other file is a client service proxy. The client will use the service proxy to communicate with the WCF service. Using Visual Studio you will simply add these two files to your client project.

    Image014

    Using the svcutil.exe utility

  15. As you can see below, FlipCaseService.cs is the proxy file that the client can use to connect to the WCF service. Notice that the class definitions, including data contracts are visible. For example, StringData is defined in this proxy file, which makes it very easy for the client to get IntelliSense and to be able to pass and receive StringData data structures. Except for the references that are added, this is the same result as what we got when we said add service reference from a client.

    Image015

    Viewing the client proxy file

  16. Output.config is the configuration file used by the client to connected the WCF service. It is generated also by the svcutil.exe utility.

    Image016

    Viewing the generated configuration file

Summary

In this post, you learned a few things:

  • How to make it very easy for a client application to connect to a WC of service
  • How to generate two important files for the client to use to simplify communication with a WC of service
    • Client proxy file
    • Client configuration file
  • How to get service metadata from a browser
  • How to use svcutil.exe to generate these files