Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/intimida/public_html/index.php:7) in /home3/intimida/public_html/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 431

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/intimida/public_html/index.php:7) in /home3/intimida/public_html/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 431
.Net « Intimidating MONKEY

Archive for category .Net

Thou shalt not explicitly DataBind

Mmmmm. Remember that the Page.DataBind does this for you, explicitly calling control.DataBind is probably a bad idea.

See:
Do you use Page.DataBind?
Avoid calling Page.DataBind and bind each control individually to optimize your data binding. Calling Page.DataBind recursively calls DataBind on each control on the page.

You’re best bet is to override DataBind and call base.DataBind()

C# Array Initialization

An important reminder from our sponsors (oh, and just admit it… You can never remember this anyway):

From the 1.2 language spec:

The new operator automatically initializes the elements of an array to their default value, which, for example, is zero for all numeric types and null for all reference types.

Important notes about Array.Initialize()

It must not be used on reference-type arrays.

You can use this method only on value types that have constructors; however, value types that are native to C# do not have constructors.

And of course, if you don’t trust me, see for yourself.

List<> Interleaving

Ever wanted to interleave multiple sources of data? Well I did, so I wrote this, which interleaves multiple (generic) lists into a single.

public static List<telement> Interleave<telement>(params List<telement>[] pvParamLists)
{
    // if nothing is provided we'll simply return an empty list
    if (pvParamLists == null) return new List<telement>();
 
    int listCount = pvParamLists.Length;
 
    // offset map indicating current position in a given list
    int[] pointerMap = new int[listCount];
 
    // count total elements
    int resultSize = 0;
    foreach(List<telement> list in pvParamLists)
    {
        if (list != null)
        {
            resultSize += list.Count;
        }
    }
 
    // create our result list
    List<telement> interleavedList = new List<telement>(resultSize);
 
    // will quit when finished
    while (interleavedList.Count < resultSize)
    {
        for (int x = 0; x < listCount; x++)
        {
            if (pointerMap[x] != -1)
            {
                List<telement> currentList = pvParamLists[x];
                // skip null or empty lists
                if ((currentList == null) || (currentList.Count == 0) || (pointerMap[x] >= currentList.Count))
                {
                    pointerMap[x] = -1;
                    continue;
                }
 
                interleavedList.Add(currentList[pointerMap[x]++]);
            }
        }
    }
 
    return interleavedList;
}

The whole file, including some unit tests is here.

Copy via XmlSerializer

For God’s sake, don’t ever write this (at least not again). Its such a trivial piece of code that I never bother librarifying it, and yet every time I write it I leave out the reset of the stream position. But, what do you expect… I’M A MONKEY!!!

public static Object XmlSerializationCopy(Object pvValue)
{
  if (pvValue == null) return null;
  XmlSerializer ser = new   XmlSerializer(pvValue.GetType());
  // most are probably small.
  MemoryStream serStream = new MemoryStream(512);
  using(serStream)
  {
    ser.Serialize(serStream, pvValue);
    serStream.Position = 0;
    return ser.Deserialize(serStream);
  }
}

Use StreamWriter with XmlSerializer

Or something similar lest you risk paying the price for not paying attention.

The XmlSerializer can serialize an object to a stream, any stream. If the stream you are searializing your object to already contains data, and the length of that data is longer than the length of the data you are currently writing (your about-to-be serialized object) you get artifacts.

This is to say that if you open a stream without clearing it in some manner, and then write less data than what was already there…. You get your new data at the begining, and leftovers from what was there before at the end.

So in the case of serializing to a file, the easiest thing to do is use a StreamWriter and provide the path of the file to which you wish to write. The StreamWriter overwrites any existing file for you by default.

As per msdn documentation:
StreamWriter Constructor (String)
The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share. If the file exists, it is overwritten; otherwise, a new file is created.

Output Caching Web Service Data

Using the output cache is super fast, so it’s nice to use when performance is a concern. And performance is often a concern around Web Service interfaces, so it’d be nice to use it there.

The easiest way to do it globaly is to hook the PostRequestHandlerExecute event in the request processing pipeline and set your cache policy values there (conditionally if necessary). However there seems to be some requirement to set the CacheDuration property on the WebMethod attribute of the method being exported via the service. I’m not sure why this is, but presumably it’s doing something to provide appropriate ‘varyby’ handling for the request body based SOAP message.