■ 요청/응답 메시지 교환 패턴을 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ ISampleService.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 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 샘플 서비스 /// </summary> [ServiceContract] public interface ISampleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 요청/응답 작업하기 - RequestReplyOperation() /// <summary> /// 요청/응답 작업하기 /// </summary> /// <returns>메시지</returns> [OperationContract(IsOneWay=false)] string RequestReplyOperation(); #endregion #region 요청/응답 작업하기 (예외 발생) - RequestReplyOperationThrowsException(); /// <summary> /// 요청/응답 작업하기 (예외 발생) /// </summary> /// <returns>메시지</returns> [OperationContract(IsOneWay = false)] string RequestReplyOperationThrowsException(); #endregion } } |
▶ SampleService.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 |
using System; using System.Threading; namespace TestServer { /// <summary> /// 샘플 서비스 /// </summary> public class SampleService : ISampleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 요청/응답 작업하기 - RequestReplyOperation() /// <summary> /// 요청/응답 작업하기 /// </summary> /// <returns>메시지</returns> public string RequestReplyOperation() { DateTime startTime = DateTime.Now; Thread.Sleep(2000); DateTime endTime = DateTime.Now; return "경과 시간 : " + endTime.Subtract(startTime).Seconds.ToString() + "초"; } #endregion #region 요청/응답 작업하기 (예외 발생) - RequestReplyOperationThrowsException() /// <summary> /// 요청/응답 작업하기 (예외 발생) /// </summary> /// <returns>메시지</returns> public string RequestReplyOperationThrowsException() { throw new NotImplementedException(); } #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 |
using System; using System.ServiceModel; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { ////////////////////////////////////////////////////////////////////////////////////////// Method //////////////////////////////////////////////////////////////////////////////// Static ////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(ServiceHost serviceHost = new ServiceHost(typeof(SampleService))) { 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 28 29 30 31 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <services> <service name="TestServer.SampleService" behaviorConfiguration="mexBehavior"> <endpoint address="CalculatorService" binding="basicHttpBinding" contract="TestServer.ISampleService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> |
[TestClient 프로젝트]
▶ MainForm.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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System; using System.Windows.Forms; using TestClient.SampleService; namespace TestClient { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 클라이언트 /// </summary> private SampleServiceClient client; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.client = new SampleServiceClient("BasicHttpBinding_ISampleService"); this.testButton1.Click += testButton1_Click; this.testButton2.Click += testButton2_Click; this.clearButton.Click += clearButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 요청/응답 작업하기 버튼 클릭시 처리하기 - testButton1_Click(sender, e) /// <summary> /// 요청/응답 작업하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton1_Click(object sender, EventArgs e) { try { this.messageListBox.Items.Add("요청/응답 작업을 시작합니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); this.testButton1.Enabled = false; this.messageListBox.Items.Add(this.client.RequestReplyOperation()); this.testButton1.Enabled = true; this.messageListBox.Items.Add("요청/응답 작업을 완료합니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); this.messageListBox.Items.Add(string.Empty); } catch(Exception exception) { MessageBox.Show(exception.Message); } } #endregion #region 요청/응답 작업하기 (예외 발생) 버튼 클릭시 처리하기 - testButton1_Click(sender, e) /// <summary> /// 요청/응답 작업하기 (예외 발생) 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton2_Click(object sender, EventArgs e) { try { this.messageListBox.Items.Add("요청/응답 작업(예외 발생)을 시작합니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); this.testButton1.Enabled = false; this.messageListBox.Items.Add(this.client.RequestReplyOperationThrowsException()); this.testButton1.Enabled = true; this.messageListBox.Items.Add("요청/응답 작업(예외 발생)을 완료합니다 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); this.messageListBox.Items.Add(string.Empty); } catch(Exception exception) { MessageBox.Show(exception.Message); } } #endregion #region 목록 지우기 버튼 클릭시 처리하기 - testButton1_Click(sender, e) /// <summary> /// 목록 지우기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void clearButton_Click(object sender, EventArgs e) { this.messageListBox.Items.Clear(); } #endregion } } |