Getting Started with WCF (ix)--WCF instance Management

Source: Internet
Author: User

This set of technologies used by Windows Communication Foundation (WCF) to combine a set of message (client-side request) service instances is known as instance management. One is fully activated by three types of instances to support WCF, as described below.

1. Each invocation of the service

Each invocation of the service is the default instance activation mode for Windows Communication Foundation. When a WCF service is configured for each invocation of a service, a CLR object is a time span that a client invokes or is requested to create. The CLR represents the common language runtime and the WCF service instance.

In each invocation service, each client request implementation is dedicated to consuming the same memory and the new service instance is less active than the other types of instances. You must have the InstanceContextMode property to indicate that the WCF service is set to Instancecontextmode.percall for each invocation of the service. The InstanceContextMode property belongs to the ServiceBehavior property.

Therefore, each invocation of the service can be configured to

[servicecontract]< Span class= "PLN" >interface imycontract< Span class= "pun" >{...} [servicebehavior  ( Span class= "Typ" >instancecontextmode =  Instancecontextmode. Percallclass  Myservice : imycontract {...}             

The service is represented here as imycontract.

Each invocation of the service instance activation process can describe such as.

Implementing each invocation Service
[DataContract]classParam {...} [ServiceContract]Interfaceimycontract {[OperationContract]voidMyMethod (Param objectidentifier); }  classMypercallservice:imycontract, IDisposable { Public voidMyMethod (Param objectidentifier) {GetState (objectidentifier);        DoWork ();     SaveState (Objectidentifier); }     voidGetState (Param objectidentifier) {...} voidDoWork () {...} voidSaveState (Param objectidentifier) {...}  Public voidDispose () {...} }
View Code

Here, the parameter is the parameter used to create the simulation type for the above embodiment.

2. Each invocation of the service

In this activation WCF mode, private or we can say that this is a confidential session that keeps two entities, that is, clients and specific service instances. Also known as the private session service, this mode provides a new service instance that has always been dedicated to each customer requirement and autonomy for each of the other cases of the session-aware service.

The InstanceContextMode property needs to be set to PerSession to initiate this per-session service. Here, the service instance remains in memory all through the session duration. From the scalability of the activation mode to the configured service is not able to support any additional outstanding customers more or possibly achieve some, because the cost of each dedicated service instance involved.

Therefore, each session service can be configured to

[servicebehavior(instancecontextmode=instancecontextmode.  PerSession)]classMyService:imycontract{...}        

The process for each session service can be described as the following figure.

The following code shows the use contracts and services that are configured for private sessions. The output indicates that the client does have a dedicated service instance.

Service Code

[ServiceContract (Session =true)]  Interfaceimycontract {[OperationContract]voidMyMethod (); } [ServiceBehavior (InstanceContextMode=instancecontextmode.persession)]classMyservice:imycontract, IDisposable {intM_counter =0; MyService () {Console.WriteLine ("Myservice.myservice ()"); }      Public voidMyMethod () {M_counter++; Console.WriteLine ("Counter ="+m_counter); }      Public voidDispose () {Console.WriteLine ("myservice.dispose ()"); }  }
View Code

Client code

Mycontractproxy=newmycontractproxy(); proxy.  MyMethod(); proxy.  MyMethod(); proxy.  Close();               

Output

MyService.  MyService()Counter=1Counter=2myservice.  Dispose()            
3. Single-Instance Service

In this activated WCF mode, all client requests are independent of each other, and their connections to the service endpoint are connected to the same single instance. Only if the main machine so that a single case service is not processed.

This service is just created when the host is created. In this case, the host does not provide any singleton instances, and the service returns NULL. Activation mode is the best time, with less work per method call, no waiting for the operating background to run.

The InstanceContextMode property needs to be set to Instancecontextmode.single start this singleton service.

Therefore, a singleton service can be configured to

[servicebehavior(instancecontextmode=instancecontextmode.  Single)]classMysingleton:... {...}

The process of a singleton service can be described as shown.

Initialization and hosting of a singleton code instance

Service Code

[ServiceContract]Interfaceimycontract{[OperationContract]voidMyMethod ();} [ServiceBehavior (InstanceContextMode=Instancecontextmode.single)]classmysingleton:imycontract{intM_counter =0;  Public intCounter {Get     {        returnM_counter; }     Set{M_counter=value; }  }   Public voidMyMethod () {M_counter++; Trace.WriteLine ("Counter ="+Counter); }} Host code Mysingleton singleton=NewMysingleton (); Singleton. Counter= the; ServiceHost Host=NewServiceHost (singleton); host. Open ();//Do some blocking calls thenhost. Close (); Client code mycontractclient Proxy=Newmycontractclient ();p Roxy. MyMethod ();p Roxy. Close (); Output counter= +
View Code

Original address: http://www.yiibai.com/wcf/wcf_instance_management.html

Getting Started with WCF (ix)--WCF instance Management

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.