Analysis of WCF Technology 23: Service Instance How to control the lifecycle [next article]

Source: Internet
Author: User

In [2nd], we delve into the WCF's control over the lifecycle of service instances in the monotone (Percall) mode, and now we're going to look at another Extreme service instance context pattern: Singleton mode. In singleton mode, WCF handles all client service invocation requests by creating a unique service instance. This is an extreme service instance activation method, because of the uniqueness of the service instance, the state of every invocation of all clients can be saved, but the current state is the result of all clients acting on the service instance, and cannot reflect the state after multiple invocations of a specific client. WCF is a typical multithreaded communication framework, which is the most basic capability and requirement for concurrent service invocation requests, but the singleness of service instances means that the same service instance needs to be invoked concurrently under multiple threads.

Example Demo: Demonstrating the singleness of service instances

To give the reader an intuitive view of the singleton instance context pattern, we demonstrate the singleness of the service instance in singleton mode with a simple example. Here is an example of an accumulator used in the previous chapter, the following is the definition of a service contract and a service implementation: at initialization, the result of the operation is zero, and the result is only cumulative by the Add method, and the result of the calculation is returned through the getresult operation. On top of the CalculatorService, the service is set to a single case pattern through System.ServiceModel.ServiceBehaviorAttribute.

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:void ADD (double x);
9: [OperationContract]
10:double GetResult ();
11:}
12:}
1:using System.ServiceModel;
2:using Artech.WcfServices.Contracts;
3:namespace Artech.WcfServices.Services
4: {
5: [ServiceBehavior (InstanceContextMode = instancecontextmode.single)]
6:public class Calculatorservice:icalculator
7: {
8:private double _result;
9:public void Add (Double x)
10: {
11:this._result = x;
12:}
13:public Double GetResult ()
14: {
15:return This._result;
16:}
17:}
18:}

On the client side, two service agents are created through channelfactory<icalculator> to simulate two different clients. In terms of the final output, the results do not reflect the normal accumulation of a specific client (for the client through Calculator2 simulation, only one call to add (3), the result is 6) This is the cumulative result of all the clients, this is the uniqueness of the service instance.

   1:using (channelfactory<icalculator> ChannelFactory = new Channelfactory<icalculator> (" CalculatorService "))
2: {
3: ICalculator Calculator1 = Channelfactory.createchannel ();
4: ICalculator Calculator2 = Channelfactory.createchannel ();
5:
6: Console.WriteLine ("1st Serivce proxy:");
7: Console.WriteLine ("Add (3);");
8: Calculator1. ADD (3);
9: Console.WriteLine ("The result is {0}.\n", Calculator1.) GetResult ());
10:
One: Console.WriteLine ("2nd Serivce proxy:");
Console.WriteLine ("Add (3);");
Calculator2. ADD (3);
Console.WriteLine ("The result is {0}.", Calculator2. GetResult ());
15:}

Output results:

1st Serivce Proxy:
ADD (3);
The result is 3.
2nd Serivce Proxy:
ADD (3);
The result is 6.

Related Article

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.