An Introduction to WCF: Windows Communication Foundation
Saurabh Barot
Co-Founder & CTO | Certified Enterprise Architect with TOGAF | Tech Advisor for IT Strategy Operation & Implementation
Windows Communication Foundation (WCF) is a framework by Microsoft for building service-oriented applications on the .NET platform. In this comprehensive guide, we will take a look at the basics of WCF, the problems it solves, features, architecture, and how to build WCF services and clients.?
Understanding Service-Oriented Architecture?
Before diving into WCF, it's important to understand Service-Oriented Architecture (SOA), which provides the foundation for WCF services.??
SOA is an architectural approach for building distributed systems as modular services that can be easily consumed and reused. These services have the following characteristics:?
SOA enables building complex, scalable and distributed applications by composing loosely coupled services across platforms and organizations. Now let's see how WCF enables building SOA solutions on .NET.?
?Need for WCF?
Earlier Microsoft provided various technologies for building services like ASP.NET Web Services (ASMX), .NET Remoting, Enterprise Services etc. However, these existed as independent siloed technologies making it hard to build complete solutions.??
WCF unified all these into a single service development framework with the following goals:?
WCF provides a comprehensive approach to implement SOA principles for building distributed and decoupled system.?
?Key Concepts of WCF?
Some key concepts to understand in WCF:?
SOAP and REST??
WCF services can be exposed over SOAP (Simple Object Access Protocol) or REST (Representational State Transfer) endpoints. SOAP involves exchanging XML messages while REST uses plain HTTP requests.?
Message Exchange Patterns?
WCF supports various message exchange patterns like one-way (fire and forget), request-reply, duplex (two-way) communication.?
Endpoints?
Endpoints represent a distinct addressable point where a service can be accessed using a specific protocol/data format combination. Services can expose multiple endpoints.?
Addresses, Bindings and Contracts?
These constitute the "ABC" of WCF:?
These three components fully describe a service endpoint in WCF.?
Hosting??
WCF services need to be hosted in a host process like a console app, Windows service, or IIS to be consumable. The host initializes and makes the service available. These key concepts make up the core of WCF architecture and programming model.?
WCF Architecture and Components?
WCF is designed based on SOA principles and contains the following key components:?
These components allow creating and exposing services independently of the underlying implementation and communication mechanisms.?
?Defining Service Contracts?
Service contracts define what service operations a client can perform. These are defined as .NET interfaces with attributes as follows:?
[ServiceContract]?
public interface ICustomerService?
{?
? [OperationContract]?
? Customer GetCustomer(int id);?
? [OperationContract]?
? void UpdateCustomer(Customer customer);??
}?
A service implementing this interface must define these methods. Multiple interfaces can be combined to expose a larger contract.?
Defining Data Contracts??
Data contracts define the data types used in service operations. These are regular .NET classes annotated as:?
[DataContract]?
public class Customer?
{?
? [DataMember]?
? public int ID { get; set; }?
?
? [DataMember]?
? public string Name { get; set; }?
}?
Data contracts maximize interoperability across platforms.?
Service and Data Contract Separation?
A key benefit of separating service and data contracts is that they can evolve independently. Service contract being an interface remains unchanged while data contract can add new properties without requiring service modification.?
This separation hence allows independent evolution and maintainability.?
领英推荐
?Implementing Service Contract?
To implement a WCF service, we create a class deriving from System.ServiceModel.ServiceBase and implementing the contract interface:?
[ServiceBehavior(Namespace="https://microsoft.com/customerservice")]?
public class CustomerService : ServiceBase, ICustomerService?
{?
? // Implement contract methods?
}??
Service and data contracts are hence decoupled from implementation.?
?Hosting WCF Services?
For clients to discover and consume the service, it needs to be hosted in a host process. WCF provides various hosting options:?
IIS Hosting?
Internet Information Services (IIS) provides built-in hosting capabilities for WCF services. Services are hosted in WAS (Windows Process Activation Service) processes.?
Self-Hosting??
WCF service can self-host in a custom Windows service or console app. This allows full control but extra effort.??
Windows Process Activation Service (WAS)?
In addition to IIS, WAS enables hosting WCF services in other processes like Windows services. Useful for services not exposed over HTTP. Proper hosting environment enables optimal stability, scalability and manageability of services.?
Exposing Endpoints?
A key WCF concept is endpoints which are a combination of address, binding and contract that specify how a service can be accessed.??
For example, the same service can expose:?
This enables exposing services optimally for diverse audiences. Endpoint configuration is done at hosting time.?
Consuming WCF Services?
To consume a WCF service, there are two approaches:?
Channel Factory + Channel?
This involves manually creating a channel factory for the endpoint, constructing a channel and invoking service operations. More code but full control.?
Service Reference?
Visual Studio can automatically generate strongly typed proxy classes for the service contract by adding a service reference. Simple usage but limited flexibility. Either way, the client only deals with the service contract, shielding implementation details.?
Service Metadata and Discovery?
For clients to easily discover and consume WCF services, services expose metadata using standard protocols:?
WSDL?
Web Service Description Language (WSDL) provides metadata like service endpoint details, operations, message formats etc. WCF can generate WSDL automatically.?
WS-Metadata Exchange??
Provides dynamic querying of metadata from service endpoint. Supports GET requests for metadata.?
UDDI?
Universal Description, Discovery and Integration (UDDI) defines service registries where services can publish metadata to be easily found. Using open standards for metadata and discovery helps make services accessible across platforms and frameworks.?
Security?
WCF network operations depend on security. Make sure that all data is encrypted, authenticated, and authorized. For a stable deployment, hire .NET developers who are conversant with WCF security features.?
Transactions?
WCF integrates seamlessly with underlying transaction frameworks like System.Transactions and MSDTC to make services transactional:?
Transactions enable building robust services that safeguard data integrity.?
Reliability and Availability??
For mission-critical services, WCF provides reliability patterns like:???
These constructs harden services for 24/7 production use under load.?
Performance and Scalability?
WCF includes optimization features for building high-performance services:?
These allow smoothly scaling services across user load and data volumes.?
WCF vs Web API?
WCF provides a general framework for building services on .NET. For building HTTP REST services, Web API provides a more streamlined, REST-optimized experience.?
Key differences:?
So, in summary, Web API is best for public REST services while WCF provides full breadth across transport protocols.?
Conclusion?
WCF provides a comprehensive service-oriented framework for building distributed, interoperable and secure applications on the .NET platform. Its flexibility across protocols, robust security, transactions and reliability makes it a great choice for both internal and external enterprise SOA applications.? For public HTTP REST services, Web API offers a simpler programming model. Understanding when to adopt WCF vs Web API based on the kinds of services required is important. But both continue to be crucial frameworks for .NET services. With Microsoft adding ongoing improvements in newer versions, WCF remains an indispensable part of enterprise .NET architecture.?