■ 클래스 라이브러리에서 WCF 서비스 참조를 추가하는 방법을 보여준다.
[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 |
using System; using System.Windows.Forms; using TestLibrary; 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); try { this.resultTextBox.Text = CalculatorClient.Divide(numerator, denominator).ToString(); } catch(Exception exception) { MessageBox.Show(this, exception.Message); } } #endregion } } |
▶ App.config
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration> |
[TestLibrary 프로젝트]
▶ CalculatorClient.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 |
using System.Configuration; using System.ServiceModel.Configuration; using TestLibrary.CalculatorService; namespace TestLibrary { /// <summary> /// 계산기 클라이언트 /// </summary> public static class CalculatorClient { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private static Configuration _configuration; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - CalculatorClient() /// <summary> /// 생성자 /// </summary> static CalculatorClient() { _configuration = ConfigurationManager.OpenMappedExeConfiguration ( new ExeConfigurationFileMap { ExeConfigFilename = "TestLibrary.dll.config" }, ConfigurationUserLevel.None ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 나누기 - Divide(numerator, denominator) /// <summary> /// 나누기 /// </summary> /// <param name="numerator">분자</param> /// <param name="denominator">분모</param> /// <returns>몫</returns> public static int Divide(int numerator, int denominator) { using ( ConfigurationChannelFactory<ICalculatorService> channelFactory = new ConfigurationChannelFactory<ICalculatorService> ( "BasicHttpBinding_ICalculatorService", _configuration, null ) ) { ICalculatorService calculatorService = channelFactory.CreateChannel(); int result = calculatorService.Divide(numerator, denominator); return result; } } #endregion } } |
▶ app.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <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> |