■ 멀티 서비스 계약을 사용하는 방법을 보여준다.
[TestService 프로젝트]
▶ IPublicService.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 |
using System.ServiceModel; namespace TestService { /// <summary> /// 공개 서비스 인터페이스 /// </summary> [ServiceContract] public interface IPublicService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 공개 정보 구하기 - GetPublicInformation(name) /// <summary> /// 공개 정보 구하기 /// </summary> /// <returns>공개 정보</returns> [OperationContract] string GetPublicInformation(); #endregion } } |
▶ IConfidentialService.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 |
using System.ServiceModel; namespace TestService { /// <summary> /// 비밀 서비스 인터페이스 /// </summary> [ServiceContract] public interface IConfidentialService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 비밀 정보 구하기 - GetCofidentialInformation() /// <summary> /// 비밀 정보 구하기 /// </summary> /// <returns>비밀 정보</returns> [OperationContract] string GetCofidentialInformation(); #endregion } } |
▶ DataService.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 |
namespace TestService { /// <summary> /// 데이터 서비스 /// </summary> public class DataService : IPublicService, IConfidentialService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 공개 정보 구하기 - GetPublicInformation() /// <summary> /// 공개 정보 구하기 /// </summary> /// <returns>공개 정보</returns> public string GetPublicInformation() { return "이것은 공개 정보이며 방화벽 외부의 모든 일반 대중에게 HTTP를 통해 사용할 수 있습니다."; } #endregion #region 비밀 정보 구하기 - GetCofidentialInformation() /// <summary> /// 비밀 정보 구하기 /// </summary> /// <returns>비밀 정보</returns> public string GetCofidentialInformation() { return "이것은 기밀 정보이며 방화벽 내의 TCP를 통해서만 사용 가능합니다."; } #endregion } } |
[TestServer 프로젝트]
▶ 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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="TestService.DataService" behaviorConfiguration="mexBehaviour"> <endpoint address="DataService" binding="basicHttpBinding" contract="TestService.IPublicService" /> <endpoint address="DataService" binding="netTcpBinding" contract="TestService.IConfidentialService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/" /> <add baseAddress="net.tcp://localhost:8090/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehaviour"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </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 |
using System; using System.ServiceModel; using TestService; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(ServiceHost host = new ServiceHost(typeof(DataService))) { host.Open(); Console.WriteLine("호스트가 시작되었습니다 : " + DateTime.Now.ToString()); Console.ReadKey(true); } } #endregion } } |
[TestClient 프로젝트]
▶ 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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IPublicService" /> </basicHttpBinding> <netTcpBinding> <binding name="NetTcpBinding_IConfidentialService" /> </netTcpBinding> </bindings> <client> <endpoint address="http://localhost:8080/DataService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPublicService" contract="DataService.IPublicService" name="BasicHttpBinding_IPublicService" /> <endpoint address="net.tcp://localhost:8090/DataService" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IConfidentialService" contract="DataService.IConfidentialService" name="NetTcpBinding_IConfidentialService"> </endpoint> </client> </system.serviceModel> </configuration> |
▶ 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 |
using System; using System.Windows.Forms; using TestClient.DataService; namespace TestClient { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.getInformationButton.Click += getInformationButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 메시지 구하기 버튼 클릭시 처리하기 - getMessageButton_Click(sender, e) /// <summary> /// 메시지 구하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void getInformationButton_Click(object sender, EventArgs e) { bool usePublic = this.publicRadioButton.Checked; string endpointConfigurationName = usePublic ? "BasicHttpBinding_IPublicService" : "NetTcpBinding_IConfidentialService"; if(usePublic) { using(PublicServiceClient client = new PublicServiceClient("BasicHttpBinding_IPublicService")) { MessageBox.Show(client.GetPublicInformation()); } } else { using(ConfidentialServiceClient client = new ConfidentialServiceClient("NetTcpBinding_IConfidentialService")) { MessageBox.Show(client.GetCofidentialInformation()); } } } #endregion } } |