WCF provides one framework for unified communications. The main assembly of WCF is “System.ServiceModel“.
1. Contract Model
By adopting contracts, WCF provides the single way to create services.
- Service Contract: Interface
- Operation Contract: Methods
- Data Contract: Data classes
WCF uses endpoints to define how the communication is established in a server and a client. Services expose endpoints and clients consume them.
- Address: Where – URL
- Binding: How – Http / Msmq / Tcp
- Contract: What
For a single service contract, you can define multiple endpoints.
<system.serviceModel> <behaviors> ... </behaviors> <services> <service name="SimpleService.HelloWorldService" behaviorConfiguration="HelloWorldServiceServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="IHelloWorldService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> </system.serviceModel>
[Note] WCF 4 simplified the configuration. <service> is optional in .NET 4. The endpoints is configured using the base address and contract name.
Bindings specify how WCF constructs underlying communication channels.
- BasicHttpBinding: SOAP via HTTP
- WebHttpBinding: REST via HTTP
- WSHttpBinding: SOAP + WS* via HTTP
- NetTcpBinding: Cross-machine via TCP
- NetPeerTcpBinding: Cross-machine via P2P
- NetNamedPipesBinding: Same-machine via IPC
- NetMsmqBinding: via MSMQ
[Note] NetXXX bindings are designed for .NET-to-.NET communications.
4. Metadata and Client
Metadata is used to define services and endpoints. A client needs this metadata to consume the service. The metadata is written in Web Services Description Language (WSDL).
When you work with Visual Studio, the client can easily setup the connection to the server using “Service Reference“.
5. WCF Messages
WCF messages are represented by an abstract class “System.ServiceModel.Channels.Message“.
There are 2 types of messages:
- Untyped messages: represented by a generic “Message” type
- Typed: defined as a .Net class and annotated with special mapping attributes (DataContract, DataMember)