Microsoft Technology Conference in Hanoi

March 19th, 2011 Buu Nguyen 2 comments

I had a chance to speak about HTML5 in IE9 and ASP.NET MVC 3 at the Microsoft Technology Conference in Hanoi, Vietnam on Thursday Mar 17th 2011.  The event was supposed to attract 200 audiences.  However the suddenly cold weather and non-stop rain (more later) probably made a few dozens not show up.

This trip to Hanoi was very memorable in many ways.  First, I met and had conversations with many interesting people and learned many things from other speakers (agile development with Scrum & Kanban, BI in SharePoint 2010, distributed caching in practice etc.)  It’s a pity that I had to leave early to catch the flight (which was eventually delayed for as much as 2.5 hours) so I missed a couple of late sessions.

Second, the whole trip itself contains full of surprises.  Both departing and coming back flights were delayed 1 and 2.5 hours respectively, leaving me exhausted in the airports (thanks VNA!).  And then the weather.  In Ho Chi Minh, it was hot like 30 C degree something.  In Hanoi, it was 9 C the moment the plane arrived and remained that cold during the whole time.  I ended up putting on as many clothing as I brought with me to avoid catching a cold (and sounding like a duck during my speeches).  Additionally, it was raining all the time and I was basically on taxi every time I walked out of a building. (And should I mention that there were 2 cab drivers, 1 hotel receptionist and 1 flightmate mistook me for being foreigner and started speaking English to me!)

Slides and code can be found below.  There are a few pictures (of me and some other speakers) taken by a friend of mine.  I hope to receive pictures from Microsoft as well.

ASP.NET MVC 3

View more presentations from Buu Nguyen.
Download code demo here.

Me

Me

Me

Magnus Stråle

Keynote

ASP.NET MVC 2 Cookbook

February 15th, 2011 Buu Nguyen 1 comment

One of the books of which I was the technical reviewer was published: ASP.NET MVC 2 Cookbook. Access to the link to find out more about the book.

aspnetmvccookbook

Categories: .NET Tags: ,

C# 4.0 Dynamic Binding at BarCamp Saigon 2010

December 13th, 2010 Buu Nguyen No comments

I hugely enjoyed BarCamp Saigon 2010 which took place yesterday at RMIT University.  Learned a lot of things and met great people.

Below is the slidedeck I used for my session.  Source code is here.

ASP.NET MVC 2.0 Training

November 1st, 2010 Buu Nguyen 2 comments

Last week, I conducted a training for customers of Microsoft about ASP.NET MVC 2. The training took place at Microsoft’s office in Ho Chi Minh city. The slides used for the training is below.

Handle all uncaught exceptions thrown when using Task Parallel Library (take 2)

May 7th, 2010 Buu Nguyen 3 comments

A couple of days ago I posted a solution to handle all uncaught exceptions when using TPL. I’ve found a better way to do that, making use of task continuation. The class ThreadFactory below exposes the Error event which can be subscribed by a top-level handler and provides methods to start a task attached with proper continuation.

internal class ThreadFactory
{
    public delegate void TaskError(Task task, Exception error);

    public static readonly ThreadFactory Instance = new ThreadFactory();

    private ThreadFactory() {}

    public event TaskError Error;

    public void InvokeError(Task task, Exception error)
    {
        TaskError handler = Error;
        if (handler != null) handler(task, error);
    }

    public void Start(Action action)
    {
        var task = new Task(action);
        Start(task);
    }

    public void Start(Action action, TaskCreationOptions options)
    {
        var task = new Task(action, options);
        Start(task);
    }

    private void Start(Task task)
    {
        task.ContinueWith(t => InvokeError(t, t.Exception.InnerException),
                            TaskContinuationOptions.OnlyOnFaulted |
                            TaskContinuationOptions.ExecuteSynchronously);
        task.Start();
    }
}
Categories: .NET Tags: , , ,

Handle all uncaught exceptions thrown when using Task Parallel Library

May 4th, 2010 Buu Nguyen 2 comments

I’m using the TPL (Task Parallel Library) in .NET 4.0. I want to be able to centralize the handling logic of all unhandled exceptions by using the Thread.GetDomain().UnhandledException event. However, in my application, the event is never fired for threads started with TPL code, e.g. Task.Factory.StartNew(...). The event is indeed fired if I use something like new Thread(threadStart).Start().

This MSDN article suggests to use Task#Wait() to catch the AggregateException when working with TPL, but that is not I want because it is not “centralized” enough a mechanism.

I’ve posted the above issue (verbatim) in this StackOverflow question but didn’t receive any response. So, I had to roll out some custom code to do that. Below is the description of the solution for those who are interested.

The general idea is pretty simple. First, we need to know all the currently executed tasks. Since there’s no built-in way to know which tasks are being executed, we need to intercept the task addition. The class TaskFactoryWrapper below does just that.

static class TaskFactoryWrapper
{
    public static void Start(Action action)
    {
        var task = new Task(action);
        TaskErrorWatcher.Instance.AddTask(task);
        task.Start();
    }

    public static void Start(Action action, TaskCreationOptions options)
    {
        var task = new Task(action, options);
        TaskErrorWatcher.Instance.AddTask(task);
        task.Start();
    }
}

Inside the class TaskErrorWatcher, there’s a worker thread which periodically checks each task to see if the latter already finishes, is canceled or causes an error. If it causes an error, the worker thread trigger the error event.

class TaskErrorWatcher
{
    public delegate void TaskError(Task task, Exception error);
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    private static readonly TaskErrorWatcher @Instance = new TaskErrorWatcher();

    public static TaskErrorWatcher Instance
    {
        get
        {
            return @Instance;
        }
    }

    private TaskErrorWatcher()
    {
        Task.Factory.StartNew(Monitor);
    }

    public event TaskError Error;

    public void InvokeError(Task task, Exception error)
    {
        TaskError handler = Error;
        if (handler != null) handler(task, error);
    }

    private readonly List<Task> tasks = new List<Task>();
    private readonly object syncLock = new object();

    public void AddTask(Task task)
    {
        lock (syncLock)
        {
            tasks.Add(task);
        }
    }

    private void Monitor()
    {
        Utils.Interval(3000, () =>
                                {
                                    lock (syncLock)
                                    {
                                        for (int i = tasks.Count - 1; i >= 0; i--)
                                        {
                                            var task = tasks[i];
                                            if (task.IsFaulted)
                                            {
                                                InvokeError(task, task.Exception);
                                            }
                                            if (task.IsCanceled || task.IsCompleted)
                                            {
                                                tasks.RemoveAt(i);
                                            }
                                        }
                                    }
                                },
                                ex => Log.Error(ex));
    }
}

Utils.Interval is simply a helper method which repeatedly executes an action when the specified interval has elapsed.

public static void Interval(long interval, Action action, Action<Exception> errorHandler = null)
{
    var watch = Stopwatch.StartNew();
    while (true)
    {
        try
        {
            action();
            watch.Stop();
            Thread.Sleep((int)Math.Max(0, interval - watch.ElapsedMilliseconds));
            watch.Restart();
        }
        catch (Exception ex)
        {
            if (errorHandler == null)
                throw;
            errorHandler(ex);
        }
    }
}

Now, in the top level code (e.g. inside Main()), just subscribe to the Error event of TaskErrorWatcher and do whatever you want to do in case of error, e.g.

TaskErrorWatcher.Instance.Error += (_, error) => HandleError(error);

That’s it. Hope it helps!

Speaking at the Visual Studio.NET 2010 Launch in Hanoi

April 19th, 2010 Buu Nguyen 9 comments

Last Tuesday, I spoke at the Visual Studio.NET 2010 launch in Hanoi. It was a big launch with about 400 attendees. I hosted 2 sessions, one about ASP.NET 4.0 (including web forms, MVC, AJAX and dynamic data) and another about C# 4.0 and PLINQ. On the following day, I spoke at Microsoft Vietnam’s office about building scalable .NET web applications to about 40 .NET architects and developers in Hanoi.

In general, it was a great trip to Hanoi and I had a lot of fun speaking and meeting many people there. The sessions by other speakers were really great. I was particular impressed with what Silverlight 4.0 and SharePoint 2010 had to offer.

You can find the slides and sample code I used for these sessions below.

Sample code can be downloaded here

Some pictures taken (click to view larger images)

VS.NET 2010 Launch Event

Tea Break during the VS.NET 2010 Launch

.NET Web Scalability Presentation at Microsoft Vietnam's Office

Tea Break during the Scalability Presentation

I’m a Microsoft MVP!

April 2nd, 2010 Buu Nguyen 12 comments

An email from Microsoft, starting with “Congratulations! We are pleased to present you with the 2010 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. We appreciate your outstanding contributions in ASP/ASP.NET technical communities during the past year.” has made my day.

It’s official, I am a Microsoft MVP! (Click here to see my profile.) One can’t help feeling honored knowing that he is one of the selected 4,000 in 100 million technical community participants worldwide and among the 12 MVPs in Vietnam. I am honored.

My sincere thanks to the employees at Microsoft who nominated and joined me in many community projects during the past year! I truly enjoy working with you and I am looking forwards to future projects.

MVP-h-550x222

ASP.NET MVC Validation Library 1.3 Release

March 31st, 2010 Buu Nguyen No comments

This is truly a month of release. In fact, I released Fasterflect 2.0 and Combres 2.0 within the past 10 days. And now, it’s ASP.NET MVC Validation Library 1.3. It’s not like I suddenly have all the time in the world to push the releases of these libraries. Instead, code gets accumulated over the last 3-4 months until they happen to be done at the same time. Anyway, a couple of notes about this release of ASP.NET MVC Validation Library.

First and foremost, if you are using ASP.NET MVC 2.0, then you can stop reading. This library is not for you. Really, ASP.NET MVC 2.0 comes with a highly robust and flexible validation functionality built-in already, so there’s no reason to look for a 3rd-party library which has nothing more to offer. That said, if you are still stuck with some ASP.NET MVC 1.0 legacy apps, like me, then my library can be useful.

If you are not familiar with this library, please refer to this originally introductory post to learn how to use it. Because I haven’t taken the time to posted about the all of changes since the release mentioned in that post, I’m doing it now.

Changes in 1.3

  • Support custom server-side validation
  • Support remote validation (integrated with jQuery Validate’s remote method)
  • The design of validation attributes is improved to make addition of new validation attribute much more intuitive

Changes in 1.2

  • Support RegularExpressionValidatorAttribute (use the property ClientFunctionName to specify the corresponding jQuery Validate’s function)
  • Allow specify a custom form validation function (i.e. instead of the default $(formName).validate())
  • Support PropertyComparisonValidatorAttribute (equal operator only)
  • EntityValidationException adds constructor to accept custom key-value pair (i.e. useful for non-model/custom validations)
  • Support specifying custom ready function (i.e. useful when using a 3rd-party script loader, like Google’s, to load jQuery)
  • Support specifying custom client ID (in case the client-side ID is different from the client-side name (used for posted value)

Changes in 1.1

  • Eliminate the need to specify type parameter when invoking Validate() method
  • Allow users to specify prefix when populating model state with error messages

That’s it for the post. That’s it? Yes, that’s it. The library is ridiculously easy to use, so you don’t need another 10 pages of tutorial here. Instead, the old introductory article, together with the above change notices, should be enough for you to start working with the library. Okay, it might not be that easy. Don’t worry yet, the download of ASP.NET MVC Validation Library comes with CHM API documentation and a sample application that uses almost all features of the library. So, download and explore the library yourself. And post here if you have any comment or feedback. Enjoy!

Combres 2.0 Release

March 30th, 2010 Buu Nguyen 3 comments

After a couple of months working on and off on this project, I was finally able to release the next major version of Combres, a .NET library which automates the application of many website optimization techniques in ASP.NET MVC or ASP.NET Web Forms applications.

Access to this CodePlex page to download Combres 2.0, together with source code, CHM API documentation, sample applications and config files.

For a full introduction to Combres 2.0, refer to this Code Project article.

Enjoy the improved performance!