■ 코드로 서비스 종점을 동적으로 구성하는 방법을 보여준다.
[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 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 헬로우 서비스 /// </summary> [ServiceContract] public interface IHelloService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 메시지 구하기 - GetMessage(name) /// <summary> /// 메시지 구하기 /// </summary> /// <param name="name">성명</param> /// <returns>메시지</returns> [OperationContract] string GetMessage(string name); #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 |
namespace TestServer { /// <summary> /// 헬로우 서비스 /// </summary> public class HelloService : IHelloService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 구하기 - GetMessage(name) /// <summary> /// 메시지 구하기 /// </summary> /// <param name="name">성명</param> /// <returns>메시지</returns> public string GetMessage(string name) { return "Hello " + name; } #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 |
using System; using System.ServiceModel; using System.ServiceModel.Description; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Uri[] uriArray = new Uri[] { new Uri("http://localhost:8080/"), new Uri("net.tcp://localhost:8090/") }; using(ServiceHost serviceHost = new ServiceHost(typeof(HelloService), uriArray)) { serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); serviceHost.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "HelloService"); serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); 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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration> |
[TestClient 프로젝트]
▶ 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 |
using System; using TestClient.HelloService; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(HelloServiceClient client = new HelloServiceClient("NetTcpBinding_IHelloService")) { string message = client.GetMessage("홍길동"); Console.WriteLine(message); } } #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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_IHelloService" /> </netTcpBinding> </bindings> <client> <endpoint name="NetTcpBinding_IHelloService" address="net.tcp://localhost:8090/HelloService" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHelloService" contract="HelloService.IHelloService"> </endpoint> </client> </system.serviceModel> </configuration> |