WCF 4.0 advanced series-Chapter 13th provides better performance for implementing the WCF Service (I)

Source: Internet
Author: User
ArticleDirectory
    • Preface
    • Configure service thresholds
    • WCF and service instance pool
    • Memory Requirements
Good performance: Most applications Program And the key factors of the service, you can carefully design and select the appropriate features to ensure that the WCF Service maintains its throughput, maintains response and scalability. So far, these technologies include transactions (Top, bottom), session statuses, reliable message transmission, and asynchronous operations (top, middle, bottom ). There are also some other aspects that affect performance, such as security (security of Enterprise Internal WCF and security of WCF in the Internet environment ). As we discussed in the previous chapter, implementing message-level security and secure sessions will lead to complicated message exchanges, so as to negotiate the protocols used and exchange identity information; in addition, the message content itself has become relatively large, this is because the extra security information is contained in the message header-this information causes message transmission to take longer and more memory for processing in the network. Encryption and decryption are also resource-consuming tasks. However, all these are necessary for a secure system. Therefore, most people sacrifice performance to ensure data privacy and identity information privacy. (If decryption is fast and easy, it makes no sense to execute encryption. The more resources are consumed during message encryption, the more messages can be protected.) an important aspect of maintaining performance is to ensure that the service does not exhaust the resources available on the host computer, this causes the system to slow down and even stop responding. WCF provides service thresholds to help control resource utilization. This feature can be used to maximize the scalability of services. Server Load balancer is another technology that you can use to distribute requests to multiple servers and maintain the same output. Chapter 14th "discover services and route messages" describes how to create a special WCF Service to achieve a simple load balancing. You can also create a Server Load balancer Architecture Based on Windows Network Server Load balancer and Windows Server appfabric (Windows cloud platform middleware), although the detailed information of this technology is beyond the scope of this book. When data is transmitted, the use of appropriate encoding mechanisms has a significant impact on performance improvement. You have learned that WCF supports message encoding using text and binary. In comparison, binary is often more influential and occupies less network traffic, but the binary encoding format is platform-related, therefore, services and client programs running on non-Windows platforms cannot be used easily. However, WCF also supports message transmission optimization (MTOM), which provides a standard, interoperable, and effective format for transmitting large binary data. If you know the size of data transmitted by the Service, MTOM is very useful at this time. Some services send data blocks of unknown size. Therefore, stream transmission is recommended for this type of data. WCF also supports output streams from the service. In this chapter, you will learn how to use service thresholds to maintain service scalability and how to use MTOM to encode data to reduce the system overhead of big binary data objects, and how to start the stream to maximize the use of network bandwidth. Use the service threshold to control resource usage. You can use the service threshold to prevent excessive consumption of WCF Service resources. You may remember that in chapter 11th "writing code control configuration and communication", when a message is received by the service host, the message is sent to the top of the channel stack, it then sends the message to a channeldispatcher object, which passes the message to the corresponding endpointdispatcher object, and then calls the corresponding method in the service instance. However, before sending a request to the endpointdispatcher object, the channeldispatcher object can check the current load of the service. If the request causes the service to exceed the permitted load, the request will be delayed. In this case, the request will be blocked and stored in the internal queue until the service load is released. The channeldispacher object has an attribute named servicethrottle. You can use this attribute to help the channeldispatcher object decide to block and add requests to the waiting queue or send requests directly to the service instance for execution. The servicethrottle attribute is an instance variable of the servicethrottle class. The instance variable itself exposes three attributes externally:
    • Maxconcurrentinstances. This attribute sets the maximum number of concurrent service instances.
    • This attribute sets the maximum number of concurrent messages that the service can process. If a client program produces a large number of concurrent calls, service resources will be quickly consumed regardless of the one-way call operation or the use of multiple threads on the client. In this case, you may want to restrict each client to access a single thread instance of the service. This can be done by setting the concurrencymode. Single attribute to true. In this way, the client program continues to be executed asynchronously, and the service can respond to other users. However, requests submitted by the client will be processed by the Service in various ways.
    • Maxconcurrentsessions, which sets the maximum number of concurrent sessions. The client is responsible for establishing and terminating sessions and calling service operations in sessions. Creating a client that runs the session for a long time may cause other clients to be blocked. Therefore, try to maintain a short session and avoid waiting for the user to enter such a task.
Configure the service threshold. By default, the servicethrottle attribute of the channeldispatcher object is null, and the maximum concurrent instance, method call, and default value of Service session are used during WCF running. To control scalability, you should set a servicethrottle object during the WCF runtime and set these attributes explicitly to suit your environment, in addition, the number of concurrent clients and the number of tasks that the client program may execute should be considered. You can use Code Create a servicethrottle object, set the attributes of the object, and add the object to the behavior set of the servicehost object to complete the task. Note that you must complete the preceding tasks before opening the servicehost object. The following example shows how to execute this task: however, we should warn you that the attribute of the servicethrottle object may have a severe impact on the service response time and service; therefore, you should monitor the performance of the WCF Service in real time. If the host service's computer performance declines, immediately change the previous settings. In addition, if these attribute values are set too low, a large number of clients may be blocked or even time-out or other errors may occur in the client program, there may also be other errors in the client stack; therefore, you need to set the code to capture exceptions and handle these exceptions. Because you may need to easily change the value of the servicethrottle object, A complicated way to create a servicethrottle object and set its attributes is to add a service behavior that contains the <servicethrottle> element to the service configuration file. We will use this method in the following exercises. In addition, during the exercise, you also need to modify the service host Program to display the current service threshold information. Exercise: Apply the service threshold value 1 in the shoppingcartservice. use Visual Studio to open * \ WCF \ step. by. shoppingcart in the step \ solutions \ chapter13 \ servicethrottle directory. SLN solution. This solution contains a simple non-transactional version of shoppingcartservice, which does not update databases. The Service also contains an extended client program that establishes multiple concurrent sessions with the service. 2. Open the ishoppingcartservice. CS file under the shoppingcartservice project. Note that this service uses the servicecontract feature class to specify sessionmode as sessionmode. Required for the ishoppingcartservice interface. Open the shoppingcartservice. CS file. You can see that the service instance mode of the service implementation class is persession. 3. Check the additemtocart method of the shoppingcartservice. CS file. This method first calls the writeline statement to display the name of the method on the screen. In this method, a corresponding writeline statement is added for every possible termination method. You will use these statements to track the running process of each service instance. You should also note that this method calls system. Threading. thread. Sleep (10000) after the first writeline statement to stop the current thread for 10 seconds to simulate database update operations. The purpose of this operation also facilitates the Observation of the Effect of service threshold parameters. In addition, the public method removeitemfromcart, getshoppingcart, and checkout are all implemented in accordance with the preceding method. 4. Open the programm. CS file under the shoppingcartclient project, and find the doclientwork method. This method creates a new proxy object instance, and then calls various operations of the shoppingcartservice through the proxy object instance. This method contains multiple writeline statements, which are used to display the progress of Method Processing in the console window. Wirteline output indicates the number of clients currently connected to the service. In addition, the client uses standard TCP binding to connect to the service. 5. Check the main method of programm. CS. This method uses the parallel. For constructor. This method asynchronously calls the doclientwork Method 10 times. Each call creates a parallel task. It simulates 10 different clients with the same identity to connect to the service at the same time. 6. Open the shoppingcarthost project's programm. CS file, which hosts the shoppingcart service. Add the following reference to the file header. 7. add the following code to the main method. The above code gets the reference of the channeldispatcher object of the service (in this example, the service only has one binding, so when the host program starts the service to run, only a single channeldispatcher object is created during the WCF runtime ). Then, the code checks the servicethrottle attribute of the channeldispatcher object. If it is null, it indicates that the administrator or developer has not specified any custom service threshold value, so the default service threshold value is used. If the servicethrottle attribute is not null, the Service uses the Custom Service threshold behavior. The console window displays the service threshold information set by the administrator or developer. 8. Run the solution in non-adaptive mode. In the service console window, you will find that the service currently uses the default service threshold behavior. Press enter in the console window of the client. The console window displays the result as shown in. In the console window of the service host, you should see "additemtocart operation started" after each client sends an additemtocart request: this means that the service processes requests from multiple clients at the same time. After each method is completed, the service host Console window displays "additemtocart operation completed", and the client will call other operations. At this time, the console output may become a bit confusing, but the most important thing is that the service threshold has not been broken through, because the service does not reject any of the 10 clients (the maximum number of concurrent calls is 10 by default ). Displays the interface after all operations are completed. When the client Console window appears in the message "Tests complete: Press enter to finish", Press enter to close the console window of the client program, and then press enter in the service host Program to stop the service. The above exercise uses the default value of the service threshold. Next, you will customize the service threshold. 9. In Visual Studio, use the WCF Service configuration editor to open the app. config file of the shoppingcarthost Project 10. In the control panel, expand the Advanced folder and select service behavior. In the right pane, click Create service behavior configuration. 11. On the right panel, change the new behavior name to throttlebeahavior. 12. In the lower right pane, click Add to add servicethrottlig to the newly created Service Behavior throttlebahavior13. on the configuration panel, click servicethrottling. The default machine values of the three properties of this element are displayed on the right panel: 14. change maxconcurrentcils to 3.15. on the configuration panel, click shoppingcartservice. shoppingcartserviceimple service, and then in the right panel, set the behaviorconfiguration attribute to throttlebehavior16. Save the configuration file; 17. in Visual Studio, run the solution in non-adaptive mode. In the service technical Console window, you will see the current service uses the threshold action you specified. You may be surprised by the value displayed in the service host Console window. In the service configuration editor, the value generated for the attribute of the servicethrottling element is not the actual value used by the Service Host Program unless you modify them. In WCF 4.0, the default value is determined based on the available resources of the host computer. For example, if a computer uses a single-core CPU, the maximum number of concurrent instances is 116, the maximum number of concurrent calls is 16, and the maximum number of concurrent sessions is 100. on a dual-core computer, you will find that these default values change to twice the single-core default value-but the servicethrottle default value will still be the value shown in step 13th. In addition, there is a relationship between the values of the three servicetrhottling attributes, that is, maxconcurrentinstances = maxconcurrentcals + maxconcurrentsessions. this means that if you only change the value of the maxconcurrentclass attribute without modifying the value of the other two attributes, the value is automatically generated for the other two attributes during the WCF runtime. Please note that my current computer is a quad-core CPU, so the maximum number of concurrent sessions is 100 × 4 = 400; so the maximum number of concurrent instances is 400 + 3 = 403. of course, you can also modify the values of the maxconcurrentcils and maxconcurrentsession attributes. Therefore, the value of these two attributes will not be calculated during the WCF runtime, but will be directly used. 18. Press enter on the client console. In the client Console window, all 10 clients output "clinet N: 1st additemtocart", but the service host console is different from the one shown previously, only three "additemtocart operation started" messages are displayed. This is because the service currently only supports three concurrent operation calls. Channeldispatcher puts subsequent requests into the waiting queue. When each request is completed, the "additemtocart operation completed" message is displayed, and then channeldispatcher releases the next request from the waiting queue. You will see the message "additemtocart operation started ". After that, channeldispatcher releases the next request. The "completed" and "started" messages are displayed alternately until all clients have finished their work. When the client program ends, press enter to close the console window of the client program, and then press enter in the console window of the service host to stop the service. 19. Return to the WCF Service configuration editor and click servicethrottling service behavior element in the left-side Navigation Pane. In the right pane, modify the maxconcurrentcals attribute to 16 and set maxconcurrentsessions to 3. Save the configuration file and exit the WCF Service configuration editor. 20. Return to Visual Studio and run the solution in the adjusted mode. In the service host Console window, you can see that the maximum number of concurrent instances currently used by the Service is 19. please note why this is 19; because if you manually modify the value of maxconcurrentcils to 16, even if the default value of this attribute is 16. but behind your modifications, the following results will be generated in the configuration file: (that is, the system thinks that you have manually set the maximum number of concurrent calls to 16)
According to maxconcurrentinstances = maxconcurrentcils + maxconcurrentsessions; therefore, 19 is displayed in the console window. If you delete maxconcurrentcils = "16"; then run the solution, you will find that maxconcurrentcils is no longer 19; if you are a dual-core computer, now the value of maxconcurrentcils should be 16*2 + 3 = 35. If you use a 4-core computer, the value of maxconcurrentcils is 16*4 + 3 = 67. In the console window of the client, press Enter. Similarly, in the client window, all 10 clients output the message "client N: 1st additemtocart". The service console window displays the start and end of three additemtocart operations. However, after these operations are completed, if you view the messages in the console window of the client, you will find that only the first batch of clients are processed by the service first) you can call additemtocart for the second time. Other clients are managed by channeldispacther and in the pending state because the service has reached the maximum number of sessions allowed. The first batch of clients (that is, the first three clients) to complete the entire set of work, the third call additemtocart, then execute the getshoppingcart operation, and finally execute the checkout operation. The next client can continue only when the checkout is completed and the client closes its own session. You should see in the client that the message always appears in three cycles. Subsequent sessions will display the following exception messages in the console window:
This is because they have been in the waiting status after submitting additemtocart; they have timed out before they are processed by the Service. When the test ends, press enter to close the console window of the client program, and then press enter in the console window of the service host to stop the service. The preceding exercises demonstrate how to use the service threshold to control the maximum number of concurrent calls and the maximum number of concurrent sessions allowed by the Service. Unfortunately, this exercise does not tell you how to set a specific service threshold for your service. You need to test your service in a real load environment and check whether the client program is blocked for a long time. Remember that the service threshold is designed to prevent the service from being "flooded" by a large number of requests-because the service does not have enough resources to process such a large number of requests. You should set the service threshold to make sure that the client starts to process the request after the request is accepted, in addition, the host service computer has enough resources to complete the operation before the client times out. Otherwise, your changes will further impede the performance of the entire service. Note that if the Service supports transactions, the failed client requires the Service to perform more work, because when the timeout occurs, the Service must roll back the previously executed transactions. Create a service instance to process client requests when running the WCF and service instance pool. If the service uses the persession instance mode, the service instance can exist across multiple operations. If the service uses the percall instance mode, a new service instance is generated for each operation. when the operation is completed, the service instance is discarded and destroyed. Creating and destroying a service instance not only consumes a lot of resources, but also takes a long time. In this scenario, the service instance pool is very useful. When a service instance pool is used, a service Instance Object pool is created when the service is started during the WCF runtime. If the service uses the percall instance mode, when the client program accesses the call operation, the WCF runtime obtains a pre-created service instance from the pool, and puts the service instance back into the pool after the operation is completed. If the persession instance mode is used, the working mode is the same as the percall mode in semantics, but the WCF runtime obtains a service instance from the pool at the beginning of the session, put the service instance back into the pool at the end of the session. For security purpose, all data held by the service instance (member variables in the service implementation class) will be cleared when the returned pool. In fact, WCF does not directly provide a service instance pool, but you can extend the service instance pool by defining custom behavior. WCF provides the iinstanceprovider interface through the system. servicemodel. Dispatcher namespace. You can use this interface to define a custom service instance distribution mechanism. This is a very useful technique, but its details are beyond the scope discussed in this book. For more information about the service instance pool, see the service instance pool topic in the Visual Studio help document. The topic can also be viewed on msdn: The http://msdn.microsoft.com/en-gb/library/ms751482.aspx specifies the memory requirement application service threshold behavior allows you to limit the number of sessions and the number of connections to maintain the service throughput. However, a service is an application running on a computer, and the Service performs resource-consuming operations. Therefore, it is necessary to ensure that it has sufficient resources at the beginning of the operation. Memory is a common and scarce resource. For this reason, the WCF runtime allows you to specify the minimum available memory for the service before activation. In the service configuration file, you can use the minfreememorypercentagetoactivateservice feature in the <servicehostingenvironment> element to customize the value. The default value is 5. In this example, we change it to 10. in this case, if the total available memory is less than 10% of the total memory when you try to activate the service during the WCF runtime, the WCF runtime will fail and a serviceactivationexception exception will be thrown. Of course, you can also set the parameter value through the WCF Service configuration editor. In the configuration panel, expand the Advanced folder and click host environment. On the host environment panel, specify the minimum number of memory required. As shown in:

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.