■ 클라이언트/서버 만들기 – WSHttpBinding을 사용하는 방법을 보여준다.
[Server]
▶ 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.ServiceModel; namespace TestProject { /// <summary> /// 상점 인터페이스 /// </summary> [ServiceContract(Namespace = "http://company.com/bookstore")] interface IBookStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 주문하기 1 - Order1(userID, isbn, amount) /// <summary> /// 주문하기 1 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> [OperationContract(Action = "http://company.com/bookstore/OrderAction1", ReplyAction = "http://company.com/bookstore/OrderResponseAction1")] int Order1(string userID, string isbn, int amount); #endregion #region 주문하기 2 - Order2(userID, isbn, amount) /// <summary> /// 주문하기 2 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> [OperationContract(Action = "http://company.com/bookstore/OrderAction2", ReplyAction = "http://company.com/bookstore/OrderResponseAction2")] int Order2(string userID, string isbn, int amount); #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 90 91 92 93 94 95 96 97 98 |
using System; using System.ServiceModel; namespace TestProject { /// <summary> /// 상점 서비스 /// </summary> [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class BookStoreService : IBookStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 주문 ID /// </summary> private static int _orderID = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 주문하기 1 - Order1(userID, isbn, amount) /// <summary> /// 주문하기 1 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> public int Order1(string userID, string isbn, int amount) { int price = GetUnitPrice(isbn) * amount; _orderID += 1; Console.WriteLine ( "도서 주문 1 : 주문 ID={0}\n 사용자 ID={1}, ISBN={2}, 수량={3}, 가격={4}", _orderID, userID, isbn, amount, price ); return _orderID; } #endregion #region 주문하기 2 - Order2(userID, isbn, amount) /// <summary> /// 주문하기 2 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> public int Order2(string userID, string isbn, int amount) { int price = GetUnitPrice(isbn) * amount; _orderID += 1; Console.WriteLine ( "도서 주문 2 : 주문 ID={0}\n 사용자 ID={1}, ISBN={2}, 수량={3}, 가격={4}", _orderID, userID, isbn, amount, price ); return _orderID; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 단가 구하기 - GetUnitPrice(isbn) /// <summary> /// 단가 구하기 /// </summary> /// <param name="isbn">ISBN</param> /// <returns>단가</returns> private int GetUnitPrice(string isbn) { return 35000; } #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 |
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("WSHttpBinding 클래스 테스트 서버를 시작합니다..."); ServiceHost serviceHost = new ServiceHost ( typeof(BookStoreService), new Uri("http://localhost/wcf/example/bookstoreservice") ); WSHttpBinding wsHttpBinding = new WSHttpBinding(); wsHttpBinding.Security.Mode = SecurityMode.None; serviceHost.AddServiceEndpoint(typeof(IBookStore), wsHttpBinding, ""); ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior(); serviceMetadataBehavior.HttpGetEnabled = true; serviceHost.Description.Behaviors.Add(serviceMetadataBehavior); serviceHost.Open(); Console.WriteLine("서비스 중단을 위해 아무 키나 누르세요..."); Console.ReadKey(true); serviceHost.Close(); } #endregion } } |
[Client]
▶ app.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IBookStore"> <security mode="None" /> </binding> </wsHttpBinding> </bindings> <client> <endpoint name="WSHttpBinding_IBookStore" address="http://localhost/wcf/example/bookstoreservice" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IBookStore" contract="BookStoreService.IBookStore" /> </client> </system.serviceModel> </configuration> |
▶ 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 TestProject.BookStoreService; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.WriteLine("WSHttpBinding 클래스 테스트 클라이언트를 시작합니다..."); int orderID; using(BookStoreClient bookStoreClient = new BookStoreClient()) { orderID = bookStoreClient.Order1("사용자1", "999-9-999-99999-9", 2); Console.WriteLine("주문하였습니다 : 주문 ID = {0}", orderID); orderID =bookStoreClient.Order2("사용자2", "000-0-000-00000-0", 1); Console.WriteLine("주문하였습니다 : 주문 ID = {0}", orderID); } } #endregion } } |
※ BookStoreClient 클래스는 서비스 참조 추가를 통해 생성한다.