Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

WCF에서 DataSet을 binary encoding으로 직렬화하는 방법

테스트용으로 다음과 같이 DataSet을 반환하는 WCF를 만들고,

public DataSet GetDataSet()
{
    DataSet ds = new DataSet();

    DataColumn nameCol = new DataColumn("Name", typeof(string));
    DataColumn birthCol = new DataColumn("Birth", typeof(DateTime));
    DataColumn emailCol = new DataColumn("Email", typeof(string));
    DataColumn familyCol = new DataColumn("Family", typeof(byte));

    DataTable table = new DataTable("MemberInfo");

    table.Columns.Add(nameCol);
    table.Columns.Add(birthCol);
    table.Columns.Add(emailCol);
    table.Columns.Add(familyCol);

    table.Rows.Add("Anderson", new DateTime(1950, 5, 20), "anderson@gmail.com", 2);
    table.Rows.Add("Jason", new DateTime(1967, 12, 3), "jason@gmail.com", 0);
    table.Rows.Add("Mark", new DateTime(1998, 3, 2), "mark@naver.com", 1);
    table.Rows.Add("Jennifer", new DateTime(1985, 5, 6), "jennifer@jennifersoft.com", 0);

    ds.Tables.Add(table);

    return ds;            
}

WCF 클라이언트로서 윈폼 앱을 만들어 다음과 같이 호출했습니다.

var ch = new ChannelFactory<ITestService>(
    "customHttpBinding_ITestService",
    new EndpointAddress("http://169.254.223.95:2524/TestService.svc"));

var svc = ch.CreateChannel();

DataSet ds = svc.GetDataSet();
System.Diagnostics.Trace.WriteLine(ds.GetXml());

이 사이의 통신을 가로채면 다음과 같습니다. (Body 영역은 보기 편하게 일부러 들여쓰기 편집을 했습니다.)

POST http://169.254.223.95:2524/TestService.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo+slpM8ZksBGuDd+qgAC8QYAAAAAj4yuJGaVKESSxzkDQNjf3pIuoAs6l5FJlArYEHPUGKcACQAA
SOAPAction: "http://tempuri.org/ITestService/GetDataSet"
Host: 169.254.223.95:2524
Content-Length: 135
Expect: 100-continue
Accept-Encoding: gzip, deflate

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <GetDataSet xmlns="http://tempuri.org/"/>
    </s:Body>
</s:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?ZDpcc2V0dGluZ3NcRGVza3RvcFxkYXRhc2V0X2FzX2JpblxXaW5kb3dzRm9ybXNBcHBsaWNhdGlvbjFcV2ViQXBwbGljYXRpb24xXFRlc3RTZXJ2aWNlLnN2Yw==?=
X-Powered-By: ASP.NET
Date: Tue, 27 Aug 2013 05:53:36 GMT
Content-Length: 1884

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body><
        GetDataSetResponse xmlns="http://tempuri.org/">
            <GetDataSetResult>
                <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="MemberInfo">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="Name" type="xs:string" minOccurs="0"/>
                                            <xs:element name="Birth" type="xs:dateTime" minOccurs="0"/>
                                            <xs:element name="Email" type="xs:string" minOccurs="0"/>
                                            <xs:element name="Family" type="xs:unsignedByte" minOccurs="0"/>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:schema>
                <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                    <NewDataSet xmlns="">
                        <MemberInfo diffgr:id="MemberInfo1" msdata:rowOrder="0" diffgr:hasChanges="inserted"><Name>Anderson</Name><Birth>1950-05-20T00:00:00+09:00</Birth><Email>anderson@gmail.com</Email><Family>2</Family></MemberInfo><MemberInfo diffgr:id="MemberInfo2" msdata:rowOrder="1" diffgr:hasChanges="inserted"><Name>Jason</Name><Birth>1967-12-03T00:00:00+09:00</Birth><Email>jason@gmail.com</Email><Family>0</Family></MemberInfo><MemberInfo diffgr:id="MemberInfo3" msdata:rowOrder="2" diffgr:hasChanges="inserted"><Name>Mark</Name><Birth>1998-03-02T00:00:00+09:00</Birth><Email>mark@naver.com</Email><Family>1</Family></MemberInfo><MemberInfo diffgr:id="MemberInfo4" msdata:rowOrder="3" diffgr:hasChanges="inserted"><Name>Jennifer</Name><Birth>1985-05-06T00:00:00+09:00</Birth><Email>jennifer@jennifersoft.com</Email><Family>0</Family>
                        </MemberInfo>
                    </NewDataSet>
                </diffgr:diffgram>
            </GetDataSetResult>
        </GetDataSetResponse>
    </s:Body>
</s:Envelope>


"Content-Length: 1884"라는 결과가 나오는군요. ^^




사실, 이 테스트를 해보는 것은 예전의 고객사 사이트에서 발생한 GZIP 인코딩 문제 때문입니다.

제니퍼 닷넷 적용 사례 (4) - GZIP 인코딩으로 인한 성능 하락
; https://www.sysnet.pe.kr/2/0/1484

System.IO.MemoryStream, ArraySegment 의 효율적인 사용법
; https://www.sysnet.pe.kr/2/0/1483

GZIP을 풀어놓자니 DataSet 크기가 여전히 마음에 걸립니다. 그나마 성능 손실 없이 데이터를 조금이라도 줄이는 방법은 바이너리 인코딩을 쓰는 것이 대안일 수 있는데요. 의외로 방법은 간단합니다. 우선, 서버 쪽 web.config에서 기존의 바인딩을 다음과 같이 binaryMessageEncoding + httpTransport로 적용해 주고,

<?xml version="1.0"?>

<configuration>
   <!-- ...[생략]... -->

  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="HttpBinaryBindingConfig">
          <binaryMessageEncoding>
            <readerQuotas maxStringContentLength="65536000" maxArrayLength="65536000"
                          maxBytesPerRead="4096000" />
          </binaryMessageEncoding>
          <httpTransport
            maxReceivedMessageSize="65536000"
            transferMode="Buffered"
            hostNameComparisonMode="StrongWildcard"
            authenticationScheme="Anonymous" />
        </binding>
      </customBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="TestService_Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="TestService_Behavior"
               name="WebApplication1.TestService">
        <endpoint address="" binding="customBinding" bindingConfiguration="HttpBinaryBindingConfig"
                  contract="WebApplication1.ITestService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

클라이언트 측의 app.config도 그에 맞게 변경시켜주면 됩니다.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="customHttpBinding_ITestService">
          <binaryMessageEncoding>
             <readerQuotas maxDepth="32" maxStringContentLength="65536000" maxArrayLength="65536000"
                maxBytesPerRead="4096000" maxNameTableCharCount="16384" />  
          </binaryMessageEncoding>
          <httpTransport
            maxReceivedMessageSize="65536000"
              transferMode="Buffered"
              hostNameComparisonMode="StrongWildcard"
            authenticationScheme="Anonymous" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://169.254.223.95:2524/TestService.svc" binding="customBinding"
          bindingConfiguration="customHttpBinding_ITestService" contract="ServiceReference1.ITestService"
          name="customHttpBinding_ITestService" />
    </client>
  </system.serviceModel>
</configuration>

그 외 변경되는 것은 아무것도 없습니다. 이렇게만 바꿔주고 통신을 시작하면 다음과 같이 클라이언트 <-> 서버 간에 통신이 바이너리로 이뤄지는 것을 확인할 수 있습니다.

POST http://169.254.223.95:2524/TestService.svc HTTP/1.1
Content-Type: application/soap+msbin1
Host: 169.254.223.95:2524
Content-Length: 329
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Close

V
s
aVD
???*http://tempuri.org/ITestService/GetDataSetD???i??p?L???jY?D,D*?@VsDebuggerCausalityDataAhttp://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink?<???_??*s?I??:?L{???????$f?(D??9@???.?
:??I?
?s??? ??D???*http://169.254.223.95:2524/TestService.svcV@
GetDataSethttp://tempuri.org/

HTTP/1.1 200 OK
Content-Length: 1512
Content-Type: application/soap+msbin1
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?ZDpcc2V0dGluZ3NcRGVza3RvcFxkYXRhc2V0X2FzX2JpblxXaW5kb3dzRm9ybXNBcHBsaWNhdGlvbjFcV2ViQXBwbGljYXRpb24xXFRlc3RTZXJ2aWNlLnN2Yw==?=
X-Powered-By: ASP.NET
Date: Tue, 27 Aug 2013 08:08:23 GMT
Connection: close

V
s
aVD
???2http://tempuri.org/ITestService/GetDataSetResponseD???i??p?L???jY?V@GetDataSetResponsehttp://tempuri.org/@GetDataSetResultAxsschemaid?
NewDataSet  xs http://www.w3.org/2001/XMLSchema?  msdata$urn:schemas-microsoft-com:xml-msdataAxselementname?
NewDataSetmsdata  IsDataSet?truemsdataUseCurrentLocale?trueAxs
complexTypeAxschoice minOccurs? maxOccurs?  unboundedAxselementname?
MemberInfoAxs
complexTypeAxssequenceAxselementname?Nametype? xs:string  minOccurs?Axselementname?Birthtype?
xs:dateTime    minOccurs?Axselementname?Emailtype? xs:string  minOccurs?Axselementname?Familytype?xs:unsignedByte   minOccurs?Adiffgrdiffgram diffgr)urn:schemas-microsoft-com:xml-diffgram-v1   msdata$urn:schemas-microsoft-com:xml-msdata@
NewDataSet?@
MemberInfodiffgrid?
MemberInfo1msdatarowOrder?diffgr
hasChanges?inserted@Name?Anderson@Birth?1950-05-20T00:00:00+09:00@Email?anderson@gmail.com@Family?2@
MemberInfodiffgrid?
MemberInfo2msdatarowOrder?diffgr
hasChanges?inserted@Name?Jason@Birth?1967-12-03T00:00:00+09:00@Email?jason@gmail.com@Family?@
MemberInfodiffgrid?
MemberInfo3msdatarowOrder?2diffgr
hasChanges?inserted@Name?Mark@Birth?1998-03-02T00:00:00+09:00@Email?mark@naver.com@Family?@
MemberInfodiffgrid?
MemberInfo4msdatarowOrder?3diffgr
hasChanges?inserted@Name?Jennifer@Birth?1985-05-06T00:00:00+09:00@Email?jennifer@jennifersoft.com@Family?

이전 textMessageEncoding + HTTP로 했었을 때는 1884바이트였던 것이 binaryMessageEncoding + HTTP로 바꾸니 1512바이트로 약 20% 정도의 데이터 길이가 감소했습니다. 거의 공짜로 감소된 것 치고는 괜찮은 비율입니다. 그런데, 좀 만족스럽지 않군요. ^^

직렬화된 데이터를 보니 diffgram을 유지하고 있는 것이 별로 마음에 들지 않습니다. WCF 메소드에서 딱 1라인만 추가를 해봤습니다. (사실, 서비스로부터 받아오는 데이터의 diffgram이 유지될 이유가 별로 없지요. ^^)

public DataSet GetDataSet()
{
    DataSet ds = new DataSet();

    // ...[생략]...

    ds.Tables.Add(table);

    ds.AcceptChanges();

    return ds;            
}

그런데, AcceptChanges로 인한 결과가 매우 흥미롭습니다. 이렇게 바뀌어진 WCF 메소드는 textMessageEncoding + HTTP로는 오히려 1998바이트로 늘어났고, binaryMessageEncoding + HTTP일 때는 1396바이트로 더 줄었습니다. 그럼 1884바이트 대비 1396바이트로 하면 36% 정도의 데이터가 감소했습니다.




이에 관해서 혹시 누군가 비교한 데이터가 있지 않을까 찾아봤는데 다음의 글이 나왔습니다.

WCF over HTTPS, Compression, and Binary Binding
; http://architects.dzone.com/articles/wcf-over-https-compression-and

dataset_as_bin_1.png

텍스트 인코딩인 경우 262,149 바이트였던 것을 바이너리로 바꾸면 155,565 바이트로 40%까지 감소되었다는 테스트 결과입니다. 물론, binaryMessageEncoding + GZIP 인코딩은 86%까지 감소되었다고 나오니 어느 것을 희생할 지는... 충분한 테스트를 거쳐서 판단하는 것이 좋겠습니다.

아니면 공사가 좀 크겠지만, DataSet보다 좀 더 경량화된 DTO(Data Transfer Object)의 사용이 답일 수도 있습니다.

풀리지 않는 숙제죠. ^^

DataSet 사용을 반대하는 의견
    .NET Architecture Case Study #1 - Part 1 
     ; http://blog.naver.com/saltynut/120168527432

DataSet 사용을 찬성하는 의견
    DataSet 이야기
    ; http://www.simpleisbest.net/post/2012/09/12/About-DataSet.aspx

(첨부된 파일은 위의 테스트를 진행한 프로젝트입니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 8/18/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13537정성태1/24/20242515닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성파일 다운로드1
13536정성태1/23/20242597닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/20242463닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
13534정성태1/21/20242256닷넷: 2207. C# - SQL Server DB를 bacpac으로 Export/Import파일 다운로드1
13533정성태1/18/20242472닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/20242360닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/20242260닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20242205닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/20242080닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/20242229Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/20242152오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/20242238닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/20242204오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/20242278오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/20242089오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/20242237닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/20242294닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/20242054오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....'
13519정성태1/10/20242126닷넷: 2198. C# - Reflection을 이용한 ClientWebSocket의 Ping 호출파일 다운로드1
13518정성태1/9/20242378닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/20242216스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/20242338닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
13515정성태1/6/20242610닷넷: 2195. async 메서드 내에서 C# 7의 discard 구문 활용 사례 [1]
13514정성태1/5/20242288개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/20242220닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/20242168개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...