■ 서비스 계약 구현 상속을 사용하는 방법을 보여준다.
▶ 서버 코드 (C#)
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
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 } } |
▶ 클라이언트 코드 (C#)
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
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 } } |