Saturday, February 8, 2014

Using custom roles with ASP.Net Identity v2-alpha

When creating a new ASP.Net MVC 5 site we are given the option to choose the type of authentication we want for the site. If we choose "Individual accounts" the site is configured to use forms authentication with an Entity Framework backend. This backed uses ASP.Net Identity, which is the replacement for ASP.NEt Membership. In a test project I have, I decided I wanted to use the pre-release version of ASP.Net Identity, which, at the moment, is version 2-alpha. To upgrade to this version, we only have to upgrade the package from Nuget. We open the Nuget package manager and we select we want packages from the pre-release channel. Then we can update to the pre-release packages.

Wednesday, September 11, 2013

HTML 5 data- attributes

Sometimes, when programming in Javascript you need some data associated to HTML elements. Since the introduction of HTML5 we can use the data- custom attributes. With this attributes you can associate data to HTML elements and access it via javascript:

<div data-code="jo8389">Hello</div>

Using JQuery it's really easy to access those attributes:
$("div[data-code=jo8389]").text("Good bye!"); //selector
$("div").attr("data-code","1777"); // changing the attribute

Friday, September 6, 2013

Other ways to update the UI from another thread

When programming in WPF (or any other desktop UI framework) you don't want to block the UI thread when running long tasks. Apart from what we saw in the previous post, there are other ways to do it if you are using previous versions of .NET.
For example, if you are using .NET 4 you can use the TPL:

Working=Visibility.Visible;
var tasca=Task.Run(()=>LongRunningTask());
tasca.ContinueWith(t=>
    {
        var exception=t.Exception;
        LogAndShowException(exception);
    },CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,TaskScheduler.FromCurrentSynchronizationContext());
tasca.ContinueWith(t=>
    {
        var result=t.Result;
        UpdateUI(result);
    },CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion,TaskScheduler.FromCurrentSynchronizationContext());
tasca.ContinueWith(t=>
    {
        Working=Visibility.Collapsed;
    },TaskScheduler.FromCurrentSychronizationContext());

Here we make two continuation tasks: one to deal with faulted states and the other to deal with the correct state. The trick to run these tasks on the UI thread is the TaskScheduler.FromCurrentSynchronizationContext(). The last continuation task runs always to set the Working property to Collapsed.

Another option for older versions of .NET is to use a BackgroundWorker:

BackgroundWorker bw=new BackgroundWorker();
bw.DoWork+=bw_DoWork;
bw.OnCompleted+=bw_RunWorkerCompleted;
Working=Visibility.Visible;
bw.RunWorkerAsync();

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    LongRunningTask();
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.Error!=null){
        LogAndShowException(e.Error);
    }
    else{
        var.result=e.Result;
        UpdateUI(result);
    }
    Working=Visibility.Collapsed;
}

This are some ways to run tasks in the background when using WPF. But the easiest way is to use async/await if you can afford to use .NET 4.5.

Thursday, September 5, 2013

Updating the UI thread in .NET 4.5

Recently, I had to create a WPF app for my company. In this app there were some long-running operations that had to update the UI thread when they were completed. Since I'm using MVVM and Prism, I usually start my tasks on the ViewModel and I found that using the TPL and async/await is the easiest and clearest way to synchronize my tasks:

Running=Visibility.Visible; //This is to show a hint on the view that the task is running.
var tasca=Task.Run(()=>LongRunningTask());
try{
    var result=await tasca;
    UpdateUI(result);
}
catch(Exception e){
    MessageBox.Show("Error running task");
    _log.ErrorException("Error running task",e);
}
finally{
    Running=Visibility.Collapsed;
}
This way, the code remains clear and we don't have to deal with ContinuationTasks or callbacks.

Monday, February 25, 2013

Viatge a Costa Rica (2)

Ja hem tornat del viatge a Costa Rica. El viatge va ser una passada. Varem anar amb els dos menuts, llogant 2 cotxos per moure'ns al nostre aire. La veritat és que vaig disfrutar.

Vos contaré un poc el que varem fer. Els primers dies varem estar al Parc Nacional de Tortuguero. Tot i no estar en l'època d'arribada de les tortugues varem veure molts animals.
Cocodril al riu

Saturday, January 26, 2013

Objectiu per anar de viatge

Per anar de viatge he estat cercant un objectiu zoom polivalent. Al final em vaig decantar per un Tamron B008E 18-270/3.5-5.6 Di II PZD VC:

L'he estat provant aquests dies i la veritat és que va molt bé. La comoditat de passar de un gran angular (18mm) a un tele (270mm) sense haver de canviar d'objectiu és fantàstica. Li he trobat algunes  pegues: l'objectiu no és molt lluminós i és un poc lent per enfocar. Tot i això, els avantatges superen amplament els inconvenients. Un compra molt recomanable.