1: using System;
2: using System.Data;
3: using System.Configuration;
4: using System.Web;
5: using System.Web.Security;
6: using System.Web.UI;
7: using System.Web.UI.HtmlControls;
8: using System.Web.UI.WebControls;
9: using System.Web.UI.WebControls.WebParts;
10:
11: namespace Server
12: {
13: /// <summary>
14: ///
15: /// </summary>
16: public class WebServicesGateway
17: {
18: private int maxThreadCount = 3;
19: private static int currentThreadCount;
20: private static Gateway gateway;
21: private object instanceLock = new object();
22:
23: public WebServicesGateway(int threadCount)
24: {
25: maxThreadCount = threadCount;
26: gateway = new Gateway(this);
27: }
28:
29: private void DecrementCount()
30: {
31: lock (instanceLock)
32: {
33: currentThreadCount--;
34: }
35: }
36:
37: public IDisposable GatewayRequest()
38: {
39: lock (instanceLock)
40: {
41: if (currentThreadCount >= maxThreadCount)
42: {
43: throw new WebServicesGatewayLimitException("Gateway has reached its limit");
44: }
45: currentThreadCount++;
46: }
47: return gateway;
48: }
49:
50: private class Gateway : IDisposable
51: {
52: WebServicesGateway webServicesGateway;
53:
54: public Gateway(WebServicesGateway webServicesGateway)
55: {
56: this.webServicesGateway = webServicesGateway;
57: }
58:
59: public void Dispose()
60: {
61: this.webServicesGateway.DecrementCount();
62: }
63: }
64: }
65:
66: public class WebServicesGatewayLimitException : Exception
67: {
68: private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
69:
70: public WebServicesGatewayLimitException(string message)
71: : base(message)
72: {
73: log.Error(message);
74: }
75: public WebServicesGatewayLimitException(string message, Exception exception)
76: : base(message, exception)
77: {
78: log.Error(message, exception);
79: }
80: }
81: }
82: