WCF Service programming Basics

Source: Internet
Author: User
Tags msmq
WCF Service programming-BasicsWCF is a standard platform for Microsoft to build a new generation of distributed applications and service-oriented applications. It is based on the original. NET Framework 2.0 extension. Although the WCF technology was used in the project shortly after its release. However, since there is no large-scale application of the WCF Technology in the project, it is not very good at understanding. I already have many articles on WCF on the Internet, and they have helped me a lot in my learning process. However, you still need to learn and summarize the technology. The following figure shows that WCF integrates the advantages of Microsoft's existing technologies. However, WCF is not just as simple as integrating existing technologies. Windows Communication Foundation (WCF) is a Runtime Library and a set of APIs used to create a system for sending messages between services and clients. It uses the same infrastructure and APIs to create applications. In the past, the development process and methods of different technologies were quite different. In WCF, you only need to change some attributes or configurations. WCF terminologyA message is an independent data unit. It may consist of several parts, including the message body and message header. A service is a constructor that exposes one or more endpoints. Each of these endpoints exposes one or more service operations. An endpoint is a structure used to send or receive messages (or execute both operations at the same time. An endpoint includes a location (Address) that defines the destination to which a message can be sent, a communication mechanism specification (binding) that describes how a message should be sent ), the definition of a group of messages that can be sent or received (or executed simultaneously) at this location (service agreement, used to describe which messages can be sent ). The WCF Service is made public to the outside world as a collection of endpoints. An application endpoint is a service agreement published by the application and implemented by the application. An infrastructure endpoint is exposed by the infrastructure to implement the functions required or provided by services unrelated to the service agreement. For example, a service may have an infrastructure endpoint that provides metadata. Address specifies the location where the message is received. The address is specified as a uniform resource identifier (URI. The URI architecture specifies the transmission mechanism used to reach the address, such as HTTP and TCP. The level part of the URI contains a unique position. Its format depends on the transmission mechanism. You can use the endpoint address to create a unique endpoint address for each endpoint in the service, or share an address between endpoints under certain conditions. The following example demonstrates an address that combines the HTTPS protocol with a non-default port: https: // cbcye: 7788/servicemodelsamples/calculatorservice binding (binding) the binding defines how the endpoint communicates with the outside world. It is constructed by a group of elements called binding elements, which are "stacked" together to form a communication infrastructure. At least the transmission protocol (such as HTTP or TCP) and the encoding used (such as text or binary) should be defined for binding ). The binding element can contain the specified details (for example, the message security mechanism used to protect the message or the message mode used by the endpoint. For more information, see (msdn) Configuration Service. Binding element: a binding element represents a specific part of a binding, such as the implementation of transport protocols, encoding, infrastructure-level protocols (such as WS-reliablemessaging), and any other elements of the communication stack. Behaviors is a factor that controls various runtime characteristics of services, endpoints, specific operations, or clients. Actions are grouped by scope: common actions affect all endpoints globally. Service actions only affect service-related aspects. endpoints only affect attributes related to endpoints, action-level behaviors affect specific operations. For example, a service behavior is containment, which specifies how the service responds when too many messages may exceed the Service's processing capability. On the other hand, the endpoint behavior only controls the aspects related to the endpoint, such as the method and location for finding security creden. The System-provided bindings WCF contains the bindings provided by many systems. These bindings are a set of binding elements optimized for specific solutions. For example, wshttpbinding is specially designed to implement interoperability with various ws * standard services. By providing only the options that can be correctly applied to a specific scheme, these predefined bindings can save time. If the predefined binding does not meet your requirements, you can create your own custom binding. Configuration versus coding allows you to write, configure, or combine the two to control the application. The advantage of configuration is that it allows non-developers (such as network administrators) to directly Set client and service parameters after code writing, without re-compiling. You can use the configuration to set values (such as endpoints), and add endpoints, bindings, and actions to implement further control. By writing code, developers can strictly control all components of the service or client, check all settings completed through configuration, and rewrite the code as needed. Service operation is a process defined in the service code to implement certain operations. This operation is made public to the client as a method on the WCF client. This method may return a value with a number of optional parameters or no parameters and no response. For example, a simple "hello" operation can be used as a notification to the client, and a series of operations can be started. Service Contract associates multiple related operations to form a single function unit. Protocols can define service-level settings, such as service namespaces, corresponding callback protocols, and other such settings. In most cases, the Protocol definition method is to create an interface using the selected programming language, and then apply the servicecontractattribute attribute to the interface. By implementing this interface, you can generate actual service code. Operation contract defines parameters and returns the type of the operation. When creating an interface that defines a service agreement, you can apply the operationcontractattribute attribute to each method definition contained in the agreement to represent an operation agreement. You can model an operation to use a single message as a parameter and return a single message, or use a group of types as parameters and return a type. In the latter case, the system determines the format of the message to be exchanged for this operation. A message contract describes the format of a message. For example, it declares whether the message element should be included in the message header or in the message body, and what level of security should be applied to the elements of the message. Fault contract can associate error agreements with service operations to indicate possible errors returned to the caller. One operation can have zero or more errors associated with it. These errors are soap errors that are modeled as exceptions in the programming model. The data types used by the Data contract service must be described in the metadata so that other parties can interact with the service. The description of data types is called data protocols, which can be used in any part of a message (for example, as a parameter or return type ). If the service only uses a simple type, no explicit use of data protocols is required. The host service must be hosted in a process. A "host" is an application that controls the lifetime of a service. Services can be self-hosted or managed by existing host processes. Self-hosted service is a service that runs in a process application created by a developer. Developers control the service life, set service properties, open the Service (this sets the service to the listening mode), and disable the service. A host process is an application designed to host services. These host processes include Internet Information Service (IIS), Windows activation Service (was), and Windows service. In these host schemes, the host controls the service lifetime. For example, you can use IIS to set virtual directories that contain service assembly and configuration files. When a message is received, IIS starts the service and controls its survival. Instancing each service has an instantiation model. There are three instantiation models: "single". In this model, a single CLR object provides services for all clients; and "every call", in this model, A new CLR object is created to process each client call. In this model, a set of CLR objects are created and an object is used for each independent session. The choice of the instantiation model depends on the application requirements and the expected use mode of the service. Client application (client applicant) client application is a program for exchanging messages with one or more endpoints. The client application starts to work by creating a WCF client instance and calling the methods of the WCF client. Note that a single application can act as either a client or a service. A channel is a concrete implementation of binding elements. Binding indicates configuration, and the channel is associated with the configuration. Therefore, each binding element has an associated channel. Channel stack together to form the specific implementation of binding: Channel stack. The WCF client is a client application that exposes service operations as a method.. NET Framework programming language, such as Visual Basic or Visual C #). Any application can host the WCF client, including the application that hosts the service. Therefore, you can create a service for a WCF client that contains other services. By using servicemodel metadata utility tool (svcutil.exe) and directing it to a running service that publishes metadata, You can automatically generate a WCF client. Metadata of the metadata service describes various features of the service. external entities need to understand these features to communicate with the service. Servicemodel metadata utility tool (svcutil.exe) allows you to use metadata to generate a WCF client and a client application that can be used to interact with the service. The metadata published by the service includes the XML schema document (data protocol used to define the service) and the WSDL document (used to describe the service method ). After metadata is enabled, WCF automatically generates service metadata by checking the service and its endpoints. To publish service metadata, you must enable the metadata behavior explicitly. Security in WCF includes confidentiality (message encryption to prevent eavesdropping) and integrity (methods used to detect tampering with messages) identity Authentication (methods used to verify servers and clients) and authorization (Resource Access Control ). These functions can be provided by leveraging existing security mechanisms (such as TLS over HTTP, also known as https) or by implementing one or more of the various WS-* security specifications. The transmission security model ensures security in one of the following modes: transmission mode, message security mode, and transmission mode using message creden. Transport security mode specifies that the transport layer mechanism (such as https) provides confidentiality, integrity, and authentication. When using a transmission protocol like https, this mode has excellent performance and is easy to understand because it is very popular on the Internet. Its disadvantage is that such security is applied to every hop in the communication path, which makes the communication vulnerable to man-in-the-middle attacks. The message security model ensures security by implementing one or more security specifications, for example, "Web Services Security: SOAP message security" (Web Service Security: SOAP message Security) specifications (available in http://go.microsoft.com/fwlink? Linkid = 94684 ). Each message includes the necessary security mechanisms to ensure security during message transmission, and enable the receiver to detect tampering and decrypt the message. In this sense, security information is encapsulated in each message, providing end-to-end security across multiple hops. As security information is a part of a message, you can also include multiple creden。 in the message (these creden。 are called "declarations "). This method also has the advantage that messages can be securely transmitted through any transmission protocol (including multiple transmission protocols between the start point and the target. The disadvantage of this method is that the encryption mechanism used is complicated and the performance is affected. Transport with message credential security model this mode uses the transport layer to provide message confidentiality, authentication, and integrity, each message can contain multiple creden。 required by the message receiver ). WS-* a group of increasing Web Service (WS) specifications (such as WS-Security and WS-reliablemessaging) implemented in WCF. The WCF architecture describes the main layers of the Windows Communication Foundation (WCF) architecture. (English-Chinese) agreements and Descriptions define all aspects of the message system. The description of a Data contract constitutes each parameter of each message that a service can create or use. Message parameters are defined by the XML Schema Definition Language (XSD) document, which allows any system that understands XML to process this document. The message protocol uses the SOAP protocol to define specific message parts. When interoperability requires more precise control over certain parts of the message, the message protocol can implement this control. The service agreement specifies the actual method signature of the service and uses one of the supported programming languages (such as Visual Basic or Visual C #) as the interface for distribution. Rules and binding rules are the conditions required for communication with a service. For example, the binding must (at least) Specify the transmission (such as HTTP or TCP) and encoding used. Policies include security requirements and other conditions, which must be met before they can communicate with the service. When a service is running, the service runtime layer contains only the behaviors that occur during the actual running of the service, that is, the runtime behavior of the service. Curb the number of messages processed by the control. If the demand for services increases to the preset limit, the number of messages changes. The error Action specifies the actions that the service should take when an internal error occurs, such as controlling the information transmitted to the client (too much information will provide malicious users with the opportunity to attack ). Metadata behavior controls whether and how metadata is provided externally. Instance behavior specifies the number of running service instances (for example, Singleton specifies that only a single instance can be used to process all messages ). You can roll back the operations that have been processed when a transaction fails. The scheduling behavior is used to control the way in which messages are processed by the WCF infrastructure. You can use the scalability function to customize runtime processes. For example, the Message check function is used to check each part of a message. You can use the parameter filter function to perform preset operations based on the filter acting on the message header. The message passing layer consists of channels. A channel is a component that processes messages in a certain way (for example, by performing authentication on messages. A group of channels are also called channel stacks ". The channel operates on messages and message headers. This is different from the service runtime layer. The service runtime layer mainly involves processing the message body content. There are two types of channels: Transmission Channel and protocol channel. The transmission channel reads and writes messages from the Network (or some other external communication points. Some transmission channels use encoders to convert messages (expressed as XML infoset) into the representation of byte streams used by the network, or convert byte streams to messages. Transmission Channel examples include HTTP, named pipe, TCP, and MSMQ. Examples of encoding include XML and optimized binary files. Protocol channels often Implement Message Processing protocols by reading or writing other headers of messages. Examples of such protocols include WS-Security and WS-reliability. The message passing layer describes the possible data formats and exchange modes. WS-Security is the implementation of WS-Security specifications that enable security at the message layer. The WS-reliable messaging channel ensures message transmission. The encoder provides a large number of encodings that can be used to meet the needs of messages. HTTP specifies that Hypertext Transfer Protocol should be used to transmit messages. Similarly, the TCP channel specifies the TCP protocol. The transaction flow channel controls the message mode that has been processed by the transaction. You can use the Named Pipe Channel to perform inter-process communication. The MSMQ channel can be used for interoperability with MSMQ applications. The final form of hosting and activating a service is a program. Like other programs, services must run in executable files. This is called "Self-bearer" service. Some services (such as IIS or Windows activation Service (was) are "hosted" and run in executable files managed by external agents. Was allows you to automatically activate a was application when deploying it on a computer running was. You can also manually run the service by using the executable file (.exe file. The service can also run automatically as a Windows service. The COM + component can also be used as the bearer of the WCF Service. The article about the WCF message processing process comes from Zhongrui

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.