첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestSolution.zip
다운로드

[TestServer 프로젝트]

▶ ISampleService.cs

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
    }
}

 

728x90

 

▶ SampleService.cs

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
    }
}

 

300x250

 

▶ Program.cs

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

<?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

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요