첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

▶ IBookStore.cs (서버)

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 (서버)

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 (서버)

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
    }
}

 

▶ app.config (클라이언트)

<?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 (클라이언트)

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 클래스는 서비스 참조 추가를 통해 생성한다.

728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요