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

■ WCF 메시지 보안 설정하기

------------------------------------------------------------------------------------------------------------------------

TestSolution.zip


[TestServer 프로젝트]

 

IHelloService.cs

 

 

using System.ServiceModel;

 

namespace TestServer

{

    /// <summary>

    /// 헬로우 서비스 인터페이스

    /// </summary>

    [ServiceContract]

    public interface IHelloService

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

 

        #region 메시지 구하기 - GetMessage(message)

 

        /// <summary>

        /// 메시지 구하기

        /// </summary>

        /// <param name="message">메시지</param>

        /// <returns>메시지</returns>

        [OperationContract]

        string GetMessage(string message);

 

        #endregion

    }

}

 

 

HelloService.cs

 

 

namespace TestServer

{

    /// <summary>

    /// 헬로우 서비스

    /// </summary>

    public class HelloService : IHelloService

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 메시지 구하기 - GetMessage(message)

 

        /// <summary>

        /// 메시지 구하기

        /// </summary>

        /// <param name="message">메시지</param>

        /// <returns>메시지</returns>

        public string GetMessage(string message)

        {

            return "Hello " + message;

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System.ServiceModel;

using System;

 

namespace TestServer

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            using(ServiceHost serviceHost = new ServiceHost(typeof(HelloService)))

            {

                serviceHost.Open();

 

                Console.WriteLine("서버가 시작되었습니다 : "DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

 

                Console.ReadKey(true);

            }

        }

 

        #endregion

    }

}

 

 

App.config

 

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <wsHttpBinding>

                <binding name="wsHttp">

                    <security mode="Message" />

                </binding>

            </wsHttpBinding>

        </bindings>

        <behaviors>

            <serviceBehaviors>

                <behavior name="mexBehavior">

                    <serviceMetadata httpGetEnabled="true" />

                    <serviceDebug includeExceptionDetailInFaults="true" />

                </behavior>

            </serviceBehaviors>

        </behaviors>

        <services>

            <service name="TestServer.HelloService" behaviorConfiguration="mexBehavior">

                <endpoint

                    address="HelloService"

                    binding="wsHttpBinding"

                    bindingConfiguration="wsHttp"

                    contract="TestServer.IHelloService" />

                <host>

                    <baseAddresses>

                        <add baseAddress="http://localhost:8080" />

                    </baseAddresses>

                </host>

            </service>

        </services>

    </system.serviceModel>

</configuration>

 

 

[TestClient 프로젝트]

 

MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

using TestClient.HelloService;

 

namespace TestClient

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.getMessageButton.Click += getMessageButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 메시지 구하기 버튼 클릭시 처리하기 - getMessageButton_Click(sender, e)

 

        /// <summary>

        /// 메시지 구하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void getMessageButton_Click(object sender, EventArgs e)

        {

            string sourceMessage = this.sourceMessageTextBox.Text.Trim();

 

            if(string.IsNullOrWhiteSpace(sourceMessage))

            {

                MessageBox.Show("소스 메시지를 입력해 주시기 바랍니다.");

 

                return;

            }

 

            this.targetMessageTextBox.Text = string.Empty;

 

            using(HelloServiceClient client = new HelloServiceClient("WSHttpBinding_IHelloService"))

            {

                string targetMessage = client.GetMessage(sourceMessage);

 

                this.targetMessageTextBox.Text = targetMessage;

            }

        }

 

        #endregion

    }

}

 

 

App.config

 

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

    <system.serviceModel>

        <bindings>

            <wsHttpBinding>

                <binding name="WSHttpBinding_IHelloService" />

            </wsHttpBinding>

        </bindings>

        <client>

            <endpoint name="WSHttpBinding_IHelloService"

                address="http://localhost:8080/HelloService"

                binding="wsHttpBinding"

                bindingConfiguration="WSHttpBinding_IHelloService"

                contract="HelloService.IHelloService">

            </endpoint>

        </client>

    </system.serviceModel>

</configuration>

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요