MohamedG's Log


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

Filtering Event Log Entries by Event Code

When you filter event log entries bu event code using WMI, you can run a query like the following: “SELECT Message FROM Win32_NTLogEvent WHERE EventCode = 7” And as expected, this will return the entries with event code 7. Now, let’s look at the corresponding C# code, given that you already have a list of… 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

Extending XElement to Match Child Elements by LocalName

If you want to ignore the child elements’ namespaces, and match by local name only, you can do something similar to the following: /// <summary> /// Represents an extension for <see cref="XElement"/>. /// </summary> internal static class XElementExtension { /// <summary> /// Gets the first (in document order) child element with the specified <paramref name="localName"/>…. Read more

Reflector Visual Studio Extension – Enable Debugging

I’ve been using the standalone Reflector.exe for so many years now, and recently I’ve been using JustDecompile too. They have different accuracy levels and different set of distinguishing features, but the one I use in VS is Reflector. I was excited about a feature in the Visual Studio extension, I had to blog about it!… 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

Overriding an Abstract Property vs. Passing a Parameter

An abstract class may require a value to be passed through the constructor like the following example: public abstract class Node { private readonly Uri _uri; protected Node(string address, string relativePath) { _uri = new Uri(Path.Combine(address, relativePath)); } } public class Element : Node { private const string relativePath = "/path"; public Element(string address) :… Read more

Changing the Foreground Color of an Indeterminate ProgressBar

You would think that the following XAML changes the indeterminate progress bar’s foreground color: <ProgressBar IsIndeterminate="True" Foreground="Aquamarine" /> Unfortunately, that doesn’t work. You will need to override the following value in the default theme resource dictionary: <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Default"> <x:String x:Key="ProgressBarIndeterminateForegroundThemeBrush">Aquamarine</x:String> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> You can add that override to App.xaml, or to a new resource… Read more