{"id":837,"date":"2014-09-10T23:23:00","date_gmt":"2014-09-10T13:23:00","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=837"},"modified":"2016-08-06T00:25:19","modified_gmt":"2016-08-05T14:25:19","slug":"how-to-throttle-a-section-in-wcf","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2014\/09\/10\/how-to-throttle-a-section-in-wcf\/","title":{"rendered":"How to throttle a section in WCF"},"content":{"rendered":"<p>If a WCF service internally connected to a legacy system which does not support multi threading, the WCF needs to be throttled.<\/p>\n<p>The easiest way to achieve this is to make the WCF works sequentially by setting this up not to work concurrently.<\/p>\n<p>By default, a WCF configured to process in multiple instances and concurrency mode. Before setting up the throttle in WCF, it is important to understand how concurrency works in WCF.<\/p>\n<p>Two columns below are great to understand this.<\/p>\n<p><a href=\"http:\/\/www.codeproject.com\/Articles\/86007\/ways-to-do-WCF-instance-management-Per-call-Per\" target=\"_blank\">Three ways to do WCF instance management<\/a><br \/>\n<a href=\"http:\/\/www.codeproject.com\/Articles\/89858\/WCF-Concurrency-Single-Multiple-and-Reentrant-and\" target=\"_blank\">WCF Concurrency (Single, Multiple, and Reentrant) and Throttling<\/a><\/p>\n<p>In a nutshell,\u00a0it is easy to manage concurrency in WCF. It can be configured in <span id=\"dbda8d2e-6e81-4e07-83d8-4aa1d02dcd01\" class=\"GINGER_SOFTWARE_mark\"><span id=\"060f4be7-7fd9-4ac5-a467-3722280a54d0\" class=\"GINGER_SOFTWARE_mark\"><span id=\"2e709d0a-ec9d-4410-8a96-cbc9850e9b9b\" class=\"GINGER_SOFTWARE_mark\"><span id=\"2e9f05d0-996c-424d-a5d8-4adc5341cee1\" class=\"GINGER_SOFTWARE_mark\"><span id=\"e4942192-0aa0-4a17-b34a-977f404af4ec\" class=\"GINGER_SOFTWARE_mark\">serviceBehaviors<\/span><\/span><\/span><\/span><\/span>\u00a0section of web<span id=\"e7b79c35-e39e-44f1-89bc-831959bf8364\" class=\"GINGER_SOFTWARE_mark\"><span id=\"5fa2bafb-2951-432a-bcac-c73c468c6ded\" class=\"GINGER_SOFTWARE_mark\"><span id=\"3ef30ea8-7132-4d0c-afec-92d66af92e58\" class=\"GINGER_SOFTWARE_mark\"><span id=\"5db5b8f6-c5f7-4a47-97db-87635272377f\" class=\"GINGER_SOFTWARE_mark\"><span id=\"a81c99de-d2cb-4550-8703-b373831047cd\" class=\"GINGER_SOFTWARE_mark\">.<\/span><\/span><\/span><\/span><\/span><span id=\"2e0c3aef-63f8-4ba7-9f25-c6eeeedc8f8e\" class=\"GINGER_SOFTWARE_mark\"><span id=\"4a426cde-a4a7-4c6f-ab66-f1f9ad34828b\" class=\"GINGER_SOFTWARE_mark\"><span id=\"0b40d9a0-2c21-4e2d-b6f3-c8b646e35d87\" class=\"GINGER_SOFTWARE_mark\"><span id=\"35856ba8-c1e5-4011-a3e2-9fdc588dfbe5\" class=\"GINGER_SOFTWARE_mark\"><span id=\"f9bf5dd5-bc2c-4bfe-8dc1-19e16e32b455\" class=\"GINGER_SOFTWARE_mark\">confing<\/span><\/span><\/span><\/span><\/span>.<\/p>\n<pre>&lt;serviceBehaviors&gt; \r\n &lt;behavior name=\"serviceBehavior\"&gt; \r\n &lt;serviceThrottling \r\n maxConcurrentCalls=\"16\"\r\n maxConcurrentSessions=\"10\"\r\n maxConcurrentInstances=\"2147483647\" \r\n \/&gt; \r\n &lt;\/behavior&gt; \r\n&lt;\/serviceBehaviors&gt;<\/pre>\n<p>In my case, I needed to make this WCF accept the client <span id=\"63c1ceac-3085-45fe-b67c-16f645346104\" class=\"GINGER_SOFTWARE_mark\"><span id=\"bc30e313-ad3b-4292-89f5-8eab1f641a0d\" class=\"GINGER_SOFTWARE_mark\"><span id=\"639267cd-af55-417c-ae01-51917708ec6d\" class=\"GINGER_SOFTWARE_mark\"><span id=\"19b45173-d535-486e-b6cd-03b29d9ce6e1\" class=\"GINGER_SOFTWARE_mark\"><span id=\"eab44f09-4bb9-440b-b32c-6b0effdf11f5\" class=\"GINGER_SOFTWARE_mark\">request as<\/span><\/span><\/span><\/span><\/span> fast as it\u00a0can, but had to call the back-end process in a given interval.<\/p>\n<p>To accomplish this, I changed <span id=\"cfa61294-06ef-42d4-8577-15d8f9f0091e\" class=\"GINGER_SOFTWARE_mark\"><span id=\"cce7c3d9-5665-4ff0-b526-3e3ba2dd36e9\" class=\"GINGER_SOFTWARE_mark\"><span id=\"e3a92720-279f-451c-9cde-835c3d5abe2d\" class=\"GINGER_SOFTWARE_mark\"><span id=\"2c6f8a20-2f1f-4833-ac90-d22a9bb5cf74\" class=\"GINGER_SOFTWARE_mark\"><span id=\"6bc4418c-7453-42ce-b0ee-55af0f4f4d8f\" class=\"GINGER_SOFTWARE_mark\">the configuration<\/span><\/span><\/span><\/span><\/span> as below.<\/p>\n<pre>&lt;serviceBehaviors&gt; \r\n &lt;behavior name=\"serviceBehavior\"&gt; \r\n &lt;serviceThrottling maxConcurrentInstances=\"1\" \/&gt; \r\n &lt;\/behavior&gt; \r\n&lt;\/serviceBehaviors&gt;<\/pre>\n<p>This configuration means the WCF will have only a single instance. However, by default, this WCF can have multiple threads working <span id=\"ae628db1-06fe-449a-b595-63feb76f12a2\" class=\"GINGER_SOFTWARE_mark\">on<\/span> <span id=\"a4c47282-1213-4455-939f-0af31eec6898\" class=\"GINGER_SOFTWARE_mark\">concurrency<\/span>.<\/p>\n<p>The basic idea to throttle is having a static variable that can work as\u00a0a gate keeper.<\/p>\n<pre>public class Throttle : IThrottle\r\n{\r\n public static DateTime staticTime;\r\n\r\nDoWork()\r\n {\r\n \/\/ do something\r\n }\r\n\r\nwhile (true)\r\n {\r\n if (DateTime.Now - westpaccws.staticTime &gt; TimeSpan.FromMilliseconds(300))\r\n {\r\n westpaccws.staticTime = DateTime.Now;\r\n break;\r\n }\r\n }\r\n\r\nDoThrottledWork()\r\n {\r\n \/\/ do something else that needs to be throttled.\r\n }\r\n}<\/pre>\n<p>In this example, the static <span id=\"b8405516-ef23-483a-9305-9384169b68a3\" class=\"GINGER_SOFTWARE_mark\"><span id=\"27b36a41-f23b-4324-bd46-b5b4250780ca\" class=\"GINGER_SOFTWARE_mark\"><span id=\"d714a23f-878d-4b15-9b1a-0457d90f78eb\" class=\"GINGER_SOFTWARE_mark\"><span id=\"b95e494c-f6db-46a7-b913-fd426c9ec957\" class=\"GINGER_SOFTWARE_mark\"><span id=\"062ce47e-8bd0-4ab3-a3cc-d4fd25c401f0\" class=\"GINGER_SOFTWARE_mark\">varialbe<\/span><\/span><\/span><\/span><\/span>\u00a0&#8216;<span id=\"a68eb5eb-013b-4052-ae31-8f9b7b9dfad9\" class=\"GINGER_SOFTWARE_mark\"><span id=\"3ba02119-2209-4693-b0db-e400fcd8fab4\" class=\"GINGER_SOFTWARE_mark\"><span id=\"b87a9017-ec7c-422e-98a6-d9d4c705b26f\" class=\"GINGER_SOFTWARE_mark\"><span id=\"9d717113-70d8-4d01-815c-c0df8be6b0ed\" class=\"GINGER_SOFTWARE_mark\"><span id=\"023eff35-033f-4c59-a8a6-50c6a25c10f3\" class=\"GINGER_SOFTWARE_mark\">staticTime&#8217;<\/span><\/span><\/span><\/span><\/span>\u00a0is shared by all threads. Only after 0.3 seconds has passed, a thread can proceed to the next step. The static variable must be defined in the main class of the service. Otherwise, the static variable will not work as we expected.<\/p>\n<p>This looked like working at first. But, not really. In some cases, multiple threads accessed the <span id=\"40efbd1a-72bc-4b6f-a0d5-e4f8f426bba2\" class=\"GINGER_SOFTWARE_mark\"><span id=\"6fb280c5-eb82-4675-a2a8-76d892b470fc\" class=\"GINGER_SOFTWARE_mark\"><span id=\"5d02ddd9-964e-4a7e-8834-1a27536850ff\" class=\"GINGER_SOFTWARE_mark\"><span id=\"beb127e5-e5b5-4666-9d0d-fceb47c115ed\" class=\"GINGER_SOFTWARE_mark\"><span id=\"3bb4e893-dacc-47cc-bf32-108eaa7ce334\" class=\"GINGER_SOFTWARE_mark\">staticTime<\/span><\/span><\/span><\/span><\/span>\u00a0at the same time and each one decided that it&#8217;s good to go.<\/p>\n<p>So, I had to add <span id=\"db9029d8-74a4-4e68-b033-4bb86dedb863\" class=\"GINGER_SOFTWARE_mark\"><span id=\"07e09d7d-12c4-488a-b272-554684b8cec4\" class=\"GINGER_SOFTWARE_mark\"><span id=\"b3db05a6-fb4b-42d7-8e71-85b00885092f\" class=\"GINGER_SOFTWARE_mark\"><span id=\"02ac0705-0d10-46ce-aaf9-ff0d30c83aed\" class=\"GINGER_SOFTWARE_mark\"><span id=\"c056e80e-aaf8-4d11-9649-c72bff3c7fda\" class=\"GINGER_SOFTWARE_mark\">lock<\/span><\/span><\/span><\/span><\/span>\u00a0around time checking routine.<\/p>\n<pre>public class Throttle : IThrottle\r\n{\r\n public static DateTime staticTime;\r\n public static Object lockthis = new object();\r\n\r\nDoWork()\r\n {\r\n \/\/ do something\r\n }\r\n\r\nwhile (true)\r\n {\r\n lock (lockthis)\r\n {\r\n if (DateTime.Now - westpaccws.staticTime &gt; TimeSpan.FromMilliseconds(300))\r\n {\r\n westpaccws.staticTime = DateTime.Now;\r\n break;\r\n }\r\n }\r\n }\r\n\r\nDoThrottledWork()\r\n {\r\n \/\/ do something else that needs to be throttled.\r\n }\r\n}<\/pre>\n<p>This works well for me as I have only one WCF installed in the server.<\/p>\n<p>If this will be put on the load balanced environment, the static variable may need to be changed to something else, such as a separate windows service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If a WCF service internally connected to a legacy system which does not support multi threading, the WCF needs to be throttled. The easiest way to achieve this is to make the WCF works sequentially by setting this up not to work concurrently. By default, a WCF configured to process in multiple instances and concurrency&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2014\/09\/10\/how-to-throttle-a-section-in-wcf\/\">Continue reading <span class=\"screen-reader-text\">How to throttle a section in WCF<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[182,183,181],"class_list":["post-837","post","type-post","status-publish","format-standard","hentry","category-wcf","tag-multi-threading","tag-throttle","tag-wcf-2","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":18,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/837\/revisions\/979"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}