Test the concurrency of a WCF instance

Source: Internet
Author: User

Environment. net4.0 use nettcpbinding

 

Summary

Instance ManagementIt can be understood as how the server manages (creates and destroys) Service-class instances.
WhileConcurrencyIt can be understood that the WCF framework distributes requests to the target service instance after receiving the client request. Single indicates that if the service is already processing the request, new requests (for the same service instance) will be queued. If mutiple is used, the request will be executed immediately, the essence is that the request is processed based on the lock synchronization behavior of the same service instance.

It is further explained that WCF processes requests based on the pool thread. When multiple requests are sent using the same proxy of nettcp (corresponding to a TCP link in the physical connection, if the server uses the percall + single combination, the TCP link request will be processed in serial mode (it cannot be said that a fixed thread is allocated for processing, because the same proxy is observed, run 50 concurrent threads on two threads, that is, change the thread once ).

// =========== TestCodeServer ================

View code

 Using  System;  Using  System. Collections. Generic; Using  System. LINQ;  Using  System. text;  Using  System. servicemodel;  Using  System. Threading;  Namespace  Wcfserver {  Class  Program {  Static   Void Main ( String [] ARGs ){  VaR Timer = New Timer (o) =>{ GC. Collect ();}, Null , 0 , 100  );  Using (Servicehost host = New Servicehost ( Typeof  (Calculatorservice) {Host. addserviceendpoint (  Typeof (Icalculator ),New  Nettcpbinding (),  "  Net. TCP: // 192.168.1.31: 8888/calculatorservice  "  ); Host. Opened + = (S, e) => {Console. writeline (  "  Start the service!  "  ) ;}; Host. open (); console. Read () ;}} [servicecontract]  Public   Interface Icalculator {[operationcontract]  Int Add ( Int X, Int  Y);} [servicebehavior (instancecontextmode = Instancecontextmode. Single, concurrencymode = Concurrencymode. Single)]  Public   Class  Calculatorservice: icalculator, idisposable {  Public  Calculatorservice () {console. writeline (addthreadmsg (  " Instantiated!  "  ));}  Public   Int Add ( Int X, Int  Y) {console. writeline (addthreadmsg (  String . Format ( "  Index: {0}, time: {1}, operation method is invoked.  "  , X, datetime. Now); thread. Sleep (  1000 );  Return X + Y ;} ~ Calculatorservice () {console. writeline (addthreadmsg (  "  Finalized!  "  ));}  Public   Void  Dispose () {console. writeline (addthreadmsg (  "  Dispose!  " ));}  Private   String Addthreadmsg ( String  MSG ){  Return MSG + "  , Threadid:  " + Thread. currentthread. managedthreadid ;}}} 

 

// ============= Test code client ==============

View code

  Static   Void Main ( String  [] ARGs ){  Using (Channelfactory <icalculator> channelfac = New Channelfactory <icalculator> ( New  Nettcpbinding (),  "  Net. TCP: // 192.168.1.31: 8888/calculatorservice  "  )){  VaR Proxy = Channelfac. createchannel (); If ( Null ! = Proxy As  Icommunicationobject) {(proxy  As  Icommunicationobject). open ();} random RND = New  Random ();  For ( Int I = 1 ; I < 50 ; I ++){  VaR Index = I; thread t = New Thread () => {  Try  {Thread. Sleep (RND. Next (  100 , 10000  ); Console. writeline (  String . Format ( " {3 }:{ 0 }+ {1 }={ 2}  " , Index, 2 , Proxy. Add (index, 2  ), Index ));  //  (Proxy as icommunicationobject). Close ();  }  Catch  (Exception ex) {console. writeline (  String . Format ( "  Error-{0 }:{ 1} "  , Index, Ex. Message) ;}}); T. Start () ;}console. Read () ;} console. writeline (  "  Complete  "  ); Console. Read ();}} 

// Description

1. when nettcpbinding is used, a client proxy corresponds to a connection (you can create multiple proxies to simulate multiple clients). After calling open, you can run the netstat command to view the established TCP connection.

2. when multiple threads are used to simulate multiple concurrent connections (proxy), the server can receive only 11 connections in parallel by default, which is consistent with the number of concurrent connections allowed by the listening port during socket programming.

3. When percall + mutiple is created, a proxy is called in multiple threads without being opened. The proxy will perform serial Hua processing on these call requests based on the call sequence.

4. When percall + mutiple is set up, multiple proxies are established for concurrent calls. The server processes requests in parallel.

5. during percall + single, when multiple proxies are created for concurrent requests, the server displays parallel processing, while for a single proxy for concurrent requests, the server displays serial processing, when nettcpbingding is changed to basichttpbinding, the server becomes concurrent processing. However, you can see through netstat that after using HTTP, concurrent calls of a single proxy actually establish multiple TCP connections,
It can be seen that requests sent from the same link are processed in serial mode in the percall + single hour.

 

Refer:

Http://www.cnblogs.com/tyb1222/archive/2012/05/23/2515535.html

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.