■ 폴트 예외를 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ 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 29 30 31 32 33 34 35 |
using System.ServiceModel; 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) { if(denominator == 0) { throw new FaultException("분모는 0이 될 수 없습니다.", new FaultCode("분모를 0으로 나눈 경우")); } return numerator / denominator; } #endregion } } |
[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 |
using System; using System.ServiceModel; 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(FaultException faultException) { MessageBox.Show(this, faultException.Code.Name + "\r\n" + faultException.Message); } } } #endregion } } |