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

▶ 서버 코드

using System;
using System.Collections;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace TestProject
{
    /// <summary>
    /// 상점 인터페이스
    /// </summary>
    [ServiceContract(Namespace="http://company.com/bookstore")]
    public interface IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 주문하기 - Order(userID, isbn, amount)

        /// <summary>
        /// 주문하기
        /// </summary>
        /// <param name="userID">사용자 ID</param>
        /// <param name="isbn">ISBN</param>
        /// <param name="amount">수량</param>
        /// <returns>주문 ID</returns>
        [OperationContract]
        int Order(string userID, string isbn, int amount);

        #endregion

        #region 취소하기 - Cancel(orderID)

        /// <summary>
        /// 취소하기
        /// </summary>
        /// <param name="orderID">주문 ID</param>
        /// <returns>처리 결과</returns>
        [OperationContract]
        bool Cancel(int orderID);

        #endregion

        #region 검색하기 - Search(title)

        /// <summary>
        /// 검색하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        [OperationContract]
        string[] Search(string title);

        #endregion
    }

    /// <summary>
    /// 상점 2 인터페이스
    /// </summary>
    [ServiceContract(Namespace="http://company.com/bookstore2")]
    public interface IBookStore2 : IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 검색하기 2 - Search2(title)

        /// <summary>
        /// 검색하기 2
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        [OperationContract]
        string[] Search2(string title);

        #endregion
    }

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class BookStoreService : IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 제목 배열
        /// </summary>
        protected static string[] _titleArray = new string[] { "도서 1", "도서 2", "도서 3" };

        /// <summary>
        /// 저자 배열
        /// </summary>
        protected static string[] _authorArray = new string[] { "저자 1", "저자 2", "저자 3" };

        #endregion

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

        #region Field

        /// <summary>
        /// 주문 ID
        /// </summary>
        private static int _orderID = 0;

        #endregion

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

        #region 주문하기 - Order(userID, isbn, amount)

        /// <summary>
        /// 주문하기
        /// </summary>
        /// <param name="userID">사용자 ID</param>
        /// <param name="isbn">ISBN</param>
        /// <param name="amount">수량</param>
        /// <returns>주문 ID</returns>
        public int Order(string userID, string isbn, int amount)
        {
            int price = GetUnitPrice(isbn) * amount;

            _orderID += 1;

            Console.WriteLine
            (
                "도서 주문 : 주문 ID={0}\n            사용자 ID={1}, ISBN={2}, 수량={3}, 가격={4}",
                _orderID,
                userID,
                isbn,
                amount,
                price
            );

            return _orderID;
        }

        #endregion

        #region 취소하기 - Cancel(orderID)

        /// <summary>
        /// 취소하기
        /// </summary>
        /// <param name="orderID">주문 ID</param>
        /// <returns>처리 결과</returns>
        public bool Cancel(int orderID)
        {
            Console.WriteLine("취소 주문 : 주문 ID={0}", orderID);

            return true;
        }

        #endregion

        #region 검색하기 - Search(title)

        /// <summary>
        /// 검색하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        public string[] Search(string title)
        {
            ArrayList arrayList = new ArrayList();

            for(int i = 0; i < _titleArray.Length; i++)
            {
                if(_titleArray[i].StartsWith(title) == true)
                {
                    arrayList.Add(_titleArray[i]);
                }
            }

            return (string[])arrayList.ToArray(typeof(string));
        }

        #endregion

        #region 단가 구하기 - GetUnitPrice(isbn)

        /// <summary>
        /// 단가 구하기
        /// </summary>
        /// <param name="isbn">ISBN</param>
        /// <returns>단가</returns>
        private int GetUnitPrice(string isbn)
        {
            return 35000;
        }

        #endregion
    }

    /// <summary>
    /// 상점 서비스 2
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class BookStoreService2 : BookStoreService, IBookStore2
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 검색하기 - Search2(title)

        /// <summary>
        /// 검색하기 2
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        public string[] Search2(string title)
        {
            ArrayList arrayList = new ArrayList();

            for(int i = 0; i < _titleArray.Length; i++)
            {
                if(_titleArray[i].IndexOf(title) >= 0)
                {
                    arrayList.Add(_titleArray[i]);
                }
            }

            return (string[])arrayList.ToArray(typeof(string));
        }

        #endregion
    }

    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Console.WriteLine("서버 : 계약 상속 서비스 예제...");

            ServiceHost serviceHost = new ServiceHost
            (
                typeof(BookStoreService2),
                new Uri("http://localhost/wcf/example/bookstoreservice")
            );

            serviceHost.AddServiceEndpoint(typeof(IBookStore2), new BasicHttpBinding(), string.Empty);

            ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();

            serviceMetadataBehavior.HttpGetEnabled = true;

            serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);

            serviceHost.Open();

            Console.WriteLine("서비스 중단을 위해 아무 키나 누르세요...");

            Console.ReadKey(true);

            serviceHost.Close();
        }

        #endregion
    }
}

 

728x90

 

▶ 클라이언트 코드

using System;
using System.ServiceModel;

namespace ServiceInheritanceClientApp
{
    /// <summary>
    /// 상점 인터페이스
    /// </summary>
    [ServiceContract(Namespace="http://company.com/bookstore")]
    public interface IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 주문하기 - Order(userID, isbn, amount)

        /// <summary>
        /// 주문하기
        /// </summary>
        /// <param name="userID">사용자 ID</param>
        /// <param name="isbn">ISBN</param>
        /// <param name="amount">수량</param>
        /// <returns>주문 ID</returns>
        [OperationContract]
        int Order(string userID, string isbn, int amount);

        #endregion

        #region 취소하기 - Cancel(orderID)

        /// <summary>
        /// 취소하기
        /// </summary>
        /// <param name="orderID">주문 ID</param>
        /// <returns>처리 결과</returns>
        [OperationContract]
        bool Cancel(int orderID);

        #endregion

        #region 검색하기 - Search(title)

        /// <summary>
        /// 검색하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        [OperationContract]
        string[] Search(string title);

        #endregion
    }

    /// <summary>
    /// 상점 클라이언트
    /// </summary>
    public class BookStoreClient : ClientBase<IBookStore>, IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - BookStoreClient(EndpointAddress, BasicHttpBinding)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="endpointAddress">EndpointAddress</param>
        /// <param name="basicHttpBinding">BasicHttpBinding</param>
        public BookStoreClient(EndpointAddress endpointAddress, BasicHttpBinding basicHttpBinding) :
            base(basicHttpBinding, endpointAddress)
        {
        }

        #endregion

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

        #region 주문하기 - Order(userID, isbn, amount)

        /// <summary>
        /// 주문하기
        /// </summary>
        /// <param name="userID">사용자 ID</param>
        /// <param name="isbn">ISBN</param>
        /// <param name="amount">수량</param>
        /// <returns>주문 ID</returns>
        public int Order(string userID, string isbn, int amount)
        {
            return Channel.Order(userID, isbn, amount);
        }

        #endregion

        #region 취소하기 - Cancel(orderID)

        /// <summary>
        /// 취소하기
        /// </summary>
        /// <param name="orderID">주문 ID</param>
        /// <returns>처리 결과</returns>
        public bool Cancel(int orderID)
        {
            return Channel.Cancel(orderID);
        }

        #endregion

        #region 검색하기 - Search(title)

        /// <summary>
        /// 검색하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        public string[] Search(string title)
        {
            return Channel.Search(title);
        }

        #endregion
    }

    /// <summary>
    /// 상점 2 인터페이스
    /// </summary>
    [ServiceContract(Namespace="http://company.com/bookstore2")]
    public interface IBookStore2 : IBookStore
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 검색하기 2 - Search2(title)

        /// <summary>
        /// 검색하기 2
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        [OperationContract]
        string[] Search2(string title);

        #endregion
    }

    /// <summary>
    /// 상점 2 클라이언트
    /// </summary>
    public class BookStore2Client : ClientBase<IBookStore2>, IBookStore2
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - BookStore2Client(endpointAddress, basicHttpBinding)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="endpointAddress">EndpointAddress</param>
        /// <param name="basicHttpBinding">BasicHttpBinding</param>
        public BookStore2Client(EndpointAddress endpointAddress, BasicHttpBinding basicHttpBinding) :
            base(basicHttpBinding, endpointAddress)
        {
        }

        #endregion

        // IBookStore
        #region 주문하기 - Order(userID, isbn, amount)

        /// <summary>
        /// 주문하기
        /// </summary>
        /// <param name="userID">사용자 ID</param>
        /// <param name="isbn">ISBN</param>
        /// <param name="amount">수량</param>
        /// <returns>주문 ID</returns>
        public int Order(string userID, string isbn, int amount)
        {
            return Channel.Order(userID, isbn, amount);
        }

        #endregion

        // IBookStore
        #region 취소하기 - Cancel(orderID)

        /// <summary>
        /// 취소하기
        /// </summary>
        /// <param name="orderID">주문 ID</param>
        /// <returns>처리 결과</returns>
        public bool Cancel(int orderID)
        {
            return Channel.Cancel(orderID);
        }

        #endregion

        // IBookStore
        #region 검색하기 - Search(title)

        /// <summary>
        /// 검색하기
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        public string[] Search(string title)
        {
            return Channel.Search(title);
        }

        #endregion

        // IBookStore2
        #region 검색하기 2 - Search2(title)

        /// <summary>
        /// 검색하기 2
        /// </summary>
        /// <param name="title">제목</param>
        /// <returns>제목 배열</returns>
        public string[] Search2(string title)
        {
            return Channel.Search2(title);
        }

        #endregion
    }

    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Console.WriteLine("클라이언트 : 계약 상속 서비스 예제...");

            int orderID;

            EndpointAddress  endpointAddress  = new EndpointAddress("http://localhost/wcf/example/bookstoreservice");
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

            using(BookStoreClient bookStoreClient = new BookStoreClient(endpointAddress, basicHttpBinding))
            {
                orderID = bookStoreClient.Order("BlackPearl", "000-0-000-00000-0", 2);

                Console.WriteLine("주문하였습니다. 주문 ID = {0}", orderID);
            }

            using(BookStore2Client bookStore2Client = new BookStore2Client(endpointAddress, basicHttpBinding))
            {
                bookStore2Client.Cancel(orderID);

                string[] titleArray = bookStore2Client.Search2("도서");

                Console.WriteLine("검색 결과 :");

                foreach(string title in titleArray)
                {
                    Console.WriteLine("    {0}", title);
                }
            }
        }

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

댓글을 달아 주세요