Analysis of WCF Technology 23: Service Instance how life cycle control [medium]

Source: Internet
Author: User
Tags garbage collection

In [1th], we introduced WCF about instance management some basic knowledge points, including InstanceContext, InstanceContextMode, How different instance context patterns have been applied to different services through ServiceBehaviorAttribute. In [1th], a simple comparison is made between the three different instance context patterns used by WCF, and the emphasis of this paper is on the monotone (percall) mode for detailed introduction.

In the monotone (Per-call) instance context mode, WCF always creates a new service instance context to handle each service invocation request received and reclaims the service context and service instance after the service operation has finished executing. In other words, the Monotone service instance context pattern binds the lifecycle of the service instance context to the service invocation itself. Let's first introduce the specific lifecycle of the service instance context in monotonic mode.

Service instance context provision mechanism in monotonic mode

For monotone patterns, the life cycle of a service instance can generally be seen as the life cycle of a service operation execution. The service instance is created before the service operation is executed and is reclaimed after the operation completes. The following list shows the entire service instance activation process for WCF in monotone mode for each service invocation request:

The WCF service end receives a service invocation request from the client;

Attempts to get an instance context of an existing service instance through an instance context provider (Instancecontextprovider) object, and for monotone patterns, the returned instance context is always empty;

If the get instance context is empty, the service instance is created through the instance provider (Intanceprovider), encapsulated into the newly created instance context;

The service instance object is obtained through the InstanceContext Getserviceinstance method, and the operation selector (Operationselector) is used to select the corresponding service operation. Finally, the Operation Actuator (Operationinvoker) object is used to perform the corresponding operating method.

Close the unloaded InstanceContext object after the action method has finished executing. In this procedure, the Instanceprovider object is invoked to release the service instance, and the disposable method is invoked if the service type implements the interface IDisposable;

The service instance becomes a garbage object, waiting for GC to recycle.

Important objects such as Instancecontextprovider, Instanceprovider, and related implementation mechanisms mentioned in the above list will be explained separately in the subsequent sections of this series. To deepen the reader's understanding, here is a simple example to illustrate the entire activation process of a service instance in monotone mode.

Example Demo: The life cycle of service instances in monotone mode

This case still follows a typical 4-tier architecture and computing services scenario, which is defined by the service contract and the specific service implementation. On the CalculatorService type, the instance context mode is set to monotone (Per-call) mode by the ServiceBehaviorAttribute attribute. To demonstrate the creation, release, and recycling of service instances, we define the parameterless constructors, Terminators (Finalizer), and implemented interface IDisposable, and output the corresponding indicative text in all methods to make it easier to observe the order in which they were executed.

1:using System.ServiceModel;
2:namespace Artech.WcfServices.Contracts
3: {
4: [ServiceContract (namespace= "http://www.artech.com/")]
5:public interface ICalculator
6: {
7: [OperationContract]
8:double ADD (double x, double y);
9:}
10:}
1:using System;
2:using System.ServiceModel;
3:using Artech.WcfServices.Contracts;
4:namespace Artech.WcfServices.Services
5: {
6: [ServiceBehavior (InstanceContextMode = Instancecontextmode.percall)]
7:public class Calculatorservice:icalculator, IDisposable
8: {
9:public CalculatorService ()
10: {
11:console.writeline ("Service object is instantiated.");
12:}
~calculatorservice ()
14: {
15:console.writeline ("Service object is finalized.");
16:}
17:
18:public void Dispose ()
19: {
20:console.writeline ("Service object is disposed.");
21:}
22:public double Add (double x, double y)
23: {
24:console.writeline ("Operation method is invoked.");
25:return x + y;
26:}
27:}
28:}

To demonstrate GC's recycling of service instances, the GC enforces a garbage collection every 10 milliseconds at the time of service boarding.

   1:using System;
2:using System.ServiceModel;
3:using System.Threading;
4:using Artech.WcfServices.Services;
5:namespace Artech.WcfServices.Hosting
6: {
7: Public class Program
8: {
9: private static Timer Gcscheduler;
10:
One: static void Main (string[] args)
: {
Gcscheduler = new Timer (
: Delegate
: {
: GC. Collect ();
: }, NULL, 0, 100);
: using (ServiceHost ServiceHost = new ServiceHost (typeof (CalculatorService)))
: {
: Servicehost.open ();
: Console.read ();
: }
: }
: }
25:}

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.