■ ProtectionLevel 속성을 사용해 메시지 보호를 설정하는 방법을 보여준다.
[TestServer 프로젝트]
▶ IHelloService.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 |
using System.Net.Security; using System.ServiceModel; namespace TestServer { /// <summary> /// 헬로우 서비스 인터페이스 /// </summary> [ServiceContract] public interface IHelloService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 메시지 구하기 - GetMessage() /// <summary> /// 메시지 구하기 /// </summary> /// <returns>메시지</returns> [OperationContract(ProtectionLevel = ProtectionLevel.None)] string GetMessage(); #endregion #region 서명 메시지 구하기 - GetMessageSigned() /// <summary> /// 서명 메시지 구하기 /// </summary> /// <returns>메시지</returns> [OperationContract(ProtectionLevel = ProtectionLevel.Sign)] string GetMessageSigned(); #endregion #region 서명/암호화 메시지 구하기 - GetMessageSignedAndEncrypted() /// <summary> /// 서명/암호화 메시지 구하기 /// </summary> /// <returns>메시지</returns> [OperationContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] string GetMessageSignedAndEncrypted(); #endregion } } |
▶ HelloService.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 |
namespace TestServer { /// <summary> /// 헬로우 서비스 /// </summary> public class HelloService : IHelloService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 구하기 - GetMessage() /// <summary> /// 메시지 구하기 /// </summary> /// <returns>메시지</returns> public string GetMessage() { return "Message"; } #endregion #region 서명 메시지 구하기 - GetMessageSigned() /// <summary> /// 서명 메시지 구하기 /// </summary> /// <returns>메시지</returns> public string GetMessageSigned() { return "Signed message"; } #endregion #region 서명/암호화 메시지 구하기 - GetMessageSignedAndEncrypted() /// <summary> /// 서명/암호화 메시지 구하기 /// </summary> /// <returns>메시지</returns> public string GetMessageSignedAndEncrypted() { return "Signed and encrypted message"; } #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 |
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
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 |
<?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
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 |
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.testButton1.Click += testButton1_Click; this.testButton2.Click += testButton2_Click; this.testButton3.Click += testButton3_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 메시지 구하기 버튼 클릭시 처리하기 - testButton1_Click(sender, e) /// <summary> /// 메시지 구하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton1_Click(object sender, EventArgs e) { using(HelloServiceClient client = new HelloServiceClient("WSHttpBinding_IHelloService")) { MessageBox.Show(client.GetMessage()); } } #endregion #region 서명 메시지 구하기 버튼 클릭시 처리하기 - testButton2_Click(sender, e) /// <summary> /// 서명 메시지 구하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton2_Click(object sender, EventArgs e) { using(HelloServiceClient client = new HelloServiceClient("WSHttpBinding_IHelloService")) { MessageBox.Show(client.GetMessageSigned()); } } #endregion #region 서명/암호화 메시지 구하기 버튼 클릭시 처리하기 - testButton3_Click(sender, e) /// <summary> /// 서명/암호화 메시지 구하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton3_Click(object sender, EventArgs e) { using(HelloServiceClient client = new HelloServiceClient("WSHttpBinding_IHelloService")) { MessageBox.Show(client.GetMessageSignedAndEncrypted()); } } #endregion } } |
▶ App.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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" /> </client> </system.serviceModel> </configuration> |