■ 클라이언트/서버 만들기 – Behavior 오버라이딩을 사용하는 방법을 보여준다.
[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 참조를 추가한다.
▶ 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 38 39 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="HelloWorldBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="MtomBasicHttpBinding" messageEncoding="Mtom" /> </basicHttpBinding> </bindings> <services> <service name="HelloWorldLibrary.HelloWorldService" behaviorConfiguration="HelloWorldBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost/wcf/helloworldservice" /> <add baseAddress="net.tcp://localhost/wcf/helloworldservice" /> </baseAddresses> </host> <endpoint contract="HelloWorldLibrary.IHelloWorld" binding="basicHttpBinding" bindingConfiguration="MtomBasicHttpBinding" address="" /> <endpoint contract="HelloWorldLibrary.IHelloWorld" binding="netTcpBinding" address="" /> </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 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 |
using System; using System.ServiceModel; using System.ServiceModel.Description; 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)); ServiceMetadataBehavior serviceMetadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); if(serviceMetadataBehavior == null) { serviceMetadataBehavior = new ServiceMetadataBehavior(); serviceHost.Description.Behaviors.Add(serviceMetadataBehavior); } serviceMetadataBehavior.HttpGetEnabled = true; foreach(ServiceEndpoint serviceEndpoint in serviceHost.Description.Endpoints) { BasicHttpBinding basicHttpBinding = serviceEndpoint.Binding as BasicHttpBinding; if(basicHttpBinding != null) { basicHttpBinding.MessageEncoding = WSMessageEncoding.Mtom; } } serviceHost.Open(); Console.WriteLine("서비스 중단을 위해 아무 키나 누르세요..."); Console.ReadKey(true); serviceHost.Close(); } #endregion } } |
[HelloWorldConsole 프로젝트]
※ System.ServiceModel 및 HelloWorldLibrary 참조를 추가한다.
▶ 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> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="MtomBasicHttpBinding" messageEncoding="Mtom" /> </basicHttpBinding> </bindings> <client> <endpoint name="HttpHelloWorld" contract="HelloWorldLibrary.IHelloWorld" binding="basicHttpBinding" bindingConfiguration="MtomBasicHttpBinding" address="http://localhost/wcf/helloworldservice" /> <endpoint name="TcpHelloWorld" contract="HelloWorldLibrary.IHelloWorld" binding="netTcpBinding" address="net.tcp://localhost/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 |
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() { ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("HttpHelloWorld"); BasicHttpBinding basicHttpBinding = channelFactory.Endpoint.Binding as BasicHttpBinding; if(basicHttpBinding != null) { basicHttpBinding.MessageEncoding = WSMessageEncoding.Mtom; } 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() { ChannelFactory<IHelloWorld> channelFactory = new ChannelFactory<IHelloWorld>("TcpHelloWorld"); IHelloWorld helloWorld = channelFactory.CreateChannel(); string result = helloWorld.SayHello(); (helloWorld as IDisposable).Dispose(); Console.WriteLine(result); } #endregion } } |