Sunday, 16 October 2011

Dynamic HttpModule registration in .NET Framework 4.0

This article could be useful for the scenario that you are developing HttpModule handling related functionality for a web application and you do not have the right to or not intend to modify the Global.asax file to register your HttpModule handler.

So here we go. Step 1, before register a HttpModule handler dynamically the first thing is to create the handler. See code below:

public class MyDynamicModule : IHttpModule
{
    void IHttpModule.Dispose()
    {
    }
 
    void IHttpModule.Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.EndRequest += new EventHandler(context_EndRequest);
    }
    //... more code ignored
}

Step 2, to register this HttpModule handler, we need the reference to Microsoft.Web.Infrastructure assemble and use the contained method DynamicModuleUtility.RegisterModule. See sample code below:

DynamicModuleUtility.RegisterModule(typeof(MyDynamicModule));

But where we call this line of code? As you know HttpModule registration has to be done before the instance of HttpAppliction start. To achieve that we need the newly introduced attribute class System.Web.PreApplicationStartMethodAttribute in .NET framework 4.0. Note this attribute is assembly level attribute. See the code below, as step 3 of our example, of how to use the attribute:

[assembly: PreApplicationStartMethod(typeof(DynamicHttpModule), "RegisterMyModule")]

The code above basically says I want the web application to do something in its pre-start event, which is, to execute the static method DynamicHttpModule.RegisterMyModule, see below the code of the DynamicHttpModule class:

public static class DynamicHttpModule
{
    public static void RegisterMyModule()
    {
        DynamicModuleUtility.RegisterModule(typeof(MyDynamicModule)); //code from step 2
    }
}

And that is it.

Wednesday, 12 October 2011

Controlling XML Serialization Using Attributes

The article is mainly focus on mapping the object type name with the xml file node name when De-serializing the xml file and when the node name is not a proper C# type name

Long story short, here are some brief ideas:

  • Mapping xml root element name with class name: XmlRootAttribute
  • Mapping element name other than the root with class property/field name: XmlElementAttribute
  • Mapping List of elements underneath the parent node, normally to a ArrayList (or generic List<T>): XmlArrayAttribute and XmlArrayItemAttribute
  • Mapping a xml attribute to a C# class property/field: XmlAttribute

More details you can check the Microsoft article: Controlling XML Serialization Using Attributes

Friday, 7 October 2011

Control.BeginInvoke() vs Control.Invoke()

The BeginInvoke() method has a IAsyncResult type return value and the Invoke() method simply return a object. This makes a big difference between them which is the BeginInvoke() is making a asynchronous callback to the thread created the handle of the control.

Assuming the main thread is A and currently executing code is in thread B, if call Invoke() from thread B then thread B has to wait for the callback to thread A being finished and then continue the following process.

Tuesday, 7 June 2011

Ways of passing data between WP7 pages

The book Programming Windows Phone 7 (MS Press) introduced 4 ways:
  1. add a public propery on App class to retain the data during current lifecycle;
  2. add a public propery on the target page, and then in the prev page's OnNavigatedFrom event set the value of the property for the next page;
  3. use the property State from the application's current PhoneApplicationService instance, which is a type of generic dictionary. But the object been stored has to be serializable (the reason is when app goes to deactivated it will serialize the objects to isolated storage);
  4. isolate storage. BUT obviously it is not wise to frequently access isolate storage during application running. Here is a great article of compairing the performance of using isolate storage http://blogs.claritycon.com/kevinmarshall/2010/11/03/wp7-serialization-comparison/

Friday, 18 February 2011

How Secure Are Query Strings Over HTTPS?

The original article is from HttpWatch's blog, a very good post. So, to make a long story short, the conclusions are: At the network level, HTTPS URL parameters are secure, but there are some other ways in which URL based data can leak:
  1. URLs are stored in web server logs,
  2. URLs are stored in the browser history,
  3. URLs are passed in Referrer headers