Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C# 4.0

Array instead of List in WCF

Rate me:
Please Sign up or sign in to vote.
4.71/5 (6 votes)
28 Nov 2009CPOL1 min read 43K   4   9
This article describes the differences between using arrays and generic lists in WCF.

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:

C#
[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>:

XML
...
<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:

XML
...
<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)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYou are using the wrong construction Pin
Ramon Smits30-Nov-09 4:19
Ramon Smits30-Nov-09 4:19 
GeneralRe: You are using the wrong construction Pin
Artem S. Dmitriev30-Nov-09 19:33
Artem S. Dmitriev30-Nov-09 19:33 
GeneralRe: You are using the wrong construction Pin
Ramon Smits30-Nov-09 22:03
Ramon Smits30-Nov-09 22:03 
GeneralRe: You are using the wrong construction Pin
Artem S. Dmitriev30-Nov-09 22:19
Artem S. Dmitriev30-Nov-09 22:19 
GeneralThank you for sharing ! Pin
MasTooool28-Nov-09 7:37
MasTooool28-Nov-09 7:37 
GeneralRe: Thank you for sharing ! Pin
Artem S. Dmitriev28-Nov-09 9:27
Artem S. Dmitriev28-Nov-09 9:27 
GeneralThis is not an article Pin
#realJSOP28-Nov-09 2:51
mve#realJSOP28-Nov-09 2:51 
GeneralRe: This is not an article Pin
sam.hill28-Nov-09 5:23
sam.hill28-Nov-09 5:23 
GeneralRe: This is not an article Pin
Artem S. Dmitriev28-Nov-09 6:55
Artem S. Dmitriev28-Nov-09 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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