■ WSDualHttpBinding 클래스에서 콜백 메소드를 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ IDataService.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 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 데이터 서비스 인터페이스 /// </summary> [ServiceContract(CallbackContract = typeof(IDataServiceCallback))] public interface IDataService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 고객 리스트 구하기 - GetCustomerList(count) /// <summary> /// 고객 리스트 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>고객 리스트</returns> [OperationContract] bool GetCustomerList(int count); #endregion } } |
▶ IDataServiceCallback.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 |
using System.Collections.Generic; using System.ServiceModel; namespace TestServer { /// <summary> /// 데이터 서비스 콜백 인터페이스 /// </summary> public interface IDataServiceCallback { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 고객 리스트 구하기 완료시 처리하기 - OnGetCustomerListCompleted(customerList) /// <summary> /// 고객 리스트 구하기 완료시 처리하기 /// </summary> /// <param name="customerList">고객 리스트</param> [OperationContract(IsOneWay = true)] void OnGetCustomerListCompleted(List<Customer> customerList); #endregion } } |
▶ DataService.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 47 48 49 50 51 52 53 54 55 56 57 |
using System; using System.Collections.Generic; using System.ServiceModel; using System.Threading.Tasks; namespace TestServer { /// <summary> /// 데이터 서비스 /// </summary> public class DataService : IDataService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 고객 리스트 구하기 - GetCustomerList(count) /// <summary> /// 고객 리스트 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>처리 결과</returns> public bool GetCustomerList(int count) { Console.WriteLine("고객 리스트를 구합니다 : " + count.ToString()); IDataServiceCallback callback = OperationContext.Current.GetCallbackChannel<IDataServiceCallback>(); Task task = new Task(() => { List<Customer> sourceList = new List<Customer>(); for(int i = 1; i < count + 1; i++) { Customer customer = new Customer(); customer.ID = i; customer.Name = string.Format("고객 {0}", i); sourceList.Add(customer); } Console.WriteLine("고객 리스트 구하기 콜백을 호출합니다."); callback.OnGetCustomerListCompleted(sourceList); }); task.Start(); return true; } #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 |
using System; using System.ServiceModel; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ServiceHost serviceHost = new ServiceHost(typeof(DataService)); serviceHost.Open(); Console.WriteLine("Press any key to stop."); Console.ReadKey(); Console.WriteLine("Stopping, please wait..."); if(serviceHost.State != CommunicationState.Closed) { serviceHost.Close(); } } #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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="DataServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <serviceThrottling maxConcurrentCalls="128" maxConcurrentSessions="128" maxConcurrentInstances="128" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <wsDualHttpBinding> <binding name="dual" receiveTimeout="01:00:00" sendTimeout="01:00:00" maxReceivedMessageSize="2000000000"> <readerQuotas maxDepth="2000000000" maxStringContentLength="2000000000" maxArrayLength="2000000000" maxBytesPerRead="2000000000" maxNameTableCharCount="2000000000" /> </binding> </wsDualHttpBinding> </bindings> <services> <service name="TestServer.DataService" behaviorConfiguration="DataServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:7777/DataService.svc" /> </baseAddresses> </host> <endpoint name="dual" contract="TestServer.IDataService" binding="wsDualHttpBinding" bindingConfiguration="dual" address="dual" /> </service> </services> </system.serviceModel> </configuration> |
[TestClient 프로젝트]
▶ DataServiceCallback.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 |
using System; using System.ServiceModel; using TestClient.ServiceReference; namespace TestClient { /// <summary> /// 데이터 서비스 콜백 /// </summary> [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] public class DataServiceCallback : IDataServiceCallback { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 고객 리스트 구하기 완료시 처리하기 - OnGetCustomerListCompleted(customerList) /// <summary> /// 고객 리스트 구하기 완료시 처리하기 /// </summary> /// <param name="customerList">고객 리스트</param> public void OnGetCustomerListCompleted(Customer[] customerList) { Console.WriteLine("고객 수 : " + customerList.Length); } #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 47 48 49 |
using System; using System.ServiceModel; using TestClient.ServiceReference; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Random random = new Random(DateTime.Now.Millisecond); DataServiceCallback callback = new DataServiceCallback(); InstanceContext context = new InstanceContext(callback); using(DataServiceClient client = new DataServiceClient(context, "dual")) { Console.WriteLine("고객 리스트를 구합니다."); for(int i = 0; i < 100; i++) { int count = random.Next(50, 100); client.GetCustomerList(count); } } 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 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <wsDualHttpBinding> <binding name="dual" receiveTimeout="01:00:00" sendTimeout="01:00:00" maxReceivedMessageSize="2000000000"> <readerQuotas maxDepth="2000000000" maxStringContentLength="2000000000" maxArrayLength="2000000000" maxBytesPerRead="2000000000" maxNameTableCharCount="2000000000" /> </binding> </wsDualHttpBinding> </bindings> <client> <endpoint name="dual" contract="ServiceReference.IDataService" binding="wsDualHttpBinding" bindingConfiguration="dual" address="http://localhost:7777/DataService.svc/dual" /> </client> </system.serviceModel> </configuration> |