WCF learning 2

Source: Internet
Author: User
Tags format definition msmq

Iv. WCF Architecture

 

1. Key elements and concepts of the WCF Architecture

Contracts and description

Contracts defines various aspects of the message system, including data contract, message contract, and service contract. Data contract is the format definition for data exchange between the service and client in WCF. It is defined by the XML Schema Definition Language (XSD), so that both the service and client can understand the data format definition. A Message Protocol can define a specific part of a message. By default, a message in WCF is in a fixed format, but in some cases, message contract can also be used to customize the Message format.

 

Service runtime

When a service is running, it refers to some behavior control during the actual running of the service. Errorbehavior is an operation that occurs when a service error occurs. Throttingbehavior can limit the number of instances and sessions to be created, through which you can control the performance of the WCF Service. Metabehavior controls whether and how metadata is provided externally. Transactionbehavior can define transactions so that rollback can be performed when an exception occurs. Dispatchbehavior can control how WCF processes messages and customize runtime processes through the scalability.

 

Messaging

In WCF, messages are transmitted in channels. A channel is a component that processes messages in some way. A group of channels can be combined into a channel stack. There are two main channels: protocol channel and transmission channel. The protocol channel describes the data format and exchange mode. WS-Security is the implementation of the WS-Security Specification that enables security at the message layer. Through the WS-reliable messaging channel, messages can be transmitted. 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, and the transaction channel controls the message mode that has been processed by the transaction. The Named Pipe Channel can communicate with processes, and the MSMQ channel can be used with MSMQ applications.ProgramFor interoperability.

 

Activation and hosting

The final form of the WCF Service is still a program. It can be "self-hosted" or hosted in other applications, such as IIS, Windows service, and COM +.

 

V. message exchange mode

1. message exchange mode defined by WCF

One-way CILS

This switching mode has the following features:

1) No return value. The return type can only be void.

2) parameters of the ref or out type cannot be included.

3) The server does not reply to the request only when the client initiates the request.

 

The isoneway = true of the operationcontract device can be used to set the methods that meet the requirements to this message exchange mode.

[ Operationcontract (Isoneway = True )]
Void Test ( Int Intval );

Note: If the return type of the test method is not void or a parameter with ref or out type, an invalidoperationexception is thrown.

 

Request/Reply

It is the default message exchange mode, similar to the request/response mode in HTTP.

This exchange mode is the most widely used one with the following features:

1) after calling the service method, you need to wait for the Service Message to return, even if the method returns the void type.

2) compared with duplex, this mode emphasizes the passive reception of the client, that is, after the client receives the response, the message exchange ends.

3) In this mode, the server is always the server, and the client is the client, with clear responsibilities.

 

This is the default message exchange mode. With operationcontract, you can set this mode.

[ Operationcontract ]
Void Test ( Int Intval );

Note: although the test method returns void, the server also generates a relpy response and sends it to the client.

 

Duplex

It is similar to the request/reply mode, but the processing process is more complex than the request/reply mode, because it can perform a response operation through the callback in the request client after processing the request.

1) The server and client roles are exchanged during message exchange.

2) After the server completes processing the request, it does not return a reply but a callback request to the client.

 

The duplex mode has special requirements on binding. It must support duplex MEP (message exchange pattern), such as wsdualhttpbinding and nettcpbinding.

 

V. event broadcast

1. Implement Duplex-based event broadcast

1) by calling the accept () of the server, the client can connect to the server and maintain the session.

2) When the client starts, it can remotely call getjobs () to obtain all the tasks on the current server, and present these tasks in the client form using list control.

3) The client can call addjob () to add a task to the server. After the server completes the add operation, the event is triggered and the event is broadcast to all clients.

4) when the client server sends a new task event broadcast, the client adds the new task to the List Control for display.

5) The client can command the server to execute a specific task. After the task is executed and completed, the server will broadcast the execution of the task like all clients, in addition, task execution and broadcast asynchronous execution of events.

6) After receiving the broadcast, the client can update the task information.

 

2. Several Problems in Duplex event broadcast

1) duplex mode requirements for service behavior concurrencymode

Concurrencymode controls service concurrency. By default, the value of concurrencymode is single, which sets the service to run in a single thread. When a request is not completed, the Service does not accept the next request. During callback, if the callback method is not set to one-way switch mode, the server will wait for the client to respond to the callback. This is not a good thing, the server does not guarantee that the client can normally execute the callback and return data. In more cases, we expect that the callback can be returned immediately after it is sent. There are two methods: A. Set the callback method to one-way switching mode. B. multithreading is used. When the one-way mode is set for the callback method, setting concurrencymode to single enables duplex to communicate with each other. The second method is also very simple. You only need to set concurrencymode to mutiple. In this case, even if the callback method is not one-way mode, duplex can be completed. It is worth noting that concurrencymode has a neutral attribute: concurrencymode. reentrant can accept multiple requests in a single thread at the same time, but it has advantages and disadvantages. It cannot guarantee the integrity of the Request transaction and should be cautious when using it.

 

2) instancecontextmode = instancecontextmode. persession can be broadcast.

If you set instancecontextmode to persession, we know that the server object is for each session, that is, each session will generate an object instance, so that if you want to implement broadcast, we must record the session information contained in the current service with a list object. During broadcast, we will traverse the Session list for each callback.

 

3) session expiration

After the session expires, the communication will be terminated. The solution is to set isterminating of operationcontract to maintain the session. When isterminating of an operationcontract is set to false, this operation will not cause session interruption.

 

Vi. instance mode and object Lifecycle

1. instance mode of WCF

There are three instance modes for WCF: persession, percall, and single

 

Percall

1) create a proxy on the client ).

2) The client calls a contract operation on the proxy object and the proxy object passes it to the service host Program.

3) The Host application creates a new service contract object and performs request operations.

4) after the request operation is completed, if a response is requested, the service contract will give the proxy object a response and destroy itself (if idisposable is implemented, dispose () is called ()).

 

Persession

1) create a proxy on the client ).

2) The client calls a contract operation of the proxy object for the first time, and the proxy object passes the call request to the service host.

3) The host program creates a new service object and performs request operations. If necessary, return to the client for a response.

4) when the client sends a call request again, the host will first determine whether a established session exists. If yes, it does not need to create a new service object and directly use the old object.

5) The session will expire when the specified time meets the specified requirements or for some special reasons. In this case, the service object is destroyed.

 

Single

1) The server starts and creates service objects at the same time.

2) The client calls the contract operation through a proxy.

3) The created service object accepts the request, performs the operation, and responds to the request as necessary.

4) The created service object will be retained all the time.

5) the service is closed and the created object is destroyed.

 

2. Application scenarios of various instance Modes

Percall

When a request operation comes, create a service object and apply for necessary resources. After the operation is completed, destroy the object and release the resource immediately, and leave it to the next request. This may greatly improve the server throughput, and the default instance Creation Mode in WCF is like this.

Persession

Persession can be maintained on the server and client. When a service object is created, it will not be destroyed immediately, but will be consumed by the client again. It can maintain the connection and maintenance status, which is particularly important when a callback is required. In another case, server operations do not require a large amount of resources or the resources used are not valuable, but are in a different network from the client, it takes a long time to establish a connection between them, which also applies to some instance modes.

 

Single

The service object is created only once in single mode, similar to the singleton design mode.

 

3. Considerations for using different instance Modes

Percall

For percall mode, if the data in the service object is not fixed and is not a static variable, it will be reinitialized every time it is operated.

 

Persession

For the persession mode, it should be clear that some bindings cannot be used in this mode. In addition, the persession mode does not mean that que Yun will automatically maintain them, when operations with isterminating = true are completed, resources and objects will be released and destroyed. Even if they are not true, if the client does not contact the server for a long time, the maximum endurance of the server will be reached, the server session also times out.

 

Single

In single mode, non-static global variables in service objects can be maintained. But pay special attention to thread security issues.

 

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.