WCF Service Applications

WCF services have become less prevalent because web developers prefer the more straightforward programming model of REST-based services (and the Web API). However, WCF still has its place for writing robust services that can be called by multiple clients using multiple endpoints/protocols.

Like web services, WCF services have their own set of terms. It is important that you have a baseline understanding of these before trying to understand the key concepts related to WCF service applications:


Note

It is not imperative that you understand all these terms to work with WCF. However, it can be helpful to have a cursory understanding when you are creating and configuring these services.


Image WCF service—A WCF service is a set of logic that you expose to multiple clients as a service. A service might have one or more service operations (think methods). A WCF service is exposed to clients through one or more endpoints that you define. Each endpoint has a binding and behaviors (see the “Endpoint” entry in this list). In this way, you can create a single service and configure it to work efficiently with multiple clients (such as HTTP, TCP, and named pipes).

Image WCF client—A WCF client is an application that Visual Studio generates to call a WCF service. You can create a WCF client by adding a service reference to a client application. The client application is the actual application that consumes the results of the WCF service. Think of the WCF client as the go-between or proxy that helps connect your client code to the WCF service.

Image Host—A host is a process that runs (or hosts) the WCF service. This process controls the lifetime of the service. It’s similar to the way ASP.NET provides a host for web services. You can write your own service host or allow a service to be self-hosted.

Image Contract—Contracts define your WCF services. This is essentially the public contract you guarantee between your service and any clients. There is a service contract that defines the content of the service (such as its operations). There is also an operation contract for each service operation. This contract indicates the parameters and return type of the service operation. There are also message, data, and fault contracts.

Image Endpoint—Endpoints are configured for each service operation. An endpoint is where messages for your service are sent and received. Each endpoint defines both an address and binding for communicating with a service. For example, you might have one endpoint that works with SOAP over HTTP. You might have another endpoint for the same service that enables the service to work with MSMQ. In this way, you can add and configure endpoints to your service independently of actually coding the service. This ensures that your service can be configured to work efficiently with both existing and new clients.

Image Address—An address is a unique URI for a given service. The address is used by calling clients to locate the service. The URI also defines the protocol that is required to reach the address, such as HTTP or TCP. Each endpoint you define for your service can have its own address.

Image Behaviors—A behavior defines the way an entire service, a specific endpoint, or a specific service operation behaves. You can define behaviors for such things as security credentials and service throttling.

Image Binding, binding element, and channel—Endpoints have bindings that define the way the endpoint communicates. A binding includes information about transport, encoding, and security. For example, you can configure an endpoint’s binding to work with the HTTP transport encoded as text.

Image A binding is made up of binding elements. Each element represents a single portion of the binding. You might, for example, have a binding element for the encoding and another for the transport. Binding elements and their configuration are implemented as channels. The binding elements are stacked together to create this channel. In this way, the channel represents the actual implementation of the binding.

Visual Studio provides various tools that make building WCF services easier. If you know that you intend to host your WCF service inside a website under IIS, you can actually add a WCF service to a website using the item templates. However, if you want to host outside of IIS or to decide on hosting at a different time, you can create a WCF project. In either case, you then define your service contract (as an interface). Next, you implement the service contract. Finally, you configure communication endpoints for the service. After your service is complete, you pick a hosting model and deploy it accordingly. Clients can then access the service. Let’s take a look at each of these steps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset