■ 클라이언트/서버 만들기 – 서비스 종점을 추가하는 방법을 보여준다.
[HelloWorldLibrary 프로젝트]
※ System.ServiceModel 참조를 추가한다.
▶ IHelloWorld.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 HelloWorldLibrary { /// <summary> /// Hello World 인터페이스 /// </summary> [ServiceContract] public interface IHelloWorld { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region Hello 말하기 - SayHello() /// <summary> /// Hello 말하기 /// </summary> /// <returns>Hello</returns> [OperationContract] string SayHello(); #endregion } } |
▶ HelloWorldService.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 |
namespace HelloWorldLibrary { /// <summary> /// Hello World 서비스 /// </summary> public class HelloWorldService : IHelloWorld { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region Hello 말하기 - SayHello() /// <summary> /// Hello 말하기 /// </summary> /// <returns>Hello</returns> public string SayHello() { return "Hello World!"; } #endregion } } |
[HelloWorldServer 프로젝트]
※ System.ServiceModel 및 HelloWorldLibrary 참조를 추가한다.
▶ 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 |
using System; using System.ServiceModel; using HelloWorldLibrary; namespace HelloWorldServer { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ServiceHost serviceHost = new ServiceHost ( typeof(HelloWorldService), new Uri("http://localhost/wcf/helloworldservice"), new Uri("net.tcp://localhost/wcf/helloworldservice") ); serviceHost.AddServiceEndpoint(typeof(IHelloWorld), new BasicHttpBinding(), string.Empty); serviceHost.AddServiceEndpoint(typeof(IHelloWorld), new NetTcpBinding() , string.Empty); serviceHost.Open(); Console.WriteLine("서비스 중단을 위해 아무 키나 누르세요..."); Console.ReadKey(true); serviceHost.Close(); } #endregion } } |
[HelloWorldConsole 프로젝트]
※ System.ServiceModel 및 HelloWorldLibrary 참조를 추가한다.
▶ 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 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 87 88 89 90 91 92 |
using System; using System.ServiceModel; using System.ServiceModel.Description; using HelloWorldLibrary; namespace HelloWorldConsole { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { CommunicateHTTP(); CommunicateTCP(); } #endregion #region HTTP 통신하기 - CommunicateHTTP() /// <summary> /// HTTP 통신하기 /// </summary> private static void CommunicateHTTP() { Uri uri = new Uri("http://localhost/wcf/helloworldservice"); ServiceEndpoint serviceEndpoint = new ServiceEndpoint ( ContractDescription.GetContract(typeof(IHelloWorld)), new BasicHttpBinding(), new EndpointAddress(uri) ); ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>(serviceEndpoint); IHelloWorld helloWorld = channelFactory.CreateChannel(); string result = helloWorld.SayHello(); (helloWorld as IDisposable).Dispose(); Console.WriteLine(result); } #endregion #region TCP 통신하기 - CommunicateTCP() /// <summary> /// TCP 통신하기 /// </summary> private static void CommunicateTCP() { Uri uri = new Uri("net.tcp://localhost/wcf/helloworldservice"); ServiceEndpoint serviceEndpoint = new ServiceEndpoint ( ContractDescription.GetContract(typeof(IHelloWorld)), new NetTcpBinding(), new EndpointAddress(uri) ); ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>(serviceEndpoint); IHelloWorld helloWorld = channelFactory.CreateChannel(); string result = helloWorld.SayHello(); (helloWorld as IDisposable).Dispose(); Console.WriteLine(result); } #endregion } } |