Monday, 19 January 2009

Seirei no Moribito - Guardian of the Sacred Spirit

the main characters in the story
Another pearl in the anime pond. One just has to dive and grab this fairy tale and watch it. Seirei no Moribito is the story of a female bodyguard, bound by honor and personal history to defend a child prince from the assassins sent by his father, the emperor. The anime is not really a fighting anime, though, no one battles for the sake of battle. More than that, the "ona yojimbo" has vowed to save lives, not take them, so she never kills anyone. All characters in the story are clearly drawn up, with their own particularities and personal motivations to do things and in the end, there is even a eco green moral. :)

My advice it to watch it, as it is both a beautiful story, as well as a complex one, fit for both adults and young children. Only 26 episodes, but it is worth it. I would really like to see animes for the rest of the books in the series. You can also read the first two chapters of the manga at Mangafox.

Friday, 16 January 2009

Including external javascript on async postbacks

Update: it has come to my attention that these kind of errors appear only when debug="true" in the web config. In production sites it might work regardless.

You want to programatically load a js in a user control. The user control becomes visible in the page only during async postbacks and it is hidden at page load. So you want to use some method of loading the external javascript file during that asynchronous UpdatePanel magic.

Use ScriptManager.RegisterClientScriptInclude. There is a catch, though. For each script that you want to include asynchronously, you must end it with
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
else the Ajax.Net script loader will throw an error like: Microsoft JScript runtime error: Sys.ScriptLoadFailedException:
The script '...' failed to load. Check for:
Inaccessible path. Script errors. (IE) Enable 'Display a notification about every script error' under advanced settings.
Missing call to Sys.Application.notifyScriptLoaded().
.

For the aspx version, use the ScriptManagerProxy control:
    <asp:ScriptManagerProxy runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/myScript.js" NotifyScriptLoaded="true" />
</Scripts>
</asp:ScriptManagerProxy>

Thursday, 15 January 2009

Dennou Coil - a pretty fun anime.


Dennou Coil is a cross between Ghost in the Shell and Tonari no Totoro. How can it be possible? Simple! First remove all that usual Japanese obsession with boys in high schools (you know, everything happends to some high school children, usually boys, all in the same class, maybe some opinionated women, they fight, they grow in "level", etc). Instead use girls in elementary school. Then move it in some future world where people merged the real world with the virtual, and add cute creatures like from Tonari no Totoro (and the girls) in the virtual world! Yatta! :)

Anyway, getting more serious now, the anime is really nice. The Augmented Reality concept is pretty cool and (even if towards the end it gets a little spiritual and unnecessarily action like) stays pretty consistent. Imagine wearing clear glasses that add to your usual display of reality information like virtual pets, virtual laptops, keyboards and displays, maps of your current location and so on.

The children are all drawn towards a strange phenomenon that adults seem to regard as fantasy, called Dennou Coil, where the consciousness of people gets transferred to a virtual nether world via the quantum circuits in the glasses.

Towards the end, adults try to confiscate the glasses from the children and, after a hearty discourse about how real things can be touched, I thought the whole cool idea of the series would be perverted towards some social goal of keeping children "real", but it was not so. You will have to watch the series to know what I mean :)

Bottom line: pretty interesting and captivating. A little too childish, but without resorting to silly behaviour or self referential comedy that I see so much lately in animes. The anime has only 26 episodes, with little chance of more. The manga itself is complete as well. Couldn't find the manga free online. If you know where to find it, leave me a comment.

Recommendation: watch it!

Wednesday, 14 January 2009

Session state is not available in the current context

So you did some work in an ASP.Net web site and then you decided to or to add the Ajax extensions to it or switch to ASP.Net 3.5 or whatever and suddenly you get Ajax errors like "Sys is undefined" or you don't see images and the css files are not loaded. And after googling like crazy and finding a zillion possible solutions you decide to enter in the browser address bar the url of the offending image, css file or even ScriptResource.axd files containing the ajax javascript and you see a beautiful ASP.Net error page displaying "Session state is not available in this context.". Huh?

There are some reasons why this might happen, but let's examine the one that actually prompted me to write the article. Someone made a change in global.asax, trying to work with Session there, more precisely in the Application_AcquireRequestState event. The error was returned by the Session property of the HttpApplication object, the one that the global.asax file represents! In fact, this error can only be thrown from there in the current ASP.Net implementations.

First mistake: there was a Session property available in the HttpApplication object and it was immediately assumed that it is the same as Page.Session or HttpContext.Current.Session. It is about the same, except it throws an error if the underlying Session object is null.

Ok, but why is Session null? You are a smart .Net developer and you know that the Session should be available in that event. Any documentation says so! Yes, but it applies to your page, not to all the IHttpHandler implementations, like ScriptResource.axd! Also, the css and image problems occured for me only when opening the site in Cassini, not in IIS, so maybe it opens separate threads to load those resources and ignores the session or something similar, at least at that level.

Well, how does one fix it? Adding in global.asax a condition on Session not being null throws the same error, so you have to add it on HttpContext.Current.Session. In other words:
if (HttpContext.Current.Session!=null) //do stuff with sessions

Maybe this problem occurs in other places as well, but only the Session property of the HttpApplication will throw the "Session state is not available in this context." error.

The end of the world is (finally) coming!

The previous post was the 500th post in this blog! And I haven't even noticed. Let's celebrate late with news about the end of the world!

How bored can I be? I've read this article about the asteroid Apophis striking the Earth in 2029 or 2036 and my first thought was "oh man! Why so late?". I was already calculating how unfair it is that I would have to live to be 59 years old before the world ends. So I have to cope with it until then, then, close to my retirement, the pleasant moment when I get money from the state and do nothing while my mind slowly rots away, it all goes away. Then it hit me: I am a complete idiot! What was I thinking? Sheesh!

Anyway, here is a cute animation made by a guy on youtube. I personally prefer the first one, the one that is only graphics and no realism. Again, who wants real? But it wouldn't be in the spirit of the blog :) So watch the second one, the one made to satisfy the critics.

Update: the guy REMOVED (who does that?!) the videos from YouTube. I am putting another ridiculously grand video of a possible asteroid (although this seems more like a dwarf planet :) ) destruction of the Earth to satisfy the ones hungry for obliteration.



Wow, think how many blog posts I would have written until 2036!

Monday, 12 January 2009

Classes vs. Structs

I took a test recently, one of those asking ridiculous C# syntax questions rather than trying to figure out if your brain works, but anyway, I got stuck at a question about structs and classes. What is the difference between them?

Credit must be given where it is due, I took the info from a dotnetspider article, by Dhyanchandh A.V. , who organised the answer to my question very well:
  • Classes are reference types and structs are value types. i.e. one cannot assign null to a struct
  • Classes need to be instantiated with the new keyword, structs need only be declared
  • When one instantiates a class, it will be allocated on the heap.When one instantiates a struct, it gets created on the stack
  • One works with the reference to a class, but directly with the struct
  • When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference
  • One cannot have instance Field initializers in structs
  • Classes can have explicit parameterless constructors. Structs cannot
  • Classes support inheritance. But there is no inheritance for structs, except structs can implement interfaces
  • Since struct does not support inheritance, the access modifier of a member of a struct cannot be protected or protected internal
  • It is not mandatory to initialize all fields inside the constructor of a class. All the fields of a struct must be fully initialized inside the constructor
  • A class can declare a destructor, while a struct cannot
.

What is the purpose of a struct, then? It seems to be only a primitive type of class. Well, it serves purposes of backward compatibility with C. Many C functions (and thus COM libraries) use structs as parameters. Also, think of the struct as a logical template over a memory location. One could use the same memory space of an Int32 under a struct { Int16 lo,hi }. Coming from an older and obsolete age, I sometimes feel the need to just read the memory space of a variable and be done with it. Serialization? Puh-lease! just grab that baby's memory and slap it over someone else's space! :)

OutOfMemoryException when working with StringBuilders and strings

One of our sites started exibiting some strange behaviour when adding a lot of strings to a StringBuilder. The error was intermittent (best kind there is) and the trace showed the error originating from the StringBuilder class and then from within the String class itself (from an external method).

On the web other people noticed this and one possible explanation was that strings need to be contiguous and thus they cannot grow bigger than the largest free contiguous memory block. Remember defragmentation? Heh.

In order to test this, I added a letter to a StringBuilder, then added itself to it for a few times to see where it breaks. It broke at a size of 2^27 (134,217,728 = 0x8000000)! At first I thought it was a StringBuilder bug, but getting sb.ToString() then adding to it a letter resulted in an error. Then I thought maybe it is a default size that, for whatever reason, is considered the maximum string size. That would suck. I don't usually need 134Mb of string, but what if I do? I have 2.5Gb of memory in this computer. But no, people reported the same problem but with other powers of 2, like 29 and on a Vista computer I got 2^28.

Is it from the fragmentation of memory? Right now I have only 1.8Gb in use. I rather doubt that in the 700Mb of space left there is only one 134Mb contiguous memory block. But I cannot prove it one way or another. My guess is that there is another mechanism that actually interferes with this and effectively limits the maximum string size.

But what does that mean, anyway? For practical purposes it means that you have to plan not to have strings that big in your applications. Creating a custom string builder might work, but it would have to use some other methods than strings.

I have tried the same thing with byte arrays. While declaring a byte array of 2^28 was possible (probably also because a string uses Unicode characters internally and thus takes twice the memory) writing it to a MemoryStream resulted in an error.

So watch these things out, try to always keep single variable blocks to manageable sizes.