WCF Tutorial, Part 1: Introduction

01. December 2013 WCF 0

Overview

Prior to WCF, different communication technologies such as ASP.NET Web Services, .NET Remoting, and ASP.NET’s Web Service Enhancements (reliability, platform independent security, and atomic transactions) have been used. DCOM which is used by .NET Enterprise Services with its automatic transaction support is even faster than .NET Remoting.

Windows Communication Foundation (WCF) introduced by .NET 3.0 which includes all the features from these predecessors and combines them into one programming model.

WCF Offers:

  • Hosting for components and services – WCF can be hosted in the ASP.NET runtime, a Windows service, a COM+ process, or a Windows Forms application.
  • Declarative behavior
  • Communication channels – HTTP, TCP, IPC channels, or custom channels
  • Security infrastructure – WSE
  • Extensibility
  • Support of previous technologies

The goal is to send and receive messages between a client and a service. This should be done in a platform-independent way. Typically a service offers an endpoint that is described by a contract, a binding, and an address (ABC):

  • Contract: defines the operations offered by the service;
  • Binding: gives information about the protocol and encoding; and
  • Address: is the location where the service can be accessed.

The client needs a compatible endpoint to access the service.

Figure 1 – Components Participate in WCF Communication

  • The client invokes a method on the proxy.
  • The proxy offers methods as defined by the service but converts the method call to a message and transfers the message to the channel.
  • The channel has a client-side part and a server-side part that communicate across a networking protocol.
  • From the channel, the message is passed to the dispatcher, which converts the message to a method call invoked with the service.

WCF supports several communication protocols:

  • SOAP, A platform-independent protocol that is the foundation of several web service specifications to support security, transactions, reliability.
  • Web Services Description Language (WSDL), Offers metadata to describe a service.
  • Representational State Transfer (REST), Used with RESTful Web services to communicate across HTTP.
  • JavaScript Object Notation (JSON), Enables easy use from within JavaScript clients. .NET includes a data contract serializer to create objects with the JSON notation.

Contracts

A contract defines what functionality a service offers and what functionality can be used by the client. The contract can be completely independent of the implementation of the service. The contracts defined by WCF can be grouped into four different contract types: Data, Service, Message, and Fault. The contracts can be specified by using .NET attributes:

  • Data contract, the data contract defines the data received by and returned from the service. The classes used for sending and receiving messages have data contract attributes associated with them.
  • Service contract, the service contract is used to define the WSDL that describes the service. This contract is defined with interfaces or classes.
  • Operation contract, the operation contract defines the operation of the service and is defined within the service contract.
  • Message contract, if complete control over the SOAP message is needed, a message contract can specify what data should go into the SOAP header and what belongs in the SOAP body.
  • Fault contract, the fault contract defines the error messages that are sent to the client.

Service Behavior

Specifies the internal execution behavior of a service contract implementation.

Apply the ServiceBehaviorAttribute attribute to a service implementation to specify service-wide execution behavior. (To specify execution behavior at the method level, use the OperationBehaviorAttribute attribute.) This attribute can be applied only to service implementations.

Binding

Bindings are objects that are used to specify the communication details that are required to connect to the endpoint of a Windows Communication Foundation (WCF) service. Each endpoint in a WCF service requires a binding to be well-specified.

The information in a binding can be very basic, or very complex. The most basic binding specifies only the transport protocol (such as HTTP) that must be used to connect to the endpoint. More generally, the information a binding contains about how to connect to an endpoint falls into one of the following categories:

  • Protocols – Determines the security mechanism being used: either reliable messaging capability or transaction context flow settings.
  • Encoding – Determines the message encoding (for example, text or binary).
  • Transport – Determines the underlying transport protocol to use (for example, TCP or HTTP)

Depending on the binding, different features are supported. The bindings starting with WS are platform independent, supporting web services specifications. Bindings that start with the name Net use binary formatting for high-performance communication between .NET applications.

Hosting

WCF can be host in Windows service, a COM+ application, WAS (Windows Activation Services) or IIS, a Windows application, or just a simple console application. When creating a custom host with Windows Forms or WPF, you can easily create a peer-to-peer solution.

Clients

A client application needs a proxy to access a service. There are three ways to create a proxy for the client:

  • Studio Add Service Reference, this utility creates a proxy class from the metadata of the service.
  • ServiceModel Metadata Utility tool (Svcutil.exe), you can create a proxy class with the Svcutil utility. This utility reads metadata from the service to create the proxy class.
  • ChannelFactory class, this class is used by the proxy generated from Svcutil; however, it can also be used to create a proxy programmatically.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.