■ 이진 포매터를 사용해 스트리밍 데이터를 구하는 방법을 보여준다.
[TestServer 프로젝트]
▶ IDataService.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 |
using System.IO; using System.ServiceModel; namespace TestServer { /// <summary> /// 데이터 서비스 인터페이스 /// </summary> [ServiceContract] public interface IDataService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 데이터 구하기 - GetData(dataCount, useGZipStream) /// <summary> /// 데이터 구하기 /// </summary> /// <param name="dataCount">데이터 카운트</param> /// <param name="useGZipStream">GZipStream 사용 여부</param> /// <returns>스트림</returns> [OperationContract] Stream GetData(int dataCount, bool useGZipStream); #endregion } } |
▶ DataService.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 102 103 104 105 |
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Formatters.Binary; namespace TestServer { /// <summary> /// 데이터 서비스 /// </summary> public class DataService : IDataService { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 구하기 - GetData(dataCount, useGZipStream) /// <summary> /// 데이터 구하기 /// </summary> /// <param name="dataCount">데이터 카운트</param> /// <param name="useGZipStream">GZipStream 사용 여부</param> /// <returns>스트림</returns> public Stream GetData(int dataCount, bool useGZipStream) { WriteMessage("START GetData : DataCount={0}", dataCount); #region 결과 리스트를 생성한다. List<string> resultlist = new List<string>(); for(int i = 0; i < dataCount; i++) { resultlist.Add(Guid.NewGuid().ToString().ToUpper()); } #endregion if(useGZipStream) { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true); binaryFormatter.Serialize(gZipStream, resultlist); gZipStream.Close(); memoryStream.Position = 0; WriteMessage("END GetData"); return memoryStream; } else { BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); binaryFormatter.Serialize(memoryStream, resultlist); memoryStream.Position = 0; WriteMessage("END GetData"); return memoryStream; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 메시지 쓰기 - WriteMessage(format, parameterArray) /// <summary> /// 메시지 쓰기 /// </summary> /// <param name="format">포맷 문자열</param> /// <param name="parameterArray">매개 변수 배열</param> private void WriteMessage(string format, params object[] parameterArray) { string message; if(parameterArray.Length == 0) { message = format; } else { message = string.Format(format, parameterArray); } Console.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message)); } #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(DataService))) { 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 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 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="basicHttp" sendTimeout="00:10:00" receiveTimeout="00:10:00" maxReceivedMessageSize="1262485504" transferMode="Streamed" /> </basicHttpBinding> <netTcpBinding> <binding name="netTcp" sendTimeout="00:10:00" receiveTimeout="00:10:00" maxReceivedMessageSize="1262485504" transferMode="Streamed" /> </netTcpBinding> </bindings> <services> <service behaviorConfiguration="mexBehavior" name="TestServer.DataService"> <endpoint address="FileService" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="TestServer.IDataService" /> <endpoint address="FileService" binding="netTcpBinding" bindingConfiguration="netTcp" contract="TestServer.IDataService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080" /> <add baseAddress="net.tcp://localhost:8090" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration> |
[TestClient 프로젝트]
▶ MainForm.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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Forms; using TestClient.DataService; namespace TestClient { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.getDataButton.Click += getDataButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 데이터 구하기 버튼 클릭시 처리하기 - downloadButton_Click(sender, e) /// <summary> /// 데이터 구하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void getDataButton_Click(object sender, EventArgs e) { int dataCount; try { dataCount = Convert.ToInt32(this.dataCountTextBox.Text); } catch { MessageBox.Show("유효한 데이터 카운트를 입력해 주시기 바랍니다."); return; } this.textBox.Text = string.Empty; WriteMessage("START GetData : DataCount={0}", dataCount); try { Enabled = false; DateTime startTime = DateTime.Now; using(DataServiceClient client = new DataServiceClient("NetTcpBinding_IDataService")) { List<string> list; if(this.useGZipStreamCheckBox.Checked) { Stream stream = client.GetData(dataCount, true); GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress); BinaryFormatter binaryFormatter = new BinaryFormatter(); list = (List<string>)binaryFormatter.Deserialize(gZipStream); gZipStream.Close(); stream.Close(); } else { Stream stream = client.GetData(dataCount, false); BinaryFormatter binaryFormatter = new BinaryFormatter(); list = (List<string>)binaryFormatter.Deserialize(stream); stream.Close(); } StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < list.Count; i++) { stringBuilder.AppendLine(list[i]); } this.textBox.Text = stringBuilder.ToString().Trim(); } DateTime endTime = DateTime.Now; WriteMessage("END GetData : Elapsed={0}", (endTime - startTime).ToString()); } finally { Enabled = true; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 메시지 지우기 - ClearMessage() /// <summary> /// 메시지 지우기 /// </summary> private void ClearMessage() { this.messageLabel.Text = string.Empty; } #endregion #region 메시지 쓰기 - WriteMessage(format, parameterArray) /// <summary> /// 메시지 쓰기 /// </summary> /// <param name="format">포맷 문자열</param> /// <param name="parameterArray">매개 변수 배열</param> private void WriteMessage(string format, params object[] parameterArray) { string message; if(parameterArray.Length == 0) { message = format; } else { message = string.Format(format, parameterArray); } messageLabel.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message); } #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 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IDataService" sendTimeout="00:10:00" receiveTimeout="00:10:00" maxReceivedMessageSize="1262485504" /> </basicHttpBinding> <netTcpBinding> <binding name="NetTcpBinding_IDataService" sendTimeout="00:10:00" receiveTimeout="00:10:00" maxReceivedMessageSize="1262485504" transferMode="Streamed" /> </netTcpBinding> </bindings> <client> <endpoint name="BasicHttpBinding_IDataService" address="http://localhost:8080/FileService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDataService" contract="DataService.IDataService" /> <endpoint name="NetTcpBinding_IDataService" address="net.tcp://localhost:8090/FileService" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IDataService" contract="DataService.IDataService" /> </client> </system.serviceModel> </configuration> |