Introduction to WCF 1--brief introduction to wcf
What is WCF?
Windows Communication Foundation (WCF) is a series of application frameworks developed by Microsoft to support data Communication. It can be translated as a Windows Communication development platform.
Integrates the existing. net Remoting, WebService, and Socket mechanisms for windows communications, and integrates Http and Ftp-related technologies. WCF is the unification of these technologies
According to the definition on MSDN: WCF provides a basis for. NetFramework to write code for communication between components, applications, and systems. The Design of WCF follows the service-oriented principle. A service is a piece of code that can interact with a message. Services are passive. They do not work until the message is passed in. The client is the initiator. The client sends a message to the service to request work.
Is the best practice for developing distributed applications on Windows.
Suppose we want to develop a new application for a car rental company for Car Rental booking service. The taxi booking service will be accessed by multiple applications, including Call Center, J2EE-based taxi booking service, and Partner Application)
With WCF, the implementation of this solution is much easier. As shown in. Therefore, the taxi booking application can use this technology to implement communication between all its applications.
WCF can use Web services for communication. Therefore, the interoperability with other platforms that support SOAP (such as mainstream J2EE-based application servers) becomes simple and clear.
You can also configure and expand WCF to communicate with Web services that use messages that are not based on SOAP.
Performance is a critical consideration for most businesses. The goal of developing WCF is to make it one of the fastest distributed application platforms developed by Microsoft.
WCF provides a unified and efficient development platform for secure and reliable service-oriented applications.
WCF has the following advantages:
1. Uniformity
2. Interoperability
3. Security and trustworthiness
4. Compatibility
Understanding Service-Oriented Architecture)
SOA is a software system architecture that is implemented by connecting independent functional entities that can complete specific tasks to meet the needs of business integration in the Internet environment. SOA is a component model that associates different functional units (called services) of an application with well-defined interfaces and contracts between these services.
SOA points out that the current system should be flexible enough to allow changes to the existing architecture without disrupting the current operating architecture and infrastructure.
SOA principles:
- Clear borders
- Service autonomy
- Compatibility based on policy
- Shared Mode (schma) and contract
Introduction to the WCF Architecture
Contract and description
The contract defines all aspects of the message system.
Data contract:Parameters in the service;
Message contract:Use the specific message part of the SOAP protocol;
Service contract:Methods In the service;
Policy and binding: Sets security policies or other conditions, and binds the specified transmission mode and encoding.
Service runtime
Behavior Control during service running
Behavior restriction:Control the number of messages processed;
Incorrect behavior:Operations processed when an internal error occurs;
Metadata Behavior: Whether to provide metadata and metadata externally;
Instance behavior:Number of running service instances;
Transaction behavior:Process transactions;
Scheduling behavior:Controls how WCF processes messages;
Message transmission
Message transmission layer:Describes the data exchange format and transmission mode.
The message transmission layer is composed of channels. A channel is a component used to process messages and is responsible for organizing and transmitting messages in a consistent manner. The channel is used for transmission layer, protocol layer, and message acquisition. Channels at different levels form a channel stack.
The channel operates on the message and message headers, and the message body is operated when the service is running.
Two types:Transmission Channel and protocol channel.
Transmission Channel:Reads and writes messages from the network. The transmission channel converts messages to byte streams used for network transmission through the encoder, and exchanges byte streams into messages. Example of transmission channel: HTTP channel, named pipe, TCP, MSMQ, etc;
Protocol channel:The message protocol is implemented by reading or writing the message header. Examples of protocol channels include WS-Security and WS-Reliability.
Bearer and Activation
Service Host:It is responsible for the lifecycle and context of the WCF Service, starts and stops the WCF Service, and provides basic management functions for the Control Service.
Introduction to basic concepts of WCF
Contract)
The basic concept of WCF is to define the agreement between the two parties through a Contract. The Contract must be embodied in an interface, the actual service code must be derived and implemented by these contract interfaces. There are four types of contracts:
Data Contract (Data Contract), which defines the Data format when both parties communicate. Service Contract, which defines the Service. Operation Contract, which defines the methods provided by the Service. MessageContract, which specifies the specification for rewriting the message content during communication. A contract in WCF is shown in the following code:
[Csharp]View plain copy
Using System;
Using System. ServiceModel;
Namespace Microsoft. ServiceModel. Samples {
[ServiceContract (Namespace = "http://Microsoft.ServiceModel.Samples")] // Service Contract
Public interface ICalculator
{
[OperationContract] // operation contract
Double Add (double n1, double n2 );
[OperationContract] // operation contract
Double Subtract (double n1, double n2 );
[OperationContract] // operation contract
Double Multiply (double n1, double n2 );
[OperationContract] // operation contract
Double Divide (double n1, double n2 );
}
}
Binding)
Since WCF supports HTTP, TCP, Named Pipe, MSMQ, Peer-To-Peer TCP and other protocols, HTTP is divided into basic HTTP support (BasicHttpBinding) and WS-HTTP support (WsHttpBinding ), TCP also supports communication methods such as NetTcpBinding and NetPeerTcpBinding. Therefore, both parties must unify the communication protocol and ensure consistency in encoding and format.
An example of setting Communication Protocol binding is as follows:
[Html]View plain copy
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. serviceModel>
<! -- Set the closed service information -->
<Services>
<Service name = "CalculatorService">
<Endpoint address = "" binding = "wsHttpBinding" bindingConfiguration = "Binding1" contract = "ICalculator"/>
</Service>
</Services>
<! -- Set the information for binding the communication agreement -->
<Bindings>
<WsHttpBinding>
<Binding name = "Binding1">
</Binding>
</WsHttpBinding>
</Bindings>
</System. serviceModel>
</Configuration>
Although WCF can also use SOAP for communication, it is similar to ASP. NETXML Web Services are different. Therefore, some technical articles will. net xml Web Services is called ASMX Service.
WCF Services can be mounted to Console Application, WindowsApplication, IIS (ASP. NET) Application, Windows Service, and Windows Activation Services, but most of them are mounted to Windows Services.
Main built-in system binding:
Functions supported by system binding:
Performance Comparison of various binding methods:
Endpoint
-An endpoint is a structure used to send or receive messages (or perform these two operations. An endpoint includes a location (Address) node that defines the destination to which a message can be sent, including a location (Address) that defines the destination to which the message can be sent) A communication mechanism specification (binding) that describes how a message should be sent, a definition of a group of messages that can be sent or received at this location (or both) (Service Agreement) -This definition also describes the messages that can be sent.
The endpoint address is represented by the EndpointAddress class, which contains a Uniform Resource Identifier (URI) indicating the service address. Most transmitted address Uris contain four parts.
For example,
The URI "http://www.cnblogs.com: 80/jiekzou" has the following four parts:
-Solution: http:
-Computer: www.cnblogs.com
-(Optional) Port: 80
-Path:/jiekzou
Metadata
The so-called "metadata" refers to the data describing the data, that is, the information about the service contracts, method contracts, data contracts, and endpoints of the current service. The "metadata endpoint" is the end point for exposing metadata to the outside world. When the client adds a reference to the WCF Service, it first obtains the contract information and Endpoint information of the server through metadata, and then creates a proxy class on the client, the process of calling the WCF Service on the client is actually the process of calling the WCF Service through the proxy class.
Host ):An application domain and process in which the service runs.
Differences between WCF and Webservice
1. WebService:Strictly speaking, it is an industry standard, not a technology. XML extension markup language is used to represent data (this is the key to cross-language and platform ). Microsoft's Web Service implementation is called ASP. NET Web Service. It uses the Simple Object Access Protocol Soap to implement data interaction between applications in a distributed environment.
Description of the service interface. In addition, Webservices can be registered to the UDDI Center for customers to find and use. Later, Microsoft implemented ASP. NET Web Service Security, performance, data encryption, decryption, hosting host and other aspects of the extension, called the WSE series, this is an over-product, up to WSE3.0. later is the WCF era.
2. WCF:In fact, it is ASP to a certain extent. NET WebService, because it supports the industry standard and Core Protocol of Web Service, ASP. NET WebService and WSE can do almost all the things they can do. cross-platform and language are not a problem (data also supports XML formatting and provides their own formatter ).