첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 클라이언트/서버 만들기 - 복합 데이터 계약 사용하기

------------------------------------------------------------------------------------------------------------------------

AuthorInfo.cs (서버)

 

 

using System.Runtime.Serialization;

 

namespace TestProject

{

    /// <summary>

    /// 저자 정보

    /// </summary>

    [DataContract(Namespace = "http://company.com/bookstore/data")]

    public class AuthorInfo

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 성명

        /// </summary>

        [DataMember]

        public string FullName;

 

        /// <summary>

        /// 주소

        /// </summary>

        [DataMember]

        public string Address;

 

        /// <summary>

        /// E 메일

        /// </summary>

        [DataMember]

        public string EMail;

 

        #endregion

    }

}

 

 

BookInfo.cs (서버)

 

 

using System.Runtime.Serialization;

 

namespace TestProject

{

    /// <summary>

    /// 도서 정보

    /// </summary>

    [DataContract(Namespace = "http://company.com/bookstore/data")]

    public class BookInfo

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 제목

        /// </summary>

        [DataMember]

        public string Title;

 

        /// <summary>

        /// 저자 정보

        /// </summary>

        [DataMember]

        public AuthorInfo AuthorInfo;

 

        /// <summary>

        /// ISBN

        /// </summary>

        [DataMember]

        public string ISBN;

 

        #endregion

    }

}

 

 

IBookStore.cs (서버)

 

 

using System.ServiceModel;

 

namespace TestProject

{

    /// <summary>

    /// 상점 인터페이스

    /// </summary>

    [ServiceContract(Namespace = "http://company.com/bookstore")]

    interface IBookStore

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

 

        #region 도서 정보 구하기 - GetBookInfo(isbn)

 

        /// <summary>

        /// 도서 정보 구하기

        /// </summary>

        /// <param name="isbn">ISBN</param>

        /// <returns>도서 정보</returns>

        [OperationContract]

        BookInfo GetBookInfo(string isbn);

 

        #endregion

    }

}

 

 

BookStoreService.cs (서버)

 

 

using System.Collections.Generic;

using System.ServiceModel;

 

namespace TestProject

{

    /// <summary>

    /// 상점 서비스

    /// </summary>

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]

    public class BookStoreService : IBookStore

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region Field

 

        /// <summary>

        /// 도서 정보 딕셔너리

        /// </summary>

        private static Dictionary<string, BookInfo> _bookInfoDictionary = new Dictionary<string, BookInfo>();

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Static

 

        #region 생성자 - BookStoreService()

 

        /// <summary>

        /// 생성자

        /// </summary>

        static BookStoreService()

        {

            AuthorInfo authorInfo;

            BookInfo   bookInfo;

 

            authorInfo = new AuthorInfo();

 

            authorInfo.FullName = "저자 1";

            authorInfo.Address  = "서울시 종로구";

            authorInfo.EMail    = "writer1@company.com";

 

            bookInfo = new BookInfo();

 

            bookInfo.AuthorInfo = authorInfo;

            bookInfo.Title      = "도서 1";

            bookInfo.ISBN       = "000-001";

 

            _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo);

 

            bookInfo = new BookInfo();

 

            bookInfo.AuthorInfo = authorInfo;

            bookInfo.Title      = "도서 2";

            bookInfo.ISBN       = "000-002";

 

            _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 도서 정보 구하기 - GetBookInfo(isbn)

 

        /// <summary>

        /// 도서 정보 구하기

        /// </summary>

        /// <param name="isbn">ISBN</param>

        /// <returns>도서 정보</returns>

        public BookInfo GetBookInfo(string isbn)

        {

            if(_bookInfoDictionary.ContainsKey(isbn) == true)

            {

                return _bookInfoDictionary[isbn];

            }

            else

            {

                return null;

            }

        }

 

        #endregion

    }

}

 

 

Program.cs (서버)

 

 

using System;

using System.ServiceModel;

using System.ServiceModel.Description;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    public class Program

    {

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.WriteLine("복합 데이터 계약 테스트 서버를 시작합니다...");

 

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

 

            serviceHost.AddServiceEndpoint(typeof(IBookStore), 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

    }

}

 

 

Program.cs (클라이언트)

 

 

using System;

 

using TestProject.BookStore;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    public class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.WriteLine("복합 데이터 계약 테스트 클라이언트를 시작합니다...");

 

            using(BookStoreClient bookStoreClient = new BookStoreClient())

            {

                BookInfo bookInfo1 = bookStoreClient.GetBookInfo("000-001");

 

                Console.WriteLine

                (

                    "도서 정보 :\n            제목={0}\n            ISBN={1}",

                    bookInfo1.Title,

                    bookInfo1.ISBN

                );

 

                Console.WriteLine

                (

                    "            저자={0}\n            E 메일={1}\n            주소={2}",

                    bookInfo1.AuthorInfo.FullName,

                    bookInfo1.AuthorInfo.EMail,

                    bookInfo1.AuthorInfo.Address

                );

 

                BookInfo bookInfo2 = bookStoreClient.GetBookInfo("000-002");

 

                Console.WriteLine

                (

                    "도서 정보 :\n            제목={0}\n            ISBN={1}",

                    bookInfo2.Title,

                    bookInfo2.ISBN

                );

 

                Console.WriteLine

                (

                    "            저자={0}\n            E 메일={1}\n            주소={2}",

                    bookInfo2.AuthorInfo.FullName,

                    bookInfo2.AuthorInfo.EMail,

                    bookInfo2.AuthorInfo.Address

                );

 

                Console.WriteLine

                (

                    "참조 동일 테스트 : bookInfo1.AuthorInfo == bookInfo2.AuthorInfo ? {0}",

                    bookInfo1.AuthorInfo == bookInfo2.AuthorInfo

                );

            }

        }

 

        #endregion

    }

}

 

※ BookStoreClient 클래스는 서비스 참조 추가를 통해 생성한다.

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요