Click here to Skip to main content
Click here to Skip to main content
Go to top

Array instead of List in WCF

, 28 Nov 2009
Rate this:
Please Sign up or sign in to vote.
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:

[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)

Share

About the Author

Artem S. Dmitriev
Software Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralYou are using the wrong construction PinmemberRamon Smits30-Nov-09 4:19 
GeneralRe: You are using the wrong construction Pinmembermartyn_mcfly30-Nov-09 19:33 
GeneralRe: You are using the wrong construction PinmemberRamon Smits30-Nov-09 22:03 
GeneralRe: You are using the wrong construction Pinmembermartyn_mcfly30-Nov-09 22:19 
GeneralThank you for sharing ! PinmemberMasTooool28-Nov-09 7:37 
GeneralRe: Thank you for sharing ! Pinmembermartyn_mcfly28-Nov-09 9:27 
GeneralThis is not an article PinmvpJohn Simmons / outlaw programmer28-Nov-09 2:51 
GeneralRe: This is not an article Pinmembersam.hill28-Nov-09 5:23 
GeneralRe: This is not an article Pinmembermartyn_mcfly28-Nov-09 6:55 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Nov 2009
Article Copyright 2009 by Artem S. Dmitriev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid