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

TestSolution.zip
다운로드

[TestServer 프로젝트]

▶ IDownloadService.cs

using System.ServiceModel;

namespace TestServer
{
    /// <summary>
    /// 다운로드 서비스
    /// </summary>
    [ServiceContract]
    public interface IDownloadService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 문서 다운로드 하기 - DownloadDocument(filePath)

        /// <summary>
        /// 문서 다운로드 하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>다운로드 파일</returns>
        [OperationContract]
        DownloadFile DownloadDocument(string filePath);

        #endregion
    }
}

 

728x90

 

▶ DownloadService.cs

using System.IO;

namespace TestServer
{
    /// <summary>
    /// 다운로드 서비스
    /// </summary>
    public class DownloadService : IDownloadService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 문서 다운로드 하기 - DownloadDocument(filePath)

        /// <summary>
        /// 문서 다운로드 하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>다운로드 파일</returns>
        public DownloadFile DownloadDocument(string filePath)
        {
            string fileName = Path.GetFileName(filePath);

            DownloadFile downloadFile = new DownloadFile();

            downloadFile.FileName = fileName;
            downloadFile.Content  = File.ReadAllBytes(filePath);

            return downloadFile;
        }

        #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(DownloadService)))
            {
                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>
        <bindings>
            <wsHttpBinding>
                <binding name="wsHttp"
                    messageEncoding="Mtom"
                    maxReceivedMessageSize="1073741824">
                    <readerQuotas maxArrayLength="1073741824"/>
                </binding>
            </wsHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="TestServer.DownloadService" behaviorConfiguration="mexBehavior">
                <endpoint
                    address="DownloadService"
                    binding="wsHttpBinding"
                    bindingConfiguration="wsHttp"
                    contract="TestServer.IDownloadService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

 

[TestClient 프로젝트]

▶ MainForm.cs

using System;
using System.Windows.Forms;

using TestClient.DownloadService;

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

        #region 생성자 - MainForm()

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

            this.downloadButton.Click += downloadButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 다운로드 버튼 클릭시 처리하기 - downloadButton_Click(sender, e)

        /// <summary>
        /// 다운로드 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void downloadButton_Click(object sender, EventArgs e)
        {
            string downloadFilePath = this.downloadFilePathTextBox.Text.Trim();

            if(string.IsNullOrEmpty(downloadFilePath))
            {
                MessageBox.Show("다운로드 파일 경로를 입력해 주시기 바랍니다.");

                return;
            }

            using(DownloadServiceClient client = new DownloadServiceClient("WSHttpBinding_IDownloadService"))
            {
                DownloadFile downloadFile = client.DownloadDocument(downloadFilePath);

                MessageBox.Show("다운로드를 완료했습니다 : " + downloadFile.FileName);
            }
        }

        #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>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IDownloadService"
                    messageEncoding="Mtom"
                    maxReceivedMessageSize="1073741824">
                    <readerQuotas maxArrayLength="1073741824"/>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint name="WSHttpBinding_IDownloadService"
                address="http://localhost:8080/DownloadService"
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IDownloadService"
                contract="DownloadService.IDownloadService" />
        </client>
    </system.serviceModel>
</configuration>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요