■ WCF를 사용하는 방법을 보여준다.
※ Visual Studio 실행시 관리자 권한으로 실행해야 한다.
[서버 실행 프로그램]
[클라이언트 실행 프로그램]
[TestWCFLibrary 프로젝트]
▶ 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 TestWCFLibrary { /// <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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Text; namespace TestWCFLibrary { /// <summary> /// Hello World 서비스 /// </summary> public class HelloWorldService : IHelloWorld { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region Hello 말하기 - SayHello() /// <summary> /// Hello 말하기 /// </summary> /// <returns>Hello</returns> public string SayHello() { Console.WriteLine("클라이언트 요청 처리를 시작합니다."); StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < 50000; i++) { stringBuilder.Append("0123456789"); } Console.WriteLine("클라이언트 요청 처리를 종료합니다."); return stringBuilder.ToString(); } #endregion } } |
[TestWCFServer 프로젝트]
▶ 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 37 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="HelloWorldHttpBinding" messageEncoding="Mtom" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </basicHttpBinding> <netTcpBinding> <binding name="HelloWorldTcpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <reliableSession enabled="false" ordered="true"/> <security mode="None" /> </binding> </netTcpBinding> </bindings> <services> <service name="TestWCFLibrary.HelloWorldService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="HelloWorldHttpBinding" contract="TestWCFLibrary.IHelloWorld" /> <endpoint address="" binding="netTcpBinding" bindingConfiguration="HelloWorldTcpBinding" contract="TestWCFLibrary.IHelloWorld" /> <host> <baseAddresses> <add baseAddress="http://192.168.0.2/wcf/helloworldservice" /> <add baseAddress="net.tcp://192.168.0.2/wcf/helloworldservice" /> </baseAddresses> </host> </service> </services> </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 39 |
using System; using System.ServiceModel; using TestWCFLibrary; namespace TestWCFServer { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ServiceHost serviceHost = new ServiceHost(typeof(HelloWorldService)); serviceHost.Open(); Console.WriteLine("서비스 중단을 위해 아무 키나 누르시기 바랍니다..."); Console.ReadKey(true); serviceHost.Close(); } #endregion } } |
[TestWCFClient 프로젝트]
▶ 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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="HelloWorldHttpBinding" messageEncoding="Mtom" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </basicHttpBinding> <netTcpBinding> <binding name="HelloWorldTcpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> <reliableSession enabled="false" ordered="true"/> <security mode="None" /> </binding> </netTcpBinding> </bindings> <client> <endpoint name="HttpHelloWorld" contract="TestWCFLibrary.IHelloWorld" binding="basicHttpBinding" bindingConfiguration="HelloWorldHttpBinding" address="http://192.168.0.2/wcf/helloworldservice" /> <endpoint name="TcpHelloWorld" contract="TestWCFLibrary.IHelloWorld" binding="netTcpBinding" bindingConfiguration="HelloWorldTcpBinding" address="net.tcp://192.168.0.2/wcf/helloworldservice" /> </client> </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 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 93 94 95 96 97 98 99 100 101 |
using System; using System.Diagnostics; using System.ServiceModel; using TestWCFLibrary; namespace TestWCFClient { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Stopwatch stopWatch = new Stopwatch(); Console.WriteLine("HTTP 통신을 시작합니다."); stopWatch.Start(); CommunicateHTTP(); stopWatch.Stop(); Console.WriteLine("HTTP 통신을 종료합니다."); Console.WriteLine("HTTP 통신 시간 : " + stopWatch.Elapsed.ToString()); stopWatch.Reset(); Console.WriteLine("TCP 통신을 시작합니다."); stopWatch.Start(); CommunicateTCP(); stopWatch.Stop(); Console.WriteLine("TCP 통신을 종료합니다."); Console.WriteLine("TCP 통신 시간 : " + stopWatch.Elapsed.ToString()); Console.ReadKey(); } #endregion #region HTTP 통신하기 - CommunicateHTTP() /// <summary> /// HTTP 통신하기 /// </summary> private static void CommunicateHTTP() { ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("HttpHelloWorld"); IHelloWorld helloWorld = channelFactory.CreateChannel(); for(int i = 0; i < 100; i++) { string result = helloWorld.SayHello(); } (helloWorld as IDisposable).Dispose(); } #endregion #region TCP 통신하기 - CommunicateTCP() /// <summary> /// TCP 통신하기 /// </summary> private static void CommunicateTCP() { ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("TcpHelloWorld"); IHelloWorld helloWorld = channelFactory.CreateChannel(); for(int i = 0; i < 100; i++) { string result = helloWorld.SayHello(); } (helloWorld as IDisposable).Dispose(); } #endregion } } |