Step by step (3) Behavior Model

Source: Internet
Author: User

This document uses custom behavior definitions. For more information, see the first two articles in this series.

 

Behavior. To put it bluntly, it is an internal additional operation. In terms of effect, it is a bit like httpmodule and httphandle in ASP. NET (The implementation principle is certainly different ).

In the preceding example, an additional operation is added for the service, that is, the parameter + 1 sent from the client before the contract method is called. We can also add similar behavior on the client. I will give this example in the next chapter.

 

Behavior has different types in service and client:

In service, these types are stored in the system. servicemodel. Dispatcher namespace.

In the client, these types are stored in the system. servicemodel. Channels namespace.

 

In addition, behavior can be defined at three levels,

1. Contract level. As the name suggests, the behavior is attached to the interface (contract), as shown below, that is, the attribute of mycontractbehavior:

[MyContractBehavior][ServiceContract]public interface ISimple{    [OperationContract(IsOneWay=true)]    void AcceptRequest(string someRequest);}

2. At the endpoint level, add the behavior to the endpoint. Do you still remember the myparameterinspector in the previous article? The following is code-form addition (the other is added in the configuration file, which will be discussed later ):

host.Description.Endpoints[0].Contract.Operations[0].Behaviors.Add(new MyParameterInspector());

3. At the operation level, the behavior is added to the operation (Contract method. This level is the lowest. As shown below, both identityvalidate and identitycache are custom behaviors.

[ServiceContract] public interface IIdentityService{    [IdentityValidate]    [IdentityCache]     [OperationContract]    int GetIDByName(string name);}

 

According to our needs, behavior is defined at different levels and their scopes are different. For example, the operation level only affects one method, while the endpoint level affects all operations at this endpoint, the contract level affects all operations that call the contract.

 

There are 5 types of Behavior:

Parameterinspection

Messageformatting

Operationinvoker

Messageinspection

Operationselector

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.