■ PerSession 인스턴스 컨텍스트 모드를 사용하는 방법을 보여준다.
[TestServer 프로젝트]
▶ ISimpleService.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 TestServer { /// <summary> /// 단순 서비스 /// </summary> [ServiceContract] public interface ISimpleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 번호 증가하기 - IncreaseNumber() /// <summary> /// 번호 증가하기 /// </summary> /// <returns>번호</returns> [OperationContract] int IncreaseNumber(); #endregion } } |
▶ SimpleService.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 |
using System.ServiceModel; namespace TestServer { /// <summary> /// 단순 서비스 /// </summary> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class SimpleService : ISimpleService { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 번호 /// </summary> private int number; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 번호 증가하기 - IncreaseNumber() /// <summary> /// 번호 증가하기 /// </summary> /// <returns>번호</returns> public int IncreaseNumber() { this.number = this.number + 1; return this.number; } #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 |
using System; using System.ServiceModel; namespace TestServer { /// <summary> /// 프로그램 /// </summary> class Program { ////////////////////////////////////////////////////////////////////////////////////////// Method //////////////////////////////////////////////////////////////////////////////// Static ////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { using(ServiceHost serviceHost = new ServiceHost(typeof(SimpleService))) { 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 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="TestServer.SimpleService" behaviorConfiguration="mexBehavior"> <endpoint address="SimpleService" binding="netTcpBinding" contract="TestServer.ISimpleService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> <add baseAddress="net.tcp://localhost:8090" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </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 36 37 38 39 40 41 42 43 44 45 |
using System; using TestClient.SimpleService; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { int number; using(SimpleServiceClient client = new SimpleServiceClient("NetTcpBinding_ISimpleService")) { number = client.IncreaseNumber(); Console.WriteLine("첫번째 호출 후 번호 = " + number); number = client.IncreaseNumber(); Console.WriteLine("두번째 호출 후 번호 = " + number); number = client.IncreaseNumber(); Console.WriteLine("세번째 호출 후 번호 = " + number); } } #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 |
<?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_ISimpleService" /> </netTcpBinding> </bindings> <client> <endpoint name="NetTcpBinding_ISimpleService" address="net.tcp://localhost:8090/SimpleService" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ISimpleService" contract="SimpleService.ISimpleService" /> </client> </system.serviceModel> </configuration> |