MohamedG's Log

TW9oYW1lZCBFbC1HZWlzaA==

Warning: DataContractSerializer won’t call your constructor!

Consider the following naïve data contract: [DataContract] public class Data { private int[] array; public Data() { this.array = new int[13]; } public int Length { get { return this.array.Length; } } } It looks ok, right? Let’s use it then: DataContractSerializer serializer = new DataContractSerializer(typeof(Data)); Data data = new Data(); using (MemoryStream stream =… Read more

Adding HTTP Headers to WCF Calls

To add HTTP headers, request messages have to be intercepted before they are sent to the server. This can be done by implementing the IClientMessageInspector.BeforeSendRequestmethod: /// <summary> /// Represents a message inspector object that can be added to the <c>MessageInspectors</c> collection to view or modify messages. /// </summary> public class ClientMessageInspector : IClientMessageInspector { ///… Read more

Adding Custom SOAP Headers in WCF

First, you need to decide whether to create the custom header using the MessageHeader.CreateHeader method, or by extending the abstract MessageHeader class. Under the hood, the CreateHeader method returns an instance of an internal class called XmlObjectSerializerHeader, which uses a serializer to write the header: protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) { Type type;… Read more

DefaultNetworkCredentials in Windows Store Apps

Disclaimer: I’m still getting acquainted with Windows Store Apps, so I could be totally wrong; proceed with caution. It’s been a while since I last wrote a blog post, and it’s 4 AM right now, this may give you an idea why I had to get this out. Hopefully I may save poor souls that… Read more

WCF Service Throttling

Throttling, generally speaking, is tricky. Get the limits low and you may be prone to DoS and clients timing out trying to connect to your service in vain; Get them high and you may end up with an overloaded service that’s eating up machine resources until it crashes. There’s a sweet spot in between that… Read more