■ 강한 타입의 SOAP 폴트를 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ DivideByZeroFault.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.Runtime.Serialization; namespace TestServer { /// <summary> /// 0으로 나누기 폴트 /// </summary> [DataContract] public class DivideByZeroFault { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 에러 - Error /// <summary> /// 에러 /// </summary> [DataMember] public string Error { get; set; } #endregion #region 상세 내역 - Detail [DataMember] public string Detail { get; set; } #endregion } } |
▶ 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 29 |
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] [FaultContract(typeof(DivideByZeroFault))] 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; 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) { try { return numerator / denominator; } catch(DivideByZeroException exception) { DivideByZeroFault divideByZeroFault = new DivideByZeroFault(); divideByZeroFault.Error = exception.Message; divideByZeroFault.Detail = "분모는 0이 될 수 없습니다."; throw new FaultException<DivideByZeroFault>(divideByZeroFault); } } #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<DivideByZeroFault> faultException) { MessageBox.Show(this, faultException.Detail.Error + "\r\n" + faultException.Detail.Detail); } } } #endregion } } |