Thursday, 11 February 2010

Some music: Florence (and the Machine), Sia (Furler) and Marina (and the Diamonds)

I haven't posted a music entry in quite some time, but this will compensate. Here are three female singers and some very good songs:

Cosmic Love from Florence and the Machine. You might also want to listen to The Drumming Song



The Girl you Lost to Cocaine from Sia. You might also want to listen to Buttons, with a fun video.



Hollywood from Marina and the Diamonds. She is a very prolific song writer and I like many of her songs. Not to mention she has a voice I love and she's cute as well. You might also want to listen to Mowgli's Road

Tuesday, 9 February 2010

Internal error: internal WPF code tried to reactivate a BindingExpression that was already marked as detached.

I started my WPF application and I got this weird exception: Internal error: internal WPF code tried to reactivate a BindingExpression that was already marked as detached. At the time of this entry, googling for this yields about 3 results, only one saying something about how internal WPF errors are usually multithreaded exceptions and, if you have it, try to remove IsAsync="True".

Needless to say, I didn't have that. The StackTrace of the exception was showing me, kind of obliquely, that the error was coming from a binding, but not why. Luckily, I looked in the Visual Studio Output window while getting the error. And there it was! A completely different error, this time telling me what the problem was.

Incidently, the error was caused by a style that had a BasedOn="{x:Type Something}" property setting. I know it is supposed to work, but in this case, it didn't, and it needed the more conservative yet safer BasedOn="{StaticResource {x:Type Something}}".

The cause of the error is only a detail, though, as the lesson here is to always look in the Output window while debugging WPF applications, even if you have an exception thrown. The messages there are more verbose and are actually telling you what bindings do, not what internal code is executed.

Monday, 8 February 2010

Loading the Session from a known SessionID

There are cases when pages need to use the same session, even if they are started from different contexts. One example is when trying to open a new window from within a WebBrowser control, or maybe issues with the ReportViewer control, or even some browsers who choose to open frames and new windows on different threads, like FireFox did for me a while ago. One might even imagine a situation where two different browsers open the same site and you want to use the same session. You have a SessionID, you are on the same server, so you should be able to use the session you want!

Here is how you do it:

var sessionID=(string)Request.QueryString["SessionIdentifier"];
if (Request.Cookies["ASP.NET_SessionId"] == null
&& sessionID != null)
{
Request.Cookies.Add(new HttpCookie("ASP.NET_SessionId", sessionID);
}

This piece of code must be added in the Global.asax.cs file (create a Global.asax file for your web site if you don't have one) in the void Global_PostMapRequestHandler(object sender, EventArgs e) handler and the sessionID must be given in the URL parameter SessionIdentifier.

Unfortunately you can't do it anywhere else. I've seen attempts to abandon the session in page load or page init and then do this, but it doesn't work. Basically, this post describes a horrible hack that replaces the default cookie where ASP.Net saves the SessionID value just before it is read.

As it can be a security risk, you should also add some validation logic so that the session hijacking is done only on certain pages that are likely to be opened in different application threads.

Saturday, 6 February 2010

Midnight Tides by Steven Erikson

Book cover, not very well chosenThe fifth book in the Steven Erikson's Malazan Book of the Fallen series, Midnight Tides is separated by the previous four in location, characters and, I would say, quality. We are witness to a battle between a lost enclave of the Tiste Edur and a lost enclave of The First Empire. From the perspective of the Malazans (which have no involvement at all in this book) both peoples would have been seen as ignorant savages, their conflict merely a petty squabble. The only characters we can recognize are the Crippled God, who is indirectly manipulating things, and Trull Sengar. Trull is almost the main character in the story, explaining his tortured past, although little connects this story with his freeing from the fragment of the Shadow warren in the forth book.

The end, another convergence of characters and stories and gods and magical powers, only opens avenues for further development, rather than actually explaining things. There are some interesting parts to the story, mostly the description of the Letherii culture, so much alike the Western culture today, which Erikson is criticising at every opportunity. He has similar ideas in House of Chains, but he really lets himself free in this one.

Aside from that and from the history of Trull Sengar which is surely to have an impact in the next books, the story was not really that captivating compared to previous chapters in the saga, almost like it all was a prop to describe Trull's way of thinking and to berate capitalism; like one of those TV show episodes that happen in the past so that we can understand what the character will do in the next episode that happens today. Still a good book, though.

Thursday, 4 February 2010

Tuesday, 2 February 2010

Checking if a WPF property is set or not

I had this scenario where a property was attached to objects but I wanted different default values for different objects. The code made it easy to just check if the property was set or not, then set the default myself. In order to do that, use the DependencyObject.ReadLocalValue instead of DependencyObject.GetValue and then check if the result equals DependencyProperty.UnsetValue. Like this:

return element.ReadLocalValue(MyProperty) == DependencyProperty.UnsetValue;
Of course, this can be used as an extension method for any element and/or property.

su: permission denied or the wheel of misfortune

I am not a Linux guru, but sometimes I need to use Linux systems and for that I use SSH. It is customary nowadays that one doesn't login remotely using the root account, but rather use another user, then use the command su (super user) to gain root priviledges. I've done it tens of times, most of the time checking if I can log in and then su with the new user.

Well, a few days ago I did not and, to my horror, I noticed that I couldn't use su from my new user, as a permission denied error message popped up. Plus, I had already locked the user I had previously logged in with. Add to this that the device in question had no keyboard/monitor jacks, it was remote, it only had a serial connection, my laptop did not and that the serial cable I tried to use with a borrowed desktop computer was not good enough for this damn device and you can understand the hell I was in.

Enough said, the idea is that some Linux distributions (like the ones based on BSD. Gentoo, for example) use what is known as the wheel group or the group that permits users to use su. Use usermod -G wheel myNewUser to add your user to the wheel group and always ALWAYS check if you have enough permissions for login users before you log off from root.