WCF study notes
Windows Communication Foundation (WCF) is the framework for building service-oriented applications. With WCF, data can be sent from one service endpoint to another as an asynchronous message. A service endpoint can be part of a continuously available service hosted by IIS or a service hosted in an application. An endpoint can be a service client that requests data from a service endpoint. A simple message can be a single character or a single word sent as XML, and a complex message can be a binary data stream. Some example scenarios include:
- A security service that handles corporate affairs.
- A service that provides current data to other services, such as traffic reports or other monitoring services.
- A chat service that enables two people to communicate or exchange data in real time.
- A panel application that polls one or more services to find data and present it in a logical representation.
- Workflows implemented using Windows Workflow Foundation are exposed as WCF services.
- Poll the service to find the latest data source for the Silverlight application.
Although such applications can be created before WCF exists, WCF makes endpoint development easier than ever. In summary, WCF Design provides a manageable way to create Web services and Web service clients.
WCF includes the following feature sets. For more information, see WCF Feature Details .
- One result of service-oriented use of the WS standard is that WCF allows you to create service-oriented applications. Service-Oriented Architecture (SOA) relies on Web services to send and receive data. These services have the general advantage of loose coupling, rather than hard coding from one application to another. A loosely coupled relationship means that any client created on any platform can connect to all services as long as the basic agreement is met.
WCF implements modern industry standards for Web services interoperability. For more information on supported standards, see Interoperability and Integration.
- Multiple message modes
exchange messages in one of several modes. The most common mode is the request/reply mode, where one endpoint requests data from another endpoint and the other endpoint replies. There are other modes, such as one-way messages, where only one endpoint sends a message and does not expect a reply. The more complex mode is the duplex switching mode, in which two endpoints establish a connection and send data back and forth, similar to an instant messaging program. For more information on how to implement different message exchange patterns using WCF, see Agreements .
- Service Metadata
WCF supports publishing service metadata in the format specified in industry standards such as WSDL, XML Schema, and WS-Policy. This metadata can be used to automatically generate and configure the client to access WCF services. Metadata can be published via HTTP and HTTPS, or published using the Web Services Metadata Exchange standard. For more information, see the metadata .
- Data Contracts
Because WCF is built using the .NET Framework, it also includes code-friendly methods for providing the protocols that you want to enforce. Data contracts are one of the common types of agreements. Essentially, when you code a service using Visual C# or Visual Basic, the easiest way to process the data is to create a class that represents the data entity using the attributes that belong to the data entity. WCF includes an integrated system that processes data in this easy way. After the class representing the data is created, the service automatically generates metadata that enables the client to conform to the type of data being designed. For more information, see Use Data Contracts
encrypts messages to protect privacy, and can require users to authenticate themselves before allowing messages to be received. Security can be achieved using well-known standards such as SSL or WS-SecureConversation. For more information, see Windows Communication Foundation security .
- Multiple transmission and encoding methods
can send messages through any of a variety of built-in transport protocols and encodings. The most common protocol and encoding is to send text-encoded SOAP messages using the Hypertext Transfer Protocol (HTTP) for use on the World Wide Web. In addition, WCF allows messages to be sent over TCP, Named Pipes, or MSMQ. These messages can be encoded as text or in an optimized binary format. Binary data can be efficiently transmitted using the MTOM standard. If the delivery or encoding method provided does not meet your needs, you can create your own custom transmission or encoding. For more information about the transport and encoding supported by WCF, see Transport in Windows Communication Foundation .
- Reliable Queuing Messages
WCF supports the use of reliable sessions through WS-ReliableMessaging and the use of MSMQ for reliable message exchange. For more information on reliable queued messaging support in WCF, see Queues and Reliable Sessions .
- Persistent message
persistence messages are never lost due to communication interruptions. Messages in persistent message mode are always saved to the database. If an outage occurs, the database will allow you to resume the message exchange after the connection is restored. Alternatively, you can use Windows Workflow Foundation (WF) to create persistent messages. For more information, see the workflow service .
WCF also supports transactions that use one of three transaction models: WS-AtomicTtransactions, APIs in the System.Transactions namespace, and Microsoft Distributed Transaction Coordinator. For more information on transaction support in WCF, see Transactions .
- AJAX and REST support
- The extensibility
WCF architecture has a number of extension points. It also provides a number of entry points that allow you to customize the behavior of your service if additional functionality is required. For more information on available extension points, see Extending WCF .
WCF is a flexible platform. Because of this extreme flexibility, WCF is also available in several other Microsoft products. If you are also using any of these products, you will benefit immediately by understanding the basics of WCF.
The first technology paired with WCF is Windows Workflow Foundation (WF). Workflow simplifies application development by encapsulating the steps in the workflow as “active.” In the first version of Windows Workflow Foundation, developers had to create a host for the workflow. The next version of Windows Workflow Foundation is integrated with WCF, allowing any workflow to be easily hosted in WCF services; this can be achieved by automatically selecting WF/WCF as the project type in Visual Studio 2010 and Visual Studio 2010.
Microsoft BizTalk Server R2 also leverages WCF as a communication technology. BizTalk is designed to receive data in a standardized format and then convert it to another format. Messages must be passed to their central message box, where you can use strict mapping or you can convert messages by using one of the BizTalk features, such as its workflow engine. Currently, BizTalk can use a WCF Line of Business (LOB) adapter to deliver messages to message boxes.
Microsoft Silverlight is a platform for creating interoperable, rich web applications that allow developers to create media-intensive websites such as streaming video. Starting with version 2, Silverlight has joined WCF as a communication technology to connect Silverlight applications to WCF endpoints.
Microsoft .NET Services is a cloud computing initiative that uses WCF to build Internet-enabled applications. Use .NET Services to create WCF services that operate across trust boundaries.
The hosting application server for Windows Server AppFabric is specifically generated for deploying and managing applications that use WCF to communicate. Hosting features include a wealth of tools and configuration options specifically designed to enable WCF-enabled applications.
The agreement defines various aspects of the messaging system. A data contract describes each parameter that makes up each message that a service can create or use. Message parameters are defined by an XML Schema Definition Language (XSD) document, which allows any system that understands XML to process the document. Message contracts use the SOAP protocol to define specific message parts that can be implemented when interoperability requires finer control over certain parts of the message. The service contract specifies the actual method signature of the service and is distributed as one of the supported programming languages (for example, Visual Basic or Visual C#).
Policies and bindings specify the conditions required to communicate with a service. For example, a binding must (at least) specify the transport (such as HTTP or TCP) and encoding used. Policies include security requirements and other conditions that must be met in order to communicate with the service.
The service runtime layer contains behavior that occurs only during the actual operation of the service, that is, the runtime behavior of the service. Contains the number of messages processed by the control, and the number of messages changes if the demand for the service grows to a preset limit. The error behavior specifies the action that should be taken when the service has an internal error, such as controlling the information passed to the client (too much information will provide an opportunity for the malicious user to attack). Metadata behavior controls whether and how metadata is provided to the outside. The instance behavior specifies the number of service instances that can be run (for example, a singleton specifies that all messages can only be processed with a single instance). With transaction behavior, you can roll back transactions that have already been processed on failure. Scheduling behavior is used to control how the WCF infrastructure processes messages.
Runtime processes can be customized with extensibility features. For example, the message checking function is used to check various parts of a message, and the parameter filtering function can be used to perform a preset operation based on a filter acting on the message header.
The messaging layer consists of channels. A channel is a component that processes a message in some way (for example, by authenticating a message). A set of channels is also known as a “channel stack.” The channel operates on messages and message headers. This is different from the service runtime layer, which mainly deals with the processing of the message body content.
There are two types of channels: transport channels and protocol channels.
The transport channel reads and writes messages from the network (or some other communication point externally). Some transport channels use an encoder to convert a message (represented as an XML Infoset) into a representation of the byte stream used by the network, or to convert a byte stream representation into a message. Examples of transport channels include HTTP, Named Pipes, TCP, and MSMQ. Examples of encoding include XML and optimized binaries.
Protocol channels often implement message processing protocols by reading or writing other headers of the message. Examples of such protocols include WS-Security and WS-Reliability.
The messaging layer describes the possible formats and exchange patterns of the data. WS-Security is an implementation of the WS-Security specification that enables security at the message layer. Message delivery is guaranteed through the WS-Reliable Messaging channel. The encoder provides a large number of encodings that can be used to meet the needs of the message. The HTTP channel specifies that the message should be delivered using the Hypertext Transfer Protocol. Similarly, the TCP channel specifies the TCP protocol. The transaction flow channel controls the message mode that has been transacted. Interprocess communication is possible through named pipe channels. Use the MSMQ channel to interoperate with MSMQ applications.
The final form of service is the program. Similar to other programs, the service must be run in an executable. This is called a “self-hosting” service.
Some services, such as IIS or Windows Activation Service (WAS), are “hosted,” that is, run in an external agent-managed executable. With WAS, you can automatically activate a WCF application when it is deployed on a computer running WAS. The service can also be run manually in the form of an executable (.exe file). The service can also run automatically as a Windows service. COM+ components can also be hosted as WCF services.