■ 클라이언트/서버 만들기 – 데이터 계약을 사용하는 방법을 보여준다.
[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 93 94 95 96 97 |
using System.Runtime.Serialization; namespace TestProject { /// <summary> /// 도서 정보 /// </summary> [DataContract(Namespace = "http://company.com/bookstore/data")] public class BookInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// ISBN /// </summary> [DataMember(IsRequired = true, Order = 2)] public readonly string ISBN; /// <summary> /// 저자 /// </summary> [DataMember(Order = 3)] public string Author; [DataMember(Order = 4)] public int Price; /// <summary> /// 마진율 /// </summary> public double MarginRate; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제목 /// </summary> private string title; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제목 - Title /// <summary> /// 제목 /// </summary> [DataMember(Order = 1)] 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">IBSN</param> /// <param name="author">저자</param> /// <param name="price">가격</param> public BookInfo(string title, string isbn, string author, int price) { this.title = title; this.ISBN = isbn; this.Author = author; this.Price = price; this.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 |
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() { _bookInfoDictionary.Add("000-001", new BookInfo("도서01", "000-001", "저자01", 40000)); _bookInfoDictionary.Add("000-002", new BookInfo("도서02", "000-002", "저자02", 42000)); _bookInfoDictionary.Add("000-003", new BookInfo("도서03", "000-003", "저자03", 53000)); _bookInfoDictionary.Add("000-004", new BookInfo("도서04", "000-004", "저자04", 37000)); _bookInfoDictionary.Add("000-005", new BookInfo("도서05", "000-005", "저자05", 18000)); _bookInfoDictionary.Add("000-006", new BookInfo("도서06", "000-006", "저자06", 20000)); _bookInfoDictionary.Add("000-007", new BookInfo(null , "000-007", null , 20000)); } #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
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 |
using System; using System.ServiceModel; using System.ServiceModel.Description; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Porgram { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> 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 } } |
[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 45 46 47 48 49 50 51 52 53 54 55 |
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 bookInfo = bookStoreClient.GetBookInfo("000-005"); Console.WriteLine ( "도서 정보 :\n 제목={0}\n 저자={1}\n ISBN={2}\n 가격={3:#,##0}원", bookInfo.Title, bookInfo.Author, bookInfo.ISBN, bookInfo.Price ); bookInfo = bookStoreClient.GetBookInfo("000-007"); Console.WriteLine ( "도서 정보 :\n 제목={0}\n 저자={1}\n ISBN={2}\n 가격={3:#,##0}원", bookInfo.Title, bookInfo.Author, bookInfo.ISBN, bookInfo.Price ); } } #endregion } } |
※ BookStoreClient 클래스는 서비스 참조 추가를 통해 생성한다.