This article is a collection of the most frequently asked questions about Windows Communication Foundation at the beginner level.
What is WCF?
Microsoft calls WCF as a programming platform for building service-oriented applications. The Windows Communication Foundation is basically a unified programming model for developing, configuring and deploying distributed services. Microsoft has unified all its existing distributed application technologies (e.g. Microsoft Enterprise Services, ASMX web services, MSMQ, .NET Remoting etc) into the WCF platform. The code name for WCF was Indigo.
Why use WCF? What are the advantages of using WCF?
- Service Orientation is one of the key advantages of WCF. We can easily build service-oriented applications using WCF.
- Compared with ASMX web services, WCF services provide reliability and security with simplicity.
- As opposed to .NET Remoting, WCF services are interoperable.
- Various clients can interact with the same service using a different communication mechanism. This is achieved using service endpoints. A single WCF service can have multiple endpoints. So, developers can write code for a service once and just by changing the configuration (defining another service endpoint), it will be available for other clients as well.
- Extensibility is another key advantage of WCF. We can easily customize a service behavior if required.
What are the core components of a WCF Service?
A WCF service has at least the following core components:
- Service Class: A service class implemented in any CLR-based language and exposing at least one method.
- Hosting Environment: a managed process for running the service.
- Endpoint: for use by a client to communicate with the service.
What is the difference between WCF and ASMX Web services?
The basic difference is that ASMX web service is designed to send and receive messages using SOAP over HTTP only, whereas a WCF service can exchange messages using any format (SOAP is the default) over any transport protocol (HTTP, TCP/IP, MSMQ, Named Pipes etc).
You can find a detailed discussion of WCF vs ASMX Web services here.
What are the Endpoints in WCF? Explain the ABCs of endpoints.
For WCF services to be consumed, it's necessary that it must be exposed; clients need information about s service to communicate with it. This is where service endpoints play their role.
A service endpoint has three basic elements, also called the ABCs of an endpoint; they are Address, Binding and Contract.
- Address: It defines "WHERE". The Address is the URL that identifies the location of the service.
- Binding: It defines "HOW". The Binding defines how the service can be accessed.
- Contract: It defines "WHAT". The Contract identifies what is exposed by the service.
What is a WCF Binding? How many different types of bindings are available in WCF?
Bindings in WCF actually define how to communicate with the service. The Binding specifies the communication protocol as well as the encoding method to be used. Optionally, binding can specify other important factors like transactions, reliable sessions and security.
Another WCF Tutorial gives a more detailed understanding of the Binding concept in WCF.
There are various built-in bindings available in WCF, each designed to fulfill some specific need.
For details on various binding types, please follow the link to WCF bindings.
Can we have multiple endpoints for different binding types in order to serve various types of clients?
Yes, we can have multiple endpoints for different binding types. For example, an endpoint with wsHttpBinding and another one with netTcpBinging.
What are the hosting options for WCF Services? Explain.
For a service to host, we need at least a managed process, a ServiceHost instance and an Endpoint configured. The possible approaches for hosting a service are:
- Hosting in a Managed Application or Self Hosting:
- Console Application
- Windows Application
- Windows Service
- Hosting in a Web Server:
- IIS 6.0 (ASP.NET Application supports only HTTP)
- Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP, NamedPipes, MSMQ.
What are Contracts in WCF?
A Contract is basically an agreement between the two parties i.e. Service and Client. In WCF, Contracts can be categorized as behavioral or structural.
- Behavioral Contracts define what operations a client can perform on a service:
- ServiceContract attribute is used to mark a type as Service contract that contains operations.
- OperationContract attributes is used to mark the operations that will be exposed.
- Fault Contract defines what errors are raised by the service being exposed.
- Structural Contracts
- DataContract attribute define types that will be moved between the parties.
- MessageContract attribute define the structure of the SOAP message.
What Message Exchange Patterns are supported by WCF?
- One Way
It's the default pattern. In this pattern, a response message will always be generated to the consumer when the operation is called, even with the void return type. In this scenario, responses will have an empty SOAP body.
In some cases, we are interested in sending a message to a service in order to execute certain business functionality but not interested in receiving anything back. OneWay MEP will work in such scenarios. If we want a queued message delivery, OneWay is the only available option.
The Duplex MEP is basically a two-way message channel. In some cases, we want to send a message to a service to initiate longer-running processing and require a notification back from the service to confirm that the requested process has completed.