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

TestSolution.zip
다운로드

[TestServer 프로젝트]

▶ IDataService.cs

using System.IO;
using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 데이터 서비스 인터페이스
    /// </summary>
    [ServiceContract]
    public interface IDataService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 데이터 구하기 - GetData(dataCount, useGZipStream)

        /// <summary>
        /// 데이터 구하기
        /// </summary>
        /// <param name="dataCount">데이터 카운트</param>
        /// <param name="useGZipStream">GZipStream 사용 여부</param>
        /// <returns>스트림</returns>
        [OperationContract]
        Stream GetData(int dataCount, bool useGZipStream);

        #endregion
    }
}

 

728x90

 

▶ DataService.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;

namespace TestServer
{
    /// <summary>
    /// 데이터 서비스
    /// </summary>
    public class DataService : IDataService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 데이터 구하기 - GetData(dataCount, useGZipStream)

        /// <summary>
        /// 데이터 구하기
        /// </summary>
        /// <param name="dataCount">데이터 카운트</param>
        /// <param name="useGZipStream">GZipStream 사용 여부</param>
        /// <returns>스트림</returns>
        public Stream GetData(int dataCount, bool useGZipStream)
        {
            WriteMessage("START GetData : DataCount={0}", dataCount);

            #region 결과 리스트를 생성한다.

            List<string> resultlist = new List<string>();

            for(int i = 0; i < dataCount; i++)
            {
                resultlist.Add(Guid.NewGuid().ToString().ToUpper());
            }

            #endregion

            if(useGZipStream)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
            
                MemoryStream memoryStream = new MemoryStream();
            
                GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
            
                binaryFormatter.Serialize(gZipStream, resultlist);
            
                gZipStream.Close();
            
                memoryStream.Position = 0;
            
                WriteMessage("END GetData");
            
                return memoryStream;
            }
            else
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
            
                MemoryStream memoryStream = new MemoryStream();
            
                binaryFormatter.Serialize(memoryStream, resultlist);
            
                memoryStream.Position = 0;
            
                WriteMessage("END GetData");
            
                return memoryStream;
            }
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 메시지 쓰기 - WriteMessage(format, parameterArray)

        /// <summary>
        /// 메시지 쓰기
        /// </summary>
        /// <param name="format">포맷 문자열</param>
        /// <param name="parameterArray">매개 변수 배열</param>
        private void WriteMessage(string format, params object[] parameterArray)
        {
            string message;

            if(parameterArray.Length == 0)
            {
                message = format;
            }
            else
            {
                message = string.Format(format, parameterArray);
            }

            Console.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message));
        }

        #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(DataService)))
            {
                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>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="basicHttp"
                    sendTimeout="00:10:00"
                    receiveTimeout="00:10:00"
                    maxReceivedMessageSize="1262485504"
                    transferMode="Streamed" />
            </basicHttpBinding>
            <netTcpBinding>
                <binding name="netTcp"
                    sendTimeout="00:10:00"
                    receiveTimeout="00:10:00"
                    maxReceivedMessageSize="1262485504"
                    transferMode="Streamed" />
            </netTcpBinding>
        </bindings>
        <services>
            <service behaviorConfiguration="mexBehavior" name="TestServer.DataService">
                <endpoint
                    address="FileService"
                    binding="basicHttpBinding"
                    bindingConfiguration="basicHttp"
                    contract="TestServer.IDataService" />
                <endpoint
                    address="FileService"
                    binding="netTcpBinding"
                    bindingConfiguration="netTcp"
                    contract="TestServer.IDataService" />
                <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.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

using TestClient.DataService;

namespace TestClient
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

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

            this.getDataButton.Click += getDataButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 데이터 구하기 버튼 클릭시 처리하기 - downloadButton_Click(sender, e)

        /// <summary>
        /// 데이터 구하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void getDataButton_Click(object sender, EventArgs e)
        {
            int dataCount;

            try
            {
                dataCount = Convert.ToInt32(this.dataCountTextBox.Text);
            }
            catch
            {
                MessageBox.Show("유효한 데이터 카운트를 입력해 주시기 바랍니다.");

                return;
            }

            this.textBox.Text = string.Empty;

            WriteMessage("START GetData : DataCount={0}", dataCount);

            try
            {
                Enabled = false;

                DateTime startTime = DateTime.Now;

                using(DataServiceClient client = new DataServiceClient("NetTcpBinding_IDataService"))
                {
                    List<string> list;

                    if(this.useGZipStreamCheckBox.Checked)
                    {
                        Stream stream = client.GetData(dataCount, true);
                    
                        GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress);
                    
                        BinaryFormatter binaryFormatter = new BinaryFormatter();
                    
                        list = (List<string>)binaryFormatter.Deserialize(gZipStream);
                    
                        gZipStream.Close();
                    
                        stream.Close();
                    }
                    else
                    {
                        Stream stream = client.GetData(dataCount, false);
                        
                        BinaryFormatter binaryFormatter = new BinaryFormatter();
                        
                        list = (List<string>)binaryFormatter.Deserialize(stream);
                        
                        stream.Close();
                    }

                    StringBuilder stringBuilder = new StringBuilder();

                    for(int i = 0; i < list.Count; i++)
                    {
                        stringBuilder.AppendLine(list[i]);
                    }

                    this.textBox.Text = stringBuilder.ToString().Trim();
                }

                DateTime endTime = DateTime.Now;

                WriteMessage("END GetData : Elapsed={0}", (endTime - startTime).ToString());
            }
            finally
            {
                Enabled = true;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 메시지 지우기 - ClearMessage()

        /// <summary>
        /// 메시지 지우기
        /// </summary>
        private void ClearMessage()
        {
            this.messageLabel.Text = string.Empty;
        }

        #endregion
        #region 메시지 쓰기 - WriteMessage(format, parameterArray)

        /// <summary>
        /// 메시지 쓰기
        /// </summary>
        /// <param name="format">포맷 문자열</param>
        /// <param name="parameterArray">매개 변수 배열</param>
        private void WriteMessage(string format, params object[] parameterArray)
        {
            string message;

            if(parameterArray.Length == 0)
            {
                message = format;
            }
            else
            {
                message = string.Format(format, parameterArray);
            }

            messageLabel.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message);
        }

        #endregion
    }
}

 

▶ App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDataService"
                    sendTimeout="00:10:00"
                    receiveTimeout="00:10:00"
                    maxReceivedMessageSize="1262485504" />
            </basicHttpBinding>
            <netTcpBinding>
                <binding name="NetTcpBinding_IDataService"
                    sendTimeout="00:10:00"
                    receiveTimeout="00:10:00"
                    maxReceivedMessageSize="1262485504"
                    transferMode="Streamed" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint name="BasicHttpBinding_IDataService"
                address="http://localhost:8080/FileService"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IDataService"
                contract="DataService.IDataService" />
            <endpoint name="NetTcpBinding_IDataService"
                address="net.tcp://localhost:8090/FileService"
                binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IDataService"
                contract="DataService.IDataService" />
        </client>
    </system.serviceModel>
</configuration>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요