■ 예외를 처리하는 방법을 보여준다.
[TestServer 프로젝트]
▶ ICalculatorService.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 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 계산기 서비스 인터페이스 /// </summary> [ServiceContract] public interface ICalculatorService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 나누기 - Divide(numerator, denominator) /// <summary> /// 나누기 /// </summary> /// <param name="numerator">분자</param> /// <param name="denominator">분모</param> /// <returns>몫</returns> [OperationContract] int Divide(int numerator, int denominator); #endregion } } |
▶ CalculatorService.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 |
namespace TestServer { /// <summary> /// 계산기 서비스 /// </summary> public class CalculatorService : ICalculatorService { ////////////////////////////////////////////////////////////////////////////////////////// Method //////////////////////////////////////////////////////////////////////////////// Public #region 나누기 - Divide(numerator, denominator) /// <summary> /// 나누기 /// </summary> /// <param name="numerator">분모</param> /// <param name="denominator">분자</param> /// <returns>몫</returns> public int Divide(int numerator, int denominator) { return numerator / denominator; } #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(TestServer.CalculatorService))) { 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.CalculatorService" behaviorConfiguration="mexBehavior"> <endpoint address="CalculatorService" binding="basicHttpBinding" contract="TestServer.ICalculatorService" /> <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 |
using System; using System.Windows.Forms; using TestClient.CalculatorService; namespace TestClient { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.calculateButton.Click += calculateButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 계산하기 - calculateButton_Click(sender, e) /// <summary> /// 계산하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void calculateButton_Click(object sender, EventArgs e) { int numerator = Convert.ToInt32(this.numeratorTextBox.Text ); int denominator = Convert.ToInt32(this.denominatorTextBox.Text); using(CalculatorServiceClient client = new CalculatorServiceClient("BasicHttpBinding_ICalculatorService")) { try { this.resultTextBox.Text = client.Divide(numerator, denominator).ToString(); } catch(Exception exception) { MessageBox.Show(this, exception.Message); } } } #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_ICalculatorService" /> </basicHttpBinding> </bindings> <client> <endpoint name="BasicHttpBinding_ICalculatorService" address="http://localhost:8080/CalculatorService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICalculatorService" contract="CalculatorService.ICalculatorService" /> </client> </system.serviceModel> </configuration> |