■ 클라이언트/서버 만들기 – Serializable 데이터 계약을 사용하는 방법을 보여준다.
[Server]
▶ BookInfo.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; using System.Runtime.Serialization; namespace TestProject { /// <summary> /// 도서 정보 /// </summary> [Serializable] public class BookInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// ISBN /// </summary> public string ISBN; [OptionalField] public string Author; [OptionalField] public int Price; [NonSerialized] public double MarginRate; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제목 /// </summary> [OptionalField] private string title; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get { return this.title; } set { this.title = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - BookInfo(title, isbn, author, price) /// <summary> /// 생성자 /// </summary> /// <param name="title">제목</param> /// <param name="isbn">ISBN</param> /// <param name="author">저자</param> /// <param name="price">가격</param> public BookInfo(string title, string isbn, string author, int price) { this.title = title; ISBN = isbn; Author = author; Price = price; MarginRate = 0.25; } #endregion } } |
▶ IBookStore.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System.ServiceModel; namespace TestProject { /// <summary> /// 상점 인터페이스 /// </summary> [ServiceContract(Namespace = "http://company.com/bookstore")] public interface IBookStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 도서 정보 구하기 - GetBookInfo(isbn) /// <summary> /// 도서 정보 구하기 /// </summary> /// <param name="isbn">ISBN</param> /// <returns>도서 정보</returns> [OperationContract] BookInfo GetBookInfo(string isbn); #endregion } } |
▶ BookStoreService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
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() { BookInfo bookInfo; bookInfo = new BookInfo("도서1", "000-001", "저자1", 40000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo("도서2", "000-002", "저자2", 42000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo("도서3", "000-003", "저자3", 53000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo("도서4", "000-004", "저자1", 37000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo("도서5", "000-005", "저자2", 18000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo("도서6", "000-006", "저자3", 20000); _bookInfoDictionary.Add(bookInfo.ISBN, bookInfo); bookInfo = new BookInfo(null , "000-007", null , 20000); _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)) { return _bookInfoDictionary[isbn]; } else { return null; } } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.ServiceModel; using System.ServiceModel.Description; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.WriteLine("Serializable 데이터 계약 테스트 서버를 시작합니다..."); 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 } } |
[Client]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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("Serializable 데이터 계약 테스트 클라이언트를 시작합니다..."); using(BookStoreClient bookStoreClient = new BookStoreClient()) { BookInfo info = bookStoreClient.GetBookInfo("000-005"); Console.WriteLine ( "도서 정보 :\n 제목={0}\n 저자={1}\n ISBN={2}\n 가격={3:#,##0}원", info.title, info.Author, info.ISBN, info.Price ); } } #endregion } } |
※ BookStoreClient 클래스는 서비스 참조 추가를 통해 생성한다.