■ 클라이언트/서버 만들기 – MTOM 인코딩을 설정하는 방법을 보여준다.
[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 48 49 |
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") ); BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); basicHttpBinding.MessageEncoding = WSMessageEncoding.Mtom; serviceHost.AddServiceEndpoint(typeof(IHelloWorld), basicHttpBinding, 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 |
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() { Uri uri = new Uri("http://localhost/wcf/helloworldservice"); BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); basicHttpBinding.MessageEncoding = WSMessageEncoding.Mtom; ServiceEndpoint serviceEndpoint = new ServiceEndpoint ( ContractDescription.GetContract(typeof(IHelloWorld)), 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 } } |