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

TestSolution.zip
다운로드

[TestServer 프로젝트]

▶ IReportService.cs

using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 리포트 서비스
    /// </summary>
    [ServiceContract(CallbackContract = typeof(IReportServiceCallback))]
    public interface IReportService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 리포트 처리하기 - ProcessReport()

        /// <summary>
        /// 리포트 처리하기
        /// </summary>
        [OperationContract]
        void ProcessReport();

        #endregion
    }
}

 

728x90

 

▶ IReportServiceCallback.cs

using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 리포트 서비스 콜백 인터페이스
    /// </summary>
    public interface IReportServiceCallback
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 진행하기 - Progress(percentage)

        /// <summary>
        /// 진행하기
        /// </summary>
        /// <param name="percentage">백분율</param>
        [OperationContract]
        void Progress(int percentage);

        #endregion
    }
}

 

300x250

 

▶ ReportService.cs

using System.ServiceModel;
using System.Threading;

namespace TestServer
{
    /// <summary>
    /// 리포트 서비스
    /// </summary>
    [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
    public class ReportService : IReportService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 리포트 처리하기 - ProcessReport()

        /// <summary>
        /// 리포트 처리하기
        /// </summary>
        public void ProcessReport()
        {
            for(int i = 1; i <= 100; i++)
            {
                Thread.Sleep(100);

                OperationContext.Current.GetCallbackChannel<IReportServiceCallback>().Progress(i);
            }
        }

        #endregion
    }
}

 

▶ 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(ReportService)))
            {
                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>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="mexBehavior" name="TestServer.ReportService">
                <endpoint
                    address="ReportService"
                    binding="netTcpBinding"
                    contract="TestServer.IReportService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080" />
                        <add baseAddress="net.tcp://localhost:8090" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

 

[TestClient 프로젝트]

▶ MainForm.cs

using System;
using System.Windows.Forms;
using System.ServiceModel;

using TestClient.ReportService;

namespace TestClient
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    [CallbackBehavior(UseSynchronizationContext = false)]
    public partial class MainForm : Form, IReportServiceCallback
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.processReportButton.Click += processReportButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 진행하기 - Progress(percentage)

        /// <summary>
        /// 진행하기
        /// </summary>
        /// <param name="percentage">백분율</param>
        public void Progress(int percentage)
        {
            this.progressTextBox.Text = percentage.ToString() + " % 완료";
        }

        #endregion
        
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 리포트 처리하기 버튼 클릭시 처리하기 - processReportButton_Click(sender, e)

        /// <summary>
        /// 리포트 처리하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void processReportButton_Click(object sender, EventArgs e)
        {
            InstanceContext instanceContext = new InstanceContext(this);

            ReportServiceClient client = new ReportServiceClient(instanceContext, "NetTcpBinding_IReportService");

            client.ProcessReport();            
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요