WCF getting started (10)-service object model

Source: Internet
Author: User

When a WCF request-response operation occurs, the following steps are taken:

  1. The WCF Client wants the WCF Server to send a service request.
  2. Create a WCF Service object
  3. The WCF Server calls the WCF Service object interface and returns the result to the WCF client.

The creation of service objects is involved in the operation process. However, since the WCF Service object is managed by the WCF framework, it usually does not focus on when it is created or when it is recycled. For services that do not need to access the status of member variables, this does not affect our function implementation.

However, in many cases, we also need to provide status-related services. In this case, we need to save the status through the member variables of the service object. In this case, we need to pay attention to the creation policy of the WCF Service object. The following uses a counter as an example to describe the impact of different policies on service results.

[ServiceContract]
Public
Interface
IService1
{
[OperationContract]
Int Increment ();
}

[ServiceBehavior]
Public
Class
Service1: IService1
{
Private
Int intCounter;

Public
Int Increment ()
{
IntCounter ++;
Return intCounter;
}
}

Generally, we have the following three policies to create a WCF object:

  • PerCall creates a new WCF Service object every time a WCF Service is called,
  • PerSession only creates one WCF Service object during each WCF session. If no session is set, the effect is the same as that of PerCall (default policy)
  • All Single WCF sessions share one WCF Service object

These three policies can be explicitly set through InstanceContextMode in ServiceBehavior.

[ServiceBehavior (InstanceContextMode = InstanceContextMode. PerCall)]
Public
Class
Service1: IService1

Note:If the session is not enabled, the effect of the PerSession is the same as that of the PerCall. The default Protocol basicHttpBinding does not support the session. To correctly demonstrate this example, bind the Protocol to wsHttpBinding.

 

PerCall mode:

In this way, a new WCF Service object is created for each WCF Service call. After the request is called, the object enters the recoverable state.

In this way, no matter how the client calls it, because every time a new object is created, the count will always be 1

[ServiceBehavior (InstanceContextMode = InstanceContextMode. PerCall)]
Public
Class
Service1: IService1
{
}

 

PerSession Mode

In this mode, each session corresponds to a service object, and the service object is recycled after the session ends. That is to say:

  1. All calls from the same client are sent to the same service object.
  2. Calls from different clients are sent to different service objects.

In this way, each client is counted independently.

[ServiceBehavior (InstanceContextMode = InstanceContextMode. PerSession)]
Public
Class
Service1: IService1
{
}

 

Single Mode

In this mode, all sessions correspond to the same service object.

In this way, the count of all clients is accumulated.

[ServiceBehavior (InstanceContextMode = InstanceContextMode. Single)]
Public
Class
Service1: IService1
{
}

 

Service object lifecycle and Dispose

Although we use InstanceContextMode to control the lifecycle of a service object, we cannot determine when to release the service object. In general, this does not need to be concerned, but if the service object saves resources in the member variable, we need to ensure that the resources can be released in time.

According to the. net design principle, if resources are saved in the member, you need to implement the IDisposable interface, and then release the resources in the Dispose method. In fact, this rule is also followed in WCF: For service objects that implement the IDisposable interface, after the end of the lifecycle of the object, the Dispose method is called to ensure timely resource release. That is to say, as long as our implementation conforms to the basic design principles, the WCF framework is automatically released in a timely manner.

To use this rule, we can use the Dispose method to perceive the deprecation of members in session mode.

 

Comparison of the three methods

In terms of performance, the Single mode creates only one object globally, with the minimum overhead. PerCall creates an object each time it is called. It seems that the overhead is the largest, but its scalability is the best. It is very easy to achieve load balancing and the most timely release of resources.

In terms of functionality, the PerSession mode is the most similar to the object-oriented mode, making it easier to implement various complex logics.

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.