Click here to Skip to main content
6,822,613 members and growing! (19,274 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » General     Beginner License: The Code Project Open License (CPOL)

Array instead of List in WCF

By martyn_mcfly

This article describes the differences between using arrays and generic lists in WCF.
XML, C#2.0, C#3.0, C#4.0, .NET (.NET3.0, .NET3.5, .NET4.0), WCF, Dev
Revision:4 (See All)
Posted:28 Nov 2009
Updated:28 Nov 2009
Views:2,446
Bookmarked:3 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.67 Rating: 4.71 out of 5

1

2
1 vote, 16.7%
3

4
5 votes, 83.3%
5

Introduction

Recently, I was developing a function which included transferring an image in a message package. So, following a habit, I made up a property in the message class like this: public List<byte> Image { get; set; }. This Image is sent to the client through a duplex channel. Can you imagine how I was surprised when I saw the percentage of processor consumption for this service process? It was ~15-20% loaded with an image size of ~400 KB. I then started to think about a better solution.

Background

Here is the structure of duplex channels in WCF:

[ServiceContract(CallbackContract = typeof(IServiceCallback), 
 SessionMode = SessionMode.Required)]

public interface IService
{
    [OperationContract(IsOneWay = true)]
    void SendData(List<byte> array);
}

public interface IServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void RecieveData(List<byte> array); //this is duplex receiver of image 
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, 
 ConcurrencyMode = ConcurrencyMode.Multiple, 
 MaxItemsInObjectGraph = Int32.MaxValue)]
public class Service : IService
{
    public void SendData(List<byte> array)
    {
        IServiceCallback callback = 
          OperationContext.Current.GetCallbackChannel<IServiceCallback>();
        callback.RecieveData(array);
        //Receiving of message back to user. 
        //It is simple exaple is only to show high duplex performance. 
    }
}

Using the Code

A search for a solution to the problem took a considerable quantity of my time, and then, having replaced the type List<byte> with an array of bytes, I received considerably better results: practically ~1-2% loading. Why is there such a big difference between what would seem like identical types of data? The difference is in how the SOAP message is generated. For List<byte>:

...
<s:Body u:Id="_0"
   xmlns:u="http://docs.oasis-open.org/wss/2004/01/
            oasis-200401-wss-wssecurity-utility-1.0.xsd">
<SendData xmlns="http://tempuri.org/">
<array
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
<a:unsignedByte>0</a:unsignedByte>
...
</array>
</SendData>
</s:Body>

...

And for the array of bytes:

...
<s:Body u:Id="_0"
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<SendData xmlns="http://tempuri.org/">
<array>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</array>
</SendData>
</s:Body>

...

Points of Interest

In general, the difference in size of these two SOAP messages differed approximately 10 times, and the loading of the processor fell from ~15-20 % to ~1-3 % at the same size of package. I was very surprised, because nowhere have I found official information on these differences.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

martyn_mcfly


Member
I have started programming in 13 years on Delphi.Then it was Pascal,Php,C++,some JavaScript. Now it at all C# and .NET Framework, such tehnologies as WCF, WPF, ADO.NET, ASP.NET, LINQ, ASP.NET MVC Framework, Entity Framework, ASP.NET Ajax and another.
Occupation: Software Developer
Company: Aspro Systems Inc.
Location: Russian Federation Russian Federation

Other popular Windows Communication Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralYou are using the wrong construction PinmemberRamon Smits5:19 30 Nov '09  
GeneralRe: You are using the wrong construction Pinmembermartyn_mcfly20:33 30 Nov '09  
GeneralRe: You are using the wrong construction PinmemberRamon Smits23:03 30 Nov '09  
GeneralRe: You are using the wrong construction Pinmembermartyn_mcfly23:19 30 Nov '09  
GeneralThank you for sharing ! PinmemberMasTooool8:37 28 Nov '09  
GeneralRe: Thank you for sharing ! Pinmembermartyn_mcfly10:27 28 Nov '09  
GeneralThis is not an article PinmvpJohn Simmons / outlaw programmer3:51 28 Nov '09  
GeneralRe: This is not an article Pinmembersam.hill6:23 28 Nov '09  
GeneralRe: This is not an article Pinmembermartyn_mcfly7:55 28 Nov '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2009
Editor: Smitha Vijayan
Copyright 2009 by martyn_mcfly
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project