■ WCF throttling을 코드로 설정하는 방법을 보여준다.
[TestServer 프로젝트]
▶ ISimpleService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 단순 서비스 /// </summary> [ServiceContract] public interface ISimpleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 작업 하기 - DoWork() /// <summary> /// 작업 하기 /// </summary> [OperationContract(IsOneWay = true)] void DoWork(); #endregion } } |
▶ SimpleService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.ServiceModel; using System.Threading; namespace TestServer { [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)] public class SimpleService : ISimpleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 작업 하기 - DoWork() /// <summary> /// 작업 하기 /// </summary> public void DoWork() { Thread.Sleep(1000); Console.WriteLine ( "[{0}] {1} 스레드가 요구를 처리했습니다.", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId ); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.ServiceModel; using System.ServiceModel.Description; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { ////////////////////////////////////////////////////////////////////////////////////////// Method //////////////////////////////////////////////////////////////////////////////// Static ////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(ServiceHost serviceHost = new ServiceHost(typeof(SimpleService))) { ServiceThrottlingBehavior serviceThrottlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 3, MaxConcurrentInstances = 3, MaxConcurrentSessions = 100 }; serviceHost.Description.Behaviors.Add(serviceThrottlingBehavior); serviceHost.Open(); Console.WriteLine("서버가 시작되었습니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); Console.ReadKey(true); } } #endregion } } |
▶ App.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="TestServer.SimpleService" behaviorConfiguration="mexBehavior"> <endpoint address="SimpleService" binding="basicHttpBinding" contract="TestServer.ISimpleService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration> |
[TestClient 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Threading; using TestClient.SimpleService; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.WriteLine("작업 요청을 시작하기 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); SimpleServiceClient client = new SimpleServiceClient("BasicHttpBinding_ISimpleService"); for(int i = 1; i <= 100; i++) { Console.WriteLine("[{0}] {1}번 작업을 요청합니다.", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), i); Thread thread = new Thread(client.DoWork); thread.Start(); } } #endregion } } |
▶ App.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_ISimpleService" /> </basicHttpBinding> </bindings> <client> <endpoint name="BasicHttpBinding_ISimpleService" address="http://localhost:8080/SimpleService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISimpleService" contract="SimpleService.ISimpleService" /> </client> </system.serviceModel> </configuration> |