Writing a super fast deep-property reader/writer using delegates

.Net, Visual Studio 3 Comments

For a while now I’ve been writing to write a non-reflection based property reader that allows me to read properties of an object in a deep hierarchy.

So lets say you have the following simple class hierarchy:

public class FirstLevel
{
    public string Leaf { get; set; }
}
public class SecondLevel
{
    public FirstLevel First { get; set; }
}
public class Root
{
    public SecondLevel Prop { get; set; }
}

Then you want to read the Leaf from the FirstLevel by looking at the Root level and all you have is the path to the property: “SecondLevel.FirstLevel.Leaf”.

Using reflection is a simple process of recursively going through the object, finding the proper property doing a GetValue on it and then repeating the process until you find your property. This is ok but it’s very slow. Very very slow.

Your other two alternatives if you don’t want the bear the hit of reflection is to code emit a method that would resemble something like this:

public static string GetLeaf(Root root)
{
    SecondLevel secondLevel = root.Prop;
    if ( secondLevel != null )
    {
        FirstLevel firstLevel = secondLevel.First;
        if ( firstLevel != null )
        {
            return firstLevel.Leaf;
        }
    }
    return null;
}

Or use dynamically created delegates to map to the get_Property and set_Property methods generated for each property.

 

using System;
using System.Reflection;

namespace ACorns.Utils
{
    public interface IPropertyAccessor
    {
        object GetValue(object target);
        void SetValue(object target, object value);
    }

    public interface IPropertyAccessor<TargetObject, FinalProperty>
    {
        FinalProperty GetValue(TargetObject target);
        void SetValue(TargetObject target, FinalProperty value);
    }

    /// <summary>
    /// Super-dooper, super-fact deep property extractor.
    /// You can use it to get/set properties deep in an object hierarchy without using reflection.
    /// Please cache the returned IPropertyAccessor if you want to reuse it.
    /// Good performance is only achived with cached IPropertyAccessor(s)!
    ///
    /// Usage: IPropertyAccessor accessor = PropertyExtractor.GetAccessor(typeof(Root), "Prop.First.Leaf", true);
    /// accessor.GetValue(target);
    /// </summary>
    public static class PropertyExtractor
    {
        public static IPropertyAccessor<TargetObject, FinalProperty>
            GetAccessor<TargetObject, FinalProperty>(Type targetType, string propertyNames)
        {
            return GetAccessor<TargetObject, FinalProperty>(targetType, propertyNames, true);
        }
        public static IPropertyAccessor<TargetObject, FinalProperty>
            GetAccessor<TargetObject, FinalProperty>(Type targetType, string propertyNames, bool throwOnNull)
        {
            IPropertyAccessor internalPropertyAccessor = GetAccessor(targetType, propertyNames, throwOnNull);

            Type accessorType = typeof(TypedPropertyAccessor<,>).MakeGenericType(typeof(TargetObject), typeof(FinalProperty));
            IPropertyAccessor<TargetObject, FinalProperty> propertyAccessor =
                (IPropertyAccessor<TargetObject, FinalProperty>)Activator.CreateInstance(accessorType, internalPropertyAccessor);

            return propertyAccessor;
        }
        public static IPropertyAccessor GetAccessor(Type targetType, string propertyNames, bool throwOnNull)
        {
            string[] deepPropertyNames = propertyNames.Split('.');
            IPropertyAccessor internalPropertyAccessor = GetAccessor(targetType, deepPropertyNames, 0, throwOnNull);
            return internalPropertyAccessor;
        }

        private static IPropertyAccessor GetAccessor(Type targetType, string[] deepPropertyNames, int level, bool throwOnNull)
        {
            string property = deepPropertyNames[level];

            PropertyInfo propertyInfo = targetType.GetProperty(property);

            // Create a delegate to a get_ method. The delegate looks like
            // Func<TargetType, PropertyType> func to a property like class TargetType { public PropertyType { get; } }
            Type getterDelegateType = typeof(Func<,>).MakeGenericType(targetType, propertyInfo.PropertyType);
            Delegate getDelegate = Delegate.CreateDelegate(getterDelegateType, propertyInfo.GetGetMethod());

            IPropertyAccessor accessor;

            level++;
            if (level < deepPropertyNames.Length)
            {
                // Recursive detect the down the property
                IPropertyAccessor nextLevelAccessor = GetAccessor(propertyInfo.PropertyType, deepPropertyNames, level, throwOnNull);

                Type accessorType = typeof(PropertyAccessor<,>).MakeGenericType(targetType, propertyInfo.PropertyType);
                accessor = (IPropertyAccessor)Activator.CreateInstance(accessorType, getDelegate, nextLevelAccessor, throwOnNull);
            }
            else
            {
                Type setterDelegateType = typeof(Action<,>).MakeGenericType(targetType, propertyInfo.PropertyType);
                Delegate setDelegate = Delegate.CreateDelegate(setterDelegateType, propertyInfo.GetSetMethod());

                Type accessorType = typeof(LeafPropertyAccessor<,>).MakeGenericType(targetType, propertyInfo.PropertyType);
                accessor = (IPropertyAccessor)Activator.CreateInstance(accessorType, getDelegate, setDelegate);
            }

            return accessor;
        }

        #region TypedPropertyAccessor
        internal sealed class TypedPropertyAccessor<T, U> : IPropertyAccessor<T, U>
        {
            private readonly IPropertyAccessor _next;

            public TypedPropertyAccessor(IPropertyAccessor next)
            {
                _next = next;
            }

            public U GetValue(T target)
            {
                return (U) _next.GetValue(target);
            }

            public void SetValue(T target, U value)
            {
                _next.SetValue(target, value);
            }
        }
        #endregion

        #region Recursive Property Accessors
        internal sealed class PropertyAccessor<T, U> : IPropertyAccessor
        {
            private readonly Func<T,U> _readDelegate;
            private readonly IPropertyAccessor _next;
            private readonly bool _throwOnNull;

            public PropertyAccessor(Func<T, U> readDelegate, IPropertyAccessor next, bool throwOnNull)
            {
                _readDelegate = readDelegate;
                _throwOnNull = throwOnNull;
                _next = next;
            }
            public object GetValue(object target)
            {
                object result = _readDelegate((T)target);
                if (result == null)
                {
                    if (_throwOnNull)
                        throw new NullReferenceException("Property '" + _readDelegate.Method.Name + "' on '" +
                            typeof(T).Name + "' returned null.");
                    else
                        return default(U);
                }
                return _next.GetValue(result);
            }
            public void SetValue(object target, object value)
            {
                object result = _readDelegate((T)target);
                if (result == null)
                {
                    if (_throwOnNull)
                        throw new NullReferenceException("Property '" + _readDelegate.Method.Name + "' on '" +
                            typeof(T).Name + "' returned null.");
                    else
                        return;
                }
                _next.SetValue((U)result, value);
            }
        }
        internal sealed class LeafPropertyAccessor<T, U> : IPropertyAccessor
        {
            private readonly Func<T, U> _readDelegate;
            private readonly Action<T, U> _setDelegate;

            public LeafPropertyAccessor(Func<T, U> readDelegate, Action<T, U> setDelegate)
            {
                _readDelegate = readDelegate;
                _setDelegate = setDelegate;
            }
            public object GetValue(object target)
            {
                object result = _readDelegate((T)target);
                return result;
            }
            public void SetValue(object target, object value)
            {
                _setDelegate((T)target, (U)value);
            }
        }
        #endregion
    }
}

To use it you would request an IPropertyExtractor and then ask it to do a GetValue for you:

[TestMethod]

public void ExtractPropertyFromLeafFixture()

{

    Root r = new Root();

    r.Prop = new SecondLevel();

    r.Prop.First = new FirstLevel();

    r.Prop.First.Leaf = "original value";

    IPropertyAccessor accessor = PropertyExtractor.GetAccessor(typeof(Root), "Prop.First.Leaf", false);

    Assert.IsNotNull(accessor);

    object value = accessor.GetValue(r);

    Assert.AreEqual("original value", value.ToString());

    accessor.SetValue(r, "new value");

   
    value = accessor.GetValue(r);

    Assert.AreEqual("new value", value.ToString());

}

The beauty of this approach is that once the IPropertyAccessor is created (using Reflection) in the beginning,  the Get/Set on it is done via a set of direct delegate calls to the property and calls via the interface to the next level.

The callstack is thus very small and efficient:

image

Make sure you cache this accessor if you need it again at a later time as creating it is expensive.

The performance should be as good as you can get without using code emitting.

TechEd done and dusted

.Net, Debugging, Readify, Tools, Visual Studio 5 Comments

Well, it seems like everyone is doing a post TechEd review of their own.

Me … I have to start with the comment that this was the best TechEd I’ve been too mostly because of the comments I’ve received for my sessions.

Some comments were good, some were great, some were funny and some were really eye-openers. (oh, yes, some were dull but they are worthless).

On Sunday I flew to TechEd NZ and much to my surprise when I’ve tried to book into my reserved hotel I was informed that I was upgraded and moved to the hotel on the other side of the road. Well, thank you. This was much appreciated. I was basically in the hotel just above the conference. I was taking the lift to level 4-5 and I was existing directly in the middle of the conference. You godda love that.

On Tuesday I had to do both my talks one after the other and then run straight to the airport. I didn’t get any feedback back as TechEd NZ seems to use paper to collect feedback and evaluations and heard no news from them. I hope they were good as I want to go back next year … one week earlier to do a bit of skiing as well …

On Wednesday I had my talk on debugging which went very well. The room was packed and the demos worked a threat. The only issue I had were the lights which were pointing straight into my eyes giving me the feeling of an FBI interview. It was hard to see the people in the room and the raised hands for questions.

The reviews&evals were really great. So great, I jumped straight in the top 10 best sessions and top 5 best speakers. Thanks everyone for filling in the reviews.

I have to write here some of the comments:

  • Best session I’ve been to for several Techeds, congratulations. this is what it is all about….
  • Simply amazed by this session, by far the best one I have seen during Tech-ed so far.
  • Utterly brilliant – this session paid for the entire entrance fee Excellent

Well, there were over 50 such comments so thanks everyone for taking the time and writing them.

Now, some of the funny ones included no comments but a rating of “Too Technical“. Well, for what we considered (and it was marked as) a 400 level presentation and some considered in their comments as a 300 level presentation, a “Too Technical” comment can only be considered a compliment. If you left that comment, then mate, the session was supposed to be technical ;)

Some of the eye-opener comments:

  • I understand there was alot to cover, but at times the information was a bit rushed, and I found myself unable to keep up with some of the more arcane sections
  • The only thing is that sometimes he’d speak a little too quickly, which combined with the accent made it hard to hear some things
  • Well delivered. He did have a “balky” moment when he mentioned he might shoot himself in the “leg”.(Woops. It was supposed to be “foot”. Sorry for that.)
  • Speaker’s accent was hard to understand at times

Comments from the security talk:

  • Ease up on the hand waving Corneliu :-) When flicking through code please linger a little longer to give people at least 2-3 seconds to process the context and the detail.
  • Speaks very fast
  • The guy was like a ferret on speed. Great knowledge, but flicked between code wayyy too quick. There were very few web developers in the room, and most of it was about web stuff. Can’t remember what was in the description, but this was disappointing.
  • Very web oriented, which was not obvious from the title.

Thanks a lot for these comments.

I know exactly what I need to focus on for next year.

  • Speak slower (and keep that rhythm of talking): I know I was quite conscious at times of this during my debugging talk but I completely lost myself in the security one as I had the feeling I’m running out of time
  • Present less: Have a smaller amount of information to talk about in one hour. Plan to have the session for only 50 minutes so I have time to speak slower. It’s easy to have an ace up the sleeves prepared that can fill in the last 5 minutes if you finish early.
  • Fix my accent: Maybe if I can just speak a bit slower it’s easier with my accent as well. I know my diction in English is quite bad and I’m hard to understand at time (let me tell you I speak as bad and hard to understand in my native Romanian as well). I’ve already engaged an English teacher (my lovely wife) to prepare a course for me to improve my diction and accent.
  • Work on some of the “quotes”/”jokers” that I use in my talks and make sure they are right. I’ll have to make sure you shot yourself in the “foot” not in the “leg”. (The original joke I was trying to refer to is How to Shoot Yourself In the Foot)

Well, now, back to my small corner of development:

  1. I have to finish the Deadlock detector as I think I have a fix for the tool not finding deadlocks between a lock() and a slim lock or read/write lock.
  2. I want to publish a Secure Web library for automatic encoding for ASP.Net controls that I was talking about in my security talk.
  3. I need to publish a new site for my games shop
  4. I need to start planning my upcoming trip to Europe

And most importantly I want to start planning for some new RDN talks and for some cool talks for next year’s TechEd.

PS>> If you were in my sessions and are looking for the demos/slides, please logon to TechEd portal and navigate to the session scheduler and you can find the pptx attached to the description of the sessions.

TechEd & TechEd: Security and debugging

.Net, Improve Your Code, Readify, Secure your code, Visual Studio 3 Comments

It’s all been confirmed: I’ll be speaking at both TechEd New Zealand and TechEd Australia delivering two talk:

  1. Secure Development Patterns – How not to screw yourself during development
    We’ve all seen the news reports showing what can happen when an application goes into production with security issues. But what can you, as a developer or architect, do to stop it from happening to your team? Plenty! This session will provide a bunch of practical, real-world examples of how you can implement “defence in depth” in your projects.
  2. Debugging the world starting with the CLR
    It’s 3:00 AM and your service is down; your boss is breathing down your neck, and your SLA is approaching fast. The operations team refuse to allow you to install Visual Studio on the production machine, and without F5 or Attach to Process, you don’t know what to do. If only you had attended this session. Corneliu Tusnea will show you how to debug systems where no IDE is allowed; on running servers with live data, or offline with memory dumps. You’ll develop a sixth sense for finding deadlocks, memory leaks or unexpected exceptions. Whether you build Windows Services, ASP.NET websites or smart client applications, this session will give you the skills you need to debug in production, impress your fellow developers, and avoid being called into work at 3:00 AM!!

TechEd New Zealand schedule:

  1. SEC314 – Secure Development Patterns: How not to screw yourself during development
    Tuesday, 02 September 2008, 12:10PM – 1:25PM
    Elliot Rooms 1-3, Crowne Plaza
  2. DEV313 – Debugging the world, starting with the CLR (or Debugging from the trenches) (a shortened version of the talk as all I received was 30 minutes of talking)
    Tuesday, 02 September 2008, 1:40PM – 2:10PM
    NZ Room 4, SkyCity

You can book the sessions here: https://aunz.msteched.com/nz/sessions.aspx

TechEd Australia schedule:

  1. DEV410 – Debugging the world, starting with the CLR (or Debugging from the trenches)
    Wednesday, 03 September 2008, 2:00PM – 3:15PM
    Parkside Ballroom B
  2. SEC314 – Secure Development Patterns: How not to screw yourself during development
    Thursday, 04 September 2008, 12:00PM – 1:15PM
    Bayside 202/203

You can book the sessions here: https://aunz.msteched.com/au/sessions.aspx

I’ll also be around at other sessions and at the Ask the Experts night so please come and ping me.

As per Michael’s post there is also a competition on Ask the Experts to find the ultimate expert. Someone has to ask a question and nominate you to provide an answer to win a cool HP TouchSmart.

So please go and ask a question and nominate me to answer it.

Make the questions cool but not too hard as I might not be able to answer them :) )

ACorns.Debugging – The .Net Deadlock Detector

.Net, Debugging, Improve Your Code, Tools, Visual Studio 18 Comments

As nothing exciting has happed in my yard since the last release of Hawkeye I’ve decided to spice up my life and write a new .Net tool: The .Net Deadlock Detector.

The punch line: The .Net Deadlock Detector is the only* tool that is able to detect and report a deadlock inside a running .Net process in a production environment or out of a memory dump.

(* Disclaimer: I couldn’t find any other tool to do this. If you know of one please let me know)

Production environment is an environment in which you don’t (want to) have installed a debugging tool like Visual Studio.

The .Net Deadlock Detector

  1. The tool does not require to have the code re-compiled in any way or form, with any external dependencies, nor reference any external library or have you modify your code to use any special type of locks inside your code
  2. It works on release builds with no PDB files
  3. It works on running processes or previously captured memory dumps
  4. It detects deadlocks across multiple threads and returns detailed call-stack and lock usage information
  5. It only detect deadlocks in which threads are actively waiting for locks acquired by other threads
  6. It does not detect the dining philosophers problem or deadlocks created in combination of time waits + wake/check + lock
  7. It has an external dependency on the cdb.exe (part of the the free Debugging Tools for Windows package from Microsoft)
  8. It requires absolutely no installation. It an xcopy deployment
  9. And best of all it’s free (source code to be published soon)

What’s a deadlock

A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. It is often seen in a paradox like ‘the chicken or the egg‘. (wikipedia)

For example Thread 1 locks resource A, Thread 2 locks resource B, Thread 1 wants resource B and starts a wait on resource B, Thread 2 wants resource A and starts a wait on resource A.

In this moment the two threads are considered deadlocked as each of them owns a resource while trying to acquire another resource owned by a different thread.

  1. Two threads, two resources
    image
  2. Thread 1 acquires resource A
    image
  3. Thread 2 acquires resource B
    image
  4. Thread 1 wants resource B and starts waiting for it
    image
  5. Thread 1 wants resource A and starts waiting for it
    image

Now both threads wait for the other one to release their resource.

How does the .Net Deadlock Detector work

The .Net Deadlock detector works by loading the cdb.exe (one of the native Windows Debuggers) on the target process and hooking the input and output streams of it to allow it to send commands and receive output from the debugger.

Then the tool is loading the sos (Son-on-Strike) debugging extensions into the cdb and starts sending commands to the cdb and sos and parse the output.

Then the tool follows a standard procedure in trying to find a managed deadlock by analysing the locks, the threads and the callbacks for each of the threads. As always one of the best examples to understand deadlocks is is Tess’s Deadlock case study.

So more or less the tool is a glorified macro system and command automation that is using standard cdb and sos commands to understand what is happening with the process and does some intensive analysis (including circular references search) to detect the deadlock.

How to “install” the .Net Deadlock Detector

  1. First of all before you can use the .Net Deadlock Detector you need to install (on your development machine not on the production machine) the Debugging Tools for Windows from Microsoft
  2. Then Download ACorns.Debugging.FindDeadlock.1.0.1.zip and unzip
  3. Now you need to copy the cdb.exe from the installation folder (defaults in C:\Program Files\Debugging Tools for Windows\cdb.exe) into the folder where the ACorns.Debugging.FindDeadlock.exe was unzipped
  4. You are now ready to use the tool
  5. You can now copy this new folder containing (ACorns.Debugging.Cdb.dll, ACorns.Debugging.FindDeadlock.exe and cdb.exe) to your production machine or target machine and start finding your deadlock

(Note: I’d love to deliver the a complete tool but the cdb.exe is not redistributable.)

How to use the .Net Deadlock Detector

If you finally got the right files prepared you are ready to try to find your deadlock.

The tool can be used with a set of command line parameters (exclusive):

ACorns.Debugging.FindDeadlock.exe [/pn=<processname>|/pid=<processid>|/DumpFile=<path to memory dump file>]

  • /pn=name of a process
  • /pid=id of a proces
  • /DumpFile=path to the file

For example to try the tool on the provided demo application start the application then start the deadlock detector with:

ACorns.Debugging.FindDeadlock.exe /pn=ACorns.Debugging.DeadlockTests.exe

Interpreting the results

The tool will output a bunch of less relevant details as it tries to understand the deadlock and at the end it presents a “graphical” representation of the deadlock with some general details about the threads and the locks:

image

Thread 4 owns a lock named B and waits for lock A. Thread 3 owns the lock A and waits for B.

Then the most relevant part of the analysis comes: the callstack involved in the deadlock. (the callstacks should be read bottom to top with the Using of locks referring to the next (up) method)

Thread 4 has the following callstack:

image

We can see that method StartThraed2 is using lock B and the Thread2Worker is trying to use lock A.

Thread 3 has the following callstack:

image

Method StartThread1 is using lock A and Thread1Worker is trying to use lock B.

With all this information at hand you should be able to find and fix your deadlock. Good luck!

If you have a memory dump of a process that has a deadlock that the tool can’t detect please let me know as I’d like to debug it and improve the tool.

Now, start the download of the ACorns.Debugging.FindDeadlock.1.0.1.zip tool and then head to Microsoft to download the Debugging Tools for Windows.

The tool is based on an idea by Tatham Oddie and Paul Stovell. Thanks guys!

Improve your debugging: Debugging Attributes to make your life easier

.Net, Improve Your Code, Visual Studio 6 Comments

During my recent talk on CLR Production Debugging I’ve talked about several interesting attributes that you introduce in your code to make debugging easier. Here is a review of these attributes:

Debugging support attributes

DebuggerStepThroughAttribute & DebuggerNonUserCodeAttrbute

Both attributes do about the same thing: they informs the debugger to not step into that class during normal “Step-Into” debugging. They are generally used on generated code or framework code to avoid the pain on getting into a method that you don’t care about while you debug.

There are very subtle differences between the two attributes:

  • DebuggerStepThroughAttribute
    • Can be applied to Classes, Structs, Constructors and Methods
    • Will not step into the method on “Step Into” through the debugger, but allows you to set a breakpoint in the method and the debugger will stop at that breakpoint.
  • DebuggerNonUserCodeAttrbute
    • Can be applied to Classes, Structs, Constructors, Methods and Properties
    • Will not step into the method at all even if you have a breakpoint setup or an exception is raised

Sample:

The debugger will not enter in any method declared in this class:

[DebuggerStepThrough()]
public static class ConfigUtils
{
    [...]
}

Debugging enhancement attributes

Enhancements attributes are attributes that have no meaning for the CLR but are used by the debugger to improve the way the type/variable is displayed when you look at it in the debugger.

DebuggerDisplayAttribute

Changes the way the class is rendered in the debugger. In the declaration of the attribute you can include multiple properties or fields of you class to be displayed and even call methods on your class and render the result.

You can also include properties or fields of your member properties/fields and your class.

This attribute can be applied to basically almost every construct (classes, structs, enums, fields, properties, delegates and assemblies).

The attribute also supports display based on conditions and basic formatting like “nq” used to strip away quotes from strings.

Samples:

[DebuggerDisplay("Id={id} Name={firstName} {lastName}")]
public class Customer
{
    private int id;
    private string firstName;
    private string lastName;

No attributes applied:

image

Applying the attribute will display:

image 

The following attribute:

[DebuggerDisplay("Id={id} Name={firstName, nq} {lastName, nq} Orders={Orders.Count}")]

Renders this:

image

More details can be found on the MSDN page Using DebuggerDisplay Attribute.

DebuggerBrowsableAttribute

The attribute can be used to let the debugger know how to display (or not to display) specified fields or properties of your class.

For example setting [DebuggerBrowsable(DebuggerBrowsableState.Never)] will make the field invisible in the watch of data tips of the debugger. This is especially useful when you have fields that are exposed as properties and you don’t want to see them twice in the debugger:

Sample:

public class Customer
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private int id;
    public int Id
    { get { return id; } set { id = value; } }
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string firstName;
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string lastName;

No attributes applied will render this:

image

(you can see both the fields and the properties)

Applying the attributes will render this:

image

(only the public properties are visible)

Several other attributes worth having a look at are:

  • DebuggerTypeProxyAttribute – allows to specify a proxy class that will be used to display your type. The debugger will instantiate the proxy and ask it to render your class. Can be very valuable when you want to look at complex classes.
  • DebuggerVisualizerAttribute – allows you to specify a class to be used to visualize your type.

Happy debugging.

Update: 25/07/2008 20:50: You can see a quick screen-cast of using these attributes here: http://www.acorns.com.au/files/VS_Debugging_Tips_Attributes.wmv (10.6Mb)

TFS Quick Search 1.2 – VS2008 and some small changes

.Net, Personal, Readify, TFS, Visual Studio 6 Comments

Based on the feedback received for the first version of TFS Quick Search I’ve modified it and here is the TFS Quick Search 1.2.0:

Changes in TFS Quick Search 1.2

  • Support for VS2008 and VS2005
  • Improved code (slightly faster searches)
  • Highlight of the currently found item while searching
  • Enter exits the search box and focuses on the selection

You can download TFS Quick Search 1.2 from the project’s homepage.

TFS Quick Search Plugin

.Net, Hawkeye, Readify, TFS, Visual Studio 9 Comments

 Working lately more and more with TFS I got a bit frustrated on scrolling in a long list of Tasks/Work Items/Issues to find for “that one task” that has a specific word in its description so I decided to write a small plugin to make my life easier.

Based on the Hawkeye core and the Dynamic Extenders that Hawkeye support I’ve build TFS Quick Search Plugin:

The plugin will add a small (and red) search text box to all TFS result windows. You can simply type a keyword in there and the first TFS Item that contains that keyword in its title will be selected in the results view. Pressing F3 will search and select the next occurrence of the specified keyword.

Prefixing the text you enter with the “#” symbol will execute the search in the “ID” column. So entering “#4581″ will select the item with ID 4581.

For more details and installation instructions visit TFS Quick Search Project page.

TechEd Day 5: .Net Addins, Irresitible forces and musical reflections

.Net, Hawkeye, Readify, Visual Studio No Comments

Last day, last set of sessions:

My day started with meeting a guy from Iceland on the metro who arrived in Australia the night before. He went out for a drink with a friend, got trashed, and lost, took a cab around to search for his hotel until he run out of money, then slept with the some homeless guys next to an ATM machine in some dead end of Barcelona. In the morning he managed to get some cash out of an ATM and was now on the train, going downtown to try to find his hotel. He looked trashed and dirty poor guy but better than what you’d expect after such an experience. I just hope he managed to find his hotel :)

Now, back at TechEd:

Addins, Extensibility

  • Talk about the new .Net 3.5 Addin model
  • Extensibility (Flash for the browser)
  • Host automation
  • Solves Version 1 type of problems
    • Discovery
    • Activation
    • Isolation
    • Lifetime Management
    • Sandboxing
    • Unloading
  • Solves Version 2 type of problems
    • Backward Compatibility
    • Forward Compatibility
    • Adding new isolation levels
  • System.Addin & System.Addin.Contract in .Net3.5
  • Separate implementation from integration
  • Questions:
    • Loader
      • There is no loader so you can’t configure a class that will load your addin.
  • Nice demos but to much of “magic” and “just works”
  • The Irresistible forces meet the moveable objects Framework

    Cool talk but too long to take any notes. Room was as packed as at the keynote as this was the only session in this time slot.

    Engineering, architecting, designing and developing reusable frameworks

  • Cool talk about the design of the .Net APIs
  • The standard triangle adds a new dimension of organization
  • Planning
    • Building the right thing
    • Peanut Butter vs Skyscrapers
    • Peanut Butter Focus: features, results: stability, incremental improvements not great end-to-end scenarios
    • Skyscrapers Focus: Scenarios, Results: Excitement, breakthroughs, but beware of leaving the existing customers behind
    • Guideline: avoid peanut butter
  • Architecture – Ensure the long term health of the framework
    • Beware of dependencies
    • Component vs componentization
    • Types of dependencies
      • API Deps: A appears in the interface of type B
      • Implementation: A used in the impl of B
      • Circular deps: forces you to have a single component
    • Framework layering
      • Core and Extensions
    • Dependency management rules
      • Inside a component: ok
    • Up : no-no
    • Cross: take care (MS has a review process for this)
    • Taxonomy
      • Primitives, Abstractions and Reusable Components
      • Abstractions are the hardest to design
        • Difficult to evolve, the glue of the framework
      • Component oriented design
      • Primitive oriented design: (functional)
    • Design: where the quality happens
    • Do treat simplicity as a feature

    Deep Dive .Net Reflection

    Roy Osherove, www.ISerializable.com  (400 level talk)

    Let the just start with the fact that the session started with a guitar on a chair in the middle of the room and a photo of a cat on the power point background :)

  • Reflection on Generics
  • Creating generics at Runtime
  • Bunch of tests on reflection vs code emitting
  • RunSharp – nice library to help code emitting
  • Presented cool IL Visualizer – I should integrate it with Hawkeye
  • Debugging
    • Symbol support
    • [Debuggable(DisableOptimizations)]
    • Add details about the line of code
    • ISymboleDocumentWriter – used to use as template
    • ilGenerator.MarkSequencePoint to mark code points in the document writer
  • StaticILLibrary – IL Parser
  • MethodBase Visualizer
  • And the session finished with Roy playing the guitar a cool song about Reflection. I recorded it so maybe I should try to upload it on YouTube.

    It’s all done. I’m going home. It was great and worth every minute of it. Hopefully I’ll be back next year.

    TechEd Day 4: C++, High-Performance bits & pieces

    .Net, Hawkeye, Readify, Visual Studio No Comments

    C++0x – The New Standard & Compiler

  • About 60-70ppl in the room :)
  • New keywords like “concept” = interface, “requires” = where
  • Concurency: atomic&lg;int&rg;
  • Support for threading primitives lock/release/fences
  • Thread pooling, futures? and lambdas
  • Variadic Templates
    • Great for types ly std:tuple
    • template&lg;typename … Type&rg;
    • tuple<Types …> make_typle(Types&& …)
  • Rvalue References &&
    • void f(int&&)
    • Allows move semantics (copy&destroy) or swap but destroying the source after the operation
    • Concept a bit close to the const& but you can modify the value inside however you lose the result
  • ‘auto’ keyword somehow similar with ‘var’ that allows auto detection of the type.
    • std:vector&lg;std:string&rg; data;
    • auto iter = data.begin(); implies std:vector&lg;std:string&rg;::iterator iter = data.begin();
  • decltype – detect the type of an expression
  • TR1 – boost based library
  • C99 features: long long, variadic macros, __func__
  • Allows ‘>>’ to terminate templates (no need for the extra space)
  • Lambdas (or closures) (line functions or anonymous delegates):
    • Std::for_each(v.begin(), v.end(), &lg;&&rg; ( std::cout << x; ) );
  • Extern templates
  • shared_ptr&lg;t&rg; for pointers  no more deletes to do
  • OH .. and this *works* in templates:
  • shared_ptr<Cat> a;
  • shared_ptr<Dog> b;
  • vector<shared_ptr>Animal>> v; v.push_back(a); v.push_back(b); :)
  • Intel demo:

  • Intel Thread Profiler
  • Inter-thread interactions
  • vTune Analyser
  • Inter-thread memory operations, cache locks, false cache hits
  • Designing High performance business models

  • Book: “Patterns of Enterprise Application Architecture”
    • Talking about “The domain model pattern”
  • Highly testable
    • A test: register for events, call a method, check properties and event args
  • Dlls can then be deployed everyone
  • Domain Models are made of pojo  or more pono now 
  • Service Layer orchestrates persistence
  • O/RM – persistence by reach-ability
  • Lazy loading: can generate performance
  • Build interface for specific operations: IMakeCustomerPrefered, IAddOrdersToCustomer
  • Have generic IFetchStrategy&lg;T&rg; :
  • MakeCustomerPreferedFetchingStrategy : IFetchingStrategy&lg;IMakeCustomerPrefered&rg; { string Strategy { get { return “UnshippedOrders, OrderLines”;} }
  • Use configuration/DI to configure implementations for fetching strategies that can be optimized for each scenario
  • Interesting presentation but a bit too long to get to the point however the idea of having very custom fetching strategies is interesting and seems to be efficient
  • Web Application Security

  • Talk about general coding issues that can lead to security problems
  • SQL Injection, execute commands via sql injection.
  • Demo about dynamically served files via ashx and permissions
  • Partition website to physical folders for public and restricted access
  • Apply defence in depth approach – use multiple gate keepers
  • Demo about using google to hack and find vulnerable sites
  • Or using google code search to find connection strings with database/username/password in them
  • Presented the GuidanceExplorer from Patterns and Practices (http://www.codeplex.com/guidanceExplorer) – looks great!
  • FxCop/Code Analysis, XSSDetect
  • Pipeline ildasm into findstr to find string constants in the assemblies, then use Outlook to review them.
  • Hardcore production debugging

    Ingo Rammer, www.thinktecture.com/conferences  

  • Amazing, fast paced, action packed presentation about using WinDbg and MDbg
  • The auditorium was packed with more ppl than any other session I’ve seen except the keynote
    • Ingo presented Hawkeye as a tool to help with debugging WinForms applications :) .Thanks Ingo.
  • There was also a a presentation of a great tool to help debuging with SOS http://www.thinktecture.com/SOSAssist. I’ll have to give it a try next time I use SOS.
  • TechEd Day 3: ASP.Net & VC++ 2008

    .Net, Hawkeye, Personal, Readify, Visual Studio No Comments

    Building highly scalable ASP.Net websites using asynchronous programming models

  • Thread saturation appears when you run long running ops in the page
  • AsyncPages
  • IsAsync=true in the @Page directive
  • Can run the page on multiple threads with a pause in between
  • Thread1: PreInit->Begin
    • BeginAsyncOperation is an event handler where you can start your async work 
    • EndAsyncOperation is the event handler where your call is returned.
  • Thread2:End->Render
  • In Load:
    • AddOnPreRenderCompleteAsync( new BeginEventHandler(..), new EndEventHandler(..)0
  • ConnectionString requires Async=true to allow Ado.net to run in async mode.
  • The HttpContext.Current is null in the EndAsyncOp but you can keep a ref to it from the Begin
  • In EndAsyncResult only grab the results and return
  • Consume the results from the Page_PreRenderComplete event
  • Multiple async calls are serialized (Begin/End, Begin/End…) so it’s bit tricky to run multiple long running operations
  • Because you free threads immediately out of long running pages they can be used to deliver other pages avoiding thread starving and request queue grow.
  • You can use the PageAsyncTask/RegisterAsyncTask param to run multiple tasks in parallel.
  • You can set timeouts and timeout handlers
  • There were some nice demos about how to use the IHttpAsyncHandler to implement async handlers including ashx.
  • Exceptional demos with of an interesting concept that should make our asp servers more responsive.
  • I did a shameless plug of Paul’s SyncLinq when someone asked if you can run LINQ queries async telling on the mic that Paul Stovell from Readify implemented a SyncLinq extension that allows you to run any LINQ in asynchronous mode

    Building a scalable environment: Top 10 tips for scalable ASP.Net

  • This was an interactive session
  • The guy was talking to fast to be able to take notes but here I go
  • What is scalability: a system is scalable is we can add more workload to a system without increasing the cost of the system per unit of workload.
    • Can ramp up to a large number of users
    • Without increasing cost per user
  • Calculate
    • Fixed cost (devs, hw, isp, bandwidth)
    • Variable costs (costs that go up per transaction, credit card tax per transactions)
    • Or Theoretical costs (this system can do xxx trans/min)
  • Session management:
    • Minimize the use of session
  • View state
    • Reduce as much as possible
  • R = payload/bandwith + RTT+ appturns(rtt)/concurrent requests + cs + cc
    • R = response time
    • RTT = round trip time
    • App turns: http requests
    • Concurrent requests: #server socksts open by browser
    • Cs: server side compute time
    • Cc: client compute time
  • Tools: FireBug, WhySlow (from Yahoo)
  • ArrowPoint switch is a cheap solution for load balancing
  • F5 is a good solution (expensive?)
  • Minimize ViewState
  • Minimize SessionState
  • Cache pages, Cache User Controls
  • Debugging and Crash Dump Analysis with Visual C++ 2008

  • /GS – adds security cookies to avoid buffer overruns
  • STL has checked iterators
  • /analyse – you can use annotations to mark you code to help the compiler detect lots of different types of errors
  • Use ENSURE to assert in debug/throw exception in release.
    • Appears in release builds
  • Build debuggable projects
    • Archive symbols
    • Build pdbs with releases
    • Use ‘props sheets’ to keep settings consistent
    • Build full setups for debug projects so QA can test with extra info
    • Automate against both debug/release
    • Collect crash dumps from customers
  • Debugging Breakpoints
    • Lots of standard stuff plus some cool
    • Tracepoints you can modify the registries eg {eip=0xaaaaa} to jump over code – really cool stuff
    • You can define wild char breakpoints person::* (all methods in person class)
    • Other breakpoints {[function],[source file], [binary module]}[condition] e.g. {,foo.cpp,}@136
  • Watch Window has some new formats:
    • Hr, ws, wm – awesome
  • Pseudoregisters
    • $handles, $TID
  • Autoexp.dat
  • Threads window has new stuff like flag or rename
  • Crashdumps
  • http://winqual.microsoft.com – register and get access to you own crash dumps
  • There was a small “world party” for people from countries that had not enough ppl to have their own party. Countries like Australia :) , Israel, South Africa, Armenia and US.

    ]

    « Previous Entries