■ 클라이언트/서버 만들기 – 복합 데이터 계약을 사용하는 방법을 보여준다.
[Server]
▶ AuthorInfo.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 |
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
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 |
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
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")] 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 82 83 84 85 86 87 88 89 |
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
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 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
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 |
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 클래스는 서비스 참조 추가를 통해 생성한다.