"If you only want to be able to reasonably apply WCF in the project Program Personnel can have two options: one is "knowing it but not knowing why". As long as you have mastered the basic knowledge of WCF, it is enough for general applications. It is easy to achieve this. Microsoft adopts the consistent method to elegantly present the WCF technology to developers and encapsulate complicated implementation logic, provides easy-to-call class libraries and related tools, allowing developers to quickly develop WCF programs. Another method is the internal implementation of deep mining WCF, which is a requirement of the WCF experts. If we want to use WCF to implement SOA solutions, we will encounter many advanced WCF applications. How can we reasonably and effectively apply WCF and extend it according to the actual situation of the project, it becomes a problem that the WCF experts must solve.
Therefore, if you want to learn WCF, you must find the motivation and goal of your learning and arrange your learning schedule properly. This is the correct learning method. This article attempts to give some tentative explanations and analysis on some basic concepts of WCF, and organize them in the form of Q & A. I hope to answer some questions that I hope to learn from WCF, but the developer is still wandering outside the door.
1. What is WCF?
From the perspective of the position of WCF, it is included in. Net 3.0 (also including. Net 3.5. The only difference between. Net 3.0 and. NET 2.0 is that. Net 3.0 contains WCF, WPF, WF (or cardspace. Therefore, we think that WCF is part of the. NET Framework and it does not seem to be too much. The most important thing is that WCF cannot be separated. the net framework exists independently (but not the WCF client can call the WCF Service). Therefore, although WCF is specially released by Microsoft to meet the development requirements of SOA solutions, however, it is not a framework like spring or struts, nor a container or server like EJB. Microsoft is truly in line with the SOA Enterprise Application Server role. I think it should be BizTalk Server.
Strictly speaking, WCF is a set of specialized classes specifically used for service customization, publishing and running, and message transmission and processing, that is, the so-called "class library ". These classes are organized in a certain way to work together and provide developers with a unified programming mode. The reason why WCF is special is that it deals with common scenarios. the net class library is different. It is mainly used to process inter-process and inter-machine message transmission and processing. It also introduces the design idea of SOA and publishes and runs it as a service, to facilitate the client's cross-process and machine calls to services. In fact, WCF is a set of developers of distributed processing. It integrates DCOM, remoting, Web Service, WSE, and MSMQ, reducing the learning curve of distributed system developers, and unified development standards.
The difference between WCF and other class libraries lies in that WCF fully embodies the concept of runtime environment. For developers who used WCF earlier, they may know that. NET 2.0 to develop WCF, you also need to download a special runtime component version 3.0, which contains the content such as WCF and WF. In. net, the so-called "host" concept always exists. The entire. NET Framework (or CLR) can be considered as a large host, just like a Java virtual machine. Since WCF has special requirements for services, services must be published and run on the server. for clients, services must be called. for developers, you need to write definitions, releases, runs, and CALLS services. Code . Services can only run on specific hosts. These Hosts can be console application processes, windows or web application processes, or Windows service processes, or the most common IIS host. Inside the host, the channel stack is encapsulated, which also includes processing for protocols, encoding, message transmission, and proxy. At the top of the channel layer, an advanced Runtime is provided for developers of applications.
Therefore, we can think that WCF is. net Framework 3. X contains a set of runtime environments for service customization, publishing and running, message transmission and processing, and related classes, it provides sdks for developing and deploying services on the Windows platform. Shows the approximate composition:
2. How does WCF run?
If you analyze the running mechanism of WCF from a macro perspective, its implementation is not complicated. The architecture of WCF is implemented based on an interceptor mechanism. The component responsible for transmitting and intercepting messages is a channel. When the client calls the server service, first, a service proxy object is used to serialize the object provided by the caller to the message, and then the message is transmitted through the channel. A channel not only includes one, but also processes messages through multiple channels, including transmission, message encoding, session management, and transaction propagation. However, the underlying channel is always a transmission channel. These channels form a channel stack. Because the object has been serialized, messages transmitted through the channel can be transmitted across processes or machines and transmitted to the server through the transmission channel. The composition of the server is basically similar to that of the client. It still receives messages through the lowest-layer transmission channel in the channel stack, parses the message encoding, and transmits the messages layer by layer. On the channel stack of the server side, it is a distributor (dispatcher, or Scheduler). It first checks the message and then selects the operation to be called by a client. In this process, messages are deserialized.
Describes the entire process of running WCF:
Since WCF transmits messages through a channel, the entire channel also serves as a listener and interceptor. It can execute different operations before or after method execution according to service definitions, such as transactions, session management, and security. In WCF, most of these operations can be applied to the service contract in the form of attribute. This implementation method is similar to adopting AOP (Service-Oriented Programming) the method provides a lot of basic functions for the service and helps simplify the work of service developers.
3. Why should we use WCF? On Windows platforms, especially on the. NET platform, developing service-oriented applications or developing distributed systems, the best choice is WCF. Why? The reason is that WCF covers all the technologies released by Microsoft for distributed development, including remoting, web services, WSE, and MSMQ, and is implemented in a unified programming mode.
WCF supports both interoperable web services and implementations.. Net client and. the communication between the net server and the distributed transaction is supported. In terms of security, it fully complies with the WS-* standard. In addition, it also supports the queue service, it is very convenient to use message queues for asynchronous operations and offline calls. These functions are only partially implemented in the past. See the following table:
Features |
Web Service |
. Net remoting |
Enterprise Services |
WSe |
MSMQ |
WCF |
Web services with interoperability |
Supported |
|
|
|
|
Supported |
. Net to. Net Communication |
|
Supported |
|
|
|
Supported |
Distributed transactions |
|
|
Supported |
|
|
Supported |
WS standards supported |
|
|
|
Supported |
|
Supported |
Message Queue |
|
|
|
|
Supported |
Supported |
It also makes Service-Oriented Programming simpler and more unified. If the old technology is used, the program migration is very difficult because the programming models of various technologies are completely inconsistent. For example, a distributed system originally developed using the. NET remoting technology needs to be redefined to release web services with interoperability due to changes in business requirements. In addition, the call method of the client has also changed. You need to add web references to discover services through UDDI.
Otherwise, use WCF. WCF introduces a channel that encapsulates the communication details of messages, such as encoding, transaction processing, and security. Then, it introduces the concept of binding, encapsulates the composition sequence and processing details of the channel. Finally, a unique endpoint element is introduced, which integrates the "three in one" between addresses, bindings, and contracts to define and publish services in the simplest way.
Each binding corresponds to different transmission protocols, message encoding formats and versions, and security, reliability, and transaction modes. WCF also provides extended binding methods, such as using custombinding or defining classes derived from binding.
A WCF contract includes a service contract, a data contract, and a message contract (especially an error contract for exception handling ). The service contract is the core of the service-oriented application, through which you can define the service. The data contract is the data to be transmitted by the Service. As service calls require communication across processes or machines, service data must be serialized and deserialized. Although. Net itself provides the data serialization function, the WCF data contract is more in line with the service data definition habits. For message contracts, service data can be defined as messages, including XML text format, MTOM (message transmission optimization mechanism) format, and binary format.
Binding, contract, and service address are combined to form an endpoint, as shown in:
Address is the network address of the endpoint. It indicates the destination of the message. Binding describes how to send messages, such as transmission protocols (such as TCP and HTTP) for sending messages, and security (such as SSL and SOAP message security ). Contract describes the content of a message and the message organization and operation methods, such as unidirectional, bidirectional, or request/response methods.
The introduction of endpoints can be said to be a great innovation of WCF. It makes it easier for us to publish and manage services, especially to publish and manage multiple services. Each service must have at least one endpoint, and the client knows the service information through the endpoint, such as the address, message encoding format, transmission protocol, and service content, then, make the correct call. The most special thing is that the same service can define multiple endpoints. Each endpoints can be different addresses and different binding methods to meet different needs of multiple clients. For service publishers, we only need to manage the configuration of endpoints to manage services, which also facilitates service hosting.
5. What content does WCF mainly contain?
Juval's book programming WCF services covers almost all aspects of the WCF technology. In summary, it mainly includes binding, service contract, data contract, message transmission, exception handling, instance mode, concurrent processing, transaction processing, security, and queue service.
Binding is a basic technical element of WCF and serves as the basis for communication processing. Knowledge about binding helps develop WCF applications. Because different business needs may have different requirements on communication methods, protocols, message encoding, and other aspects. You must perform Binding operations when preparing, releasing, and running services. Therefore, we must master the related attributes of the built-in binding of WCF and be familiar with the configuration of binding elements. In addition, in some advanced applications, we also need to know how to add custom binding elements and their order, understand the attributes and methods of the binding base class. And the classes related to the binding configuration.
The service contract is the main processing object of WCF. The service definition and setting are implemented through the service contract. In addition to understanding the basic applications of servicecontract and operationcontract, we also need to compare the differences between WCF Service programming and General. NET programming, such as how the inheritance and Polymorphism of services are implemented. The most important thing is how to divide the boundaries of services according to the idea of SOA and determine the granularity of services. This requires a balance between the ease of use, scalability, performance, and other aspects of the system.
The definition of operations in a service contract is subject to many constraints, the most important of which is data processing, which is also the purpose of introducing a data contract in WCF. Due to the special nature of WCF, we need to know some special data type serialization methods, such as generic type, set, and dataset.
The service operation is also defined to handle exceptions. WCF has a special way to handle exceptions. You can determine whether the exception message needs to be communicated based on the actual situation and whether an exception occurs, whether to stop the service instance or continue the session.
WCF provides great flexibility for message transmission. It provides specialized message classes and related readers, such as xmldictionaryreader and xmldictionarywriter, to read and write messages, this includes operations on the message version, encoding format, message header, and body. At the same time, WCF also provides mappings between messages and methods. When providing system interoperability, it is necessary to understand the message transmission mode of WCF.
Based on different requirements, WCF divides the instance mode into percall, single, and persession modes. You can set the instancecontextmode attribute of service behavior to manage the service instance lifecycle, which simplifies the work of developers. We only need to know the features of these three instance modes. In general, we recommend that the service adopt the percall mode. If you need to maintain the session between the service and the client, you can use the persession mode. The single mode can be used only when there is not much requirement on performance and scalability.
Transaction processing, concurrent processing, and security are mainly set and managed through related service behaviors. These contents are necessary for the development of enterprise-level application systems. As for the queue service, the message queue is used to enable the system to access the service offline and keep the service status synchronized.
6. What application scenarios are applicable to the main use of WCF?
WCF is a set of sdks Based on. NET Framework launched by Microsoft to cope with the SOA strategy. When we develop a system based on the. NET Framework on the Windows platform and require the system to interact with other platforms such as WebLogic, websphere, or JBoss, we need to adopt the WCF technology. This is determined by the interoperability of WCF.
Currently, WCF has not been widely used in enterprise applications, but with the developer's deep understanding of. NET 3. X, it will become more and more popular. Since WCF is integrated. NET Programming Development habits,. NET developers, it greatly reduces the learning curve, In addition, WCF retains the old technology including web service ,. net remoting, MSMQ, and WSE support. It also provides a very good solution for migrating old technologies to WCF. Therefore, if. Net personnel want to apply SOA solutions, then WCF is undoubtedly the best choice. For development purposes alone, using WCF to define services, publish services, and run services, using WF to design, run, and manage workflows, and using WPF as rich clients with rich performance, alternatively, you can use Ajax + sliverlight to develop a client that calls the WCF Service, and use cardspace to set and manage Service Security. This is enough to implement a complete set of SOA solutions. At the same time, WCF can also be combined with BizTalk and Sharepoint, so that the SDK can be mounted on the application server to meet the needs of enterprise applications to a greater extent.
In addition to implementing the SOA solution, WCF is also advantageous for developing distributed systems. When our application system needs to run different services on multiple servers to support a large amount of access load, big data processing, data mining, and other work, you can use WCF to develop different services and host them on different servers. In this case, the entire system can effectively use server resources to reduce system load.