Zaki Mirza’s Blog

Icon

… About software and beyond!

Late night Bloggin’: Refactoring myself

Just been going through re-organizing my blog. Reading articles by Jakob Neilsen (Useability Guru) has pretty much changed my thoughts about blogging, how to percieve it and how to go on about doing it. He is right in his articles (here, especially) that the web really needs proper, well formed, well thought and specialised content rather than 2 second rants about something you found annoying or loved like Scoble’s over obsession with everything iPhone and everything about blogging. So i was here, seeing my categories list and I see just too many categories with single entries. I have narrowed down the categories to the following major ones:

Blog Log: My category for stuff I write about others’s blogs. Like this one.
General: For general talks. Like this one, again.
Code and Programming: Code talk and snippets.
Design: Problem (and software) design articles/comments.
Late Night Blog: Quick rants about something i found really useful.
Troubleshooter: For articles related to troubleshooting problems others found and approached me for, and my own fixes.

Apart from these major ones, there are short categories for Linux, MS Windows, MS Visual Studio, OpenGL, C++, and .Net.

So comming back to the topic “refactoring myself”. If you havn’t been studying refactoring then i highly recommend that you do. It will add up to your skillset of software engineering. I really can’t go deep into the topic right now (and how useful a skill it is) here in this post, nor do I have many cool links to hookup here for refactoring but I know you know your way to google. (Just for information, one of my favourite shortkut keys in Visual Studio 2005 C# workspace is the F2. It renames all the instances of the variable/function/etc currently my cursor is at). There are a lot of features in VS2k5 to explore for refactoring for beginners. One of the great articles i read today is the article at One more pointless blog called Refactor yourself. Eddie approaches the refactoring phenomenon from a totally different angle but does it very beautifully. A must read for anyone, not just software specialists.

As for the weekend, its really not very intresting. I’m really intrested in taking care of my new house so been searching some proper toolkits to have here, here and here. After an hour long searching in google I found some nice links.

Reading my news feeds is getting to be like life to me. I’v just been reading Ali Eteraz recent articles. One i found fairly intresting and thought provoking is the Geneology of prostrating (Muslim silence since 9/11).  Apart from all the MI and ISI covert battle shit being related to the Lal Masjid issue by my uncles and their wives, Ali Eteraz has his own views on its end here. I find a lot of consonance with his school of thought. Keep it up Ali.

Recent entries on my reader include A list apart, some Adobe blogs, and The News feed(which i dont read at all, i just added it because of the lal masjid issue. Did I say on my blog that I literally hate reading newspaper news, especially here. They never fail to disappoint me… its all just about deaths and shit).

A GREAT news I recently heard is that Rico Mariani (Someone i really lookup to meeting in this life) has finally taken charge as the Chief Architect of Visual Studio. I hope he’s not too late to take charge for Orcas and hope to see some great innovation comming up next Feb’ when microsoft releases Visual Studio 2008, Windows Server 2008, SQL Server 2008 etc. Way to go Rico, i wish you the best! I’m so happy, i wonder why. hehe!

So much for the latenight blog, im hoping to write a few articles this summer on my blog. Lets when I get the click.

Filed under: Blog Log, general, Late Night Blog, , , ,

Intro to Application Domains

.Net framework introduces a new concept called application domains that provides code isolation within a process enviornment. These domains contain objects/assemblies/threads that can be instantiated at runtime and unloaded at runtime. This allows for a more stable application which also providing a mechanism to load/unload plugable components without having to compromise application runtime. (or the runtime host, as these kind of applications that load other applications is called).

The AppDomain class is used to create, load and work with application domains in .Net framework. I setout to work on a project that requires loading assemblies and creating local/remote objects at runtime. These objects (also called agents..yes the DistributedComuting course project) are autonomous. they just require an execution enviornment and they will work themself. They can create threads to carry out the work they are programmed to work on. A single thread at application level is supposed to deal with these agents. These can be cloned, migrated , suspended and/or destroyed.

The problem arised when loading multi-threaded agents inside my application at runtime. An agent can create multiple threads and the runtime has no information about which threads were created by which agent. Unloading the agent normally (using the dispose function which shuts off the threads this agent created smartly) is an ideal way of doing this. (since the agent itself knows the threads it has made and can dispose them). But if there is some kind of mishap during this process (or agent falls sick, or well..anything), all threads might not get destroyed. The object will be removed from the memory leaving the poor threads confused hence comes down our application crashing. There can be ways to create a single thread for an agent at my application level and make sure that all the rest of the threads are its children and die when this thread dies/suspends etc (Thats hard to achieve and a lot of code will have to be written just to tackle this problem). Also unfortunately its not possible to create a parent/child relationship between managed (or for that matter unmanged) threads. Infact Windows does not have the facility of parent/child relationship of threads(though it does for processes. The reasons are out of scope of this page. Refer to Chapter 6 of Programming Application for Microsoft Windows by Jeffery ritcher for some internals on threading under windows.) Also, considering the suspension issue of a process, since windows does not schedule processes but it schedules threads, to suspend a procesd requires suspending all the contained threads (naive approach). In our problem, we might need to suspend a single Agent (remember agents can create multiple threads). Unless we have an architecture to do so, it will be hard to know which thread is working for which agent. One way is to create a seperate process for each agent, but the overhead of creating/destroying processes, suspending processes etc is a lot under windows though in unix enviornment that might be a feasible idea. Infact thats how its carried out in a lot of of the unix applications. Again, that might create zombie issues, issues with interprocess communication etc. But that, of course is the best alternative.). Also, in a server envionrment (atleast in windows) running applications (in our case, Agents) under a single process is more scalable.

In .Net, Application Domains solves this problem (and hence this article). MSDN introduces Application Domains as follows :

“Application domains provide a more secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.”

Application domains are usually used by runtime hosts such as Internet explorer which load 3rd party plugins and addon applications within their process. IE creates application domains for each addon and when one fails, it (well…supposedly) does not bring down the whole process. (unless of course of the glitch happned in the IE core itself which is mostly the case).

Enough introduction to Application domains, lets come to the point. We need to create objects from the loaded assembly. These objects can create multiple threads and we need a way to isolate these threads from other objects’ threads within the same application so that they dont crash the application. Also we need to track which objects created which threads. So we create a seperate application domain for each of these objects. These application domains will serve the threads. We will load/unload these domains (consequently loading/unloading the objects and the threads they have created).

Starting off with the object itself,  I have a simple object that creates a thread and within that thread keeps printing which application domain it belongs to. (“Say my name!.. Say my name!!!….”). :p simple enough? He’re the code. Its not one of the best codes, but oh well bear with me.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ClassLibrary1
{
public class MyClass: MarshalByRefObject
{
public MyClass(){}
public void ThreadFunc()
{
while (true)
{
Thread.Sleep(500);
Console.WriteLine(Thread.GetDomain().FriendlyName);
}
}
public void MyFunc()
{
Thread t = new Thread(new ThreadStart(ThreadFunc));
t.Start();
}
}
}

We compile this code to a dll called classLibrary1.dll. Now in my application ill load this dll using reflection. ill create two application domains and create two objects of this class in both application domains, shut down one application domain and we’ll see waht happens.

using System;
using ClassLibrary1;
using System.Threading;
using System.Reflection;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
AppDomain appDomain1 = AppDomain.CreateDomain(“ad1”);
AppDomain appDomain2 = AppDomain.CreateDomain(“ad2”);

Assembly asm = Assembly.LoadFrom(“ClassLibrary1.dll”);
Type[] types = asm.GetTypes();

MyClass mc = (MyClass)appDomain1.CreateInstanceAndUnwrap(asm.FullName,types[0].FullName);
mc.MyFunc();

MyClass mc2 = (MyClass)appDomain2.CreateInstanceAndUnwrap(asm.FullName, types[1].FullName);
mc2.MyFunc();

Thread.Sleep(2000);
AppDomain.Unload(appDomain1);
Console.ReadLine();
}
}
}

As you can see, the objects created within one Application domains create threads in that application domain and do not interfere with the other application domains. This is a very* simple program to show how application domains can be created and how threads work under application domains. We can improve on this furtur by creating a dictionary of objects and their application domains to keep track of which object instance is in which application domain etc. But i guess this will get you going. Ill blog soon about how to unload  faulty objects from the application without having to screw up the application itself. :p

Filed under: .net, Code, , , , ,

ASP.Net Misconfiguration Solution

Many time when creating ASP.Net Application or ASP.Net  XML Web services people get into error reports e.g. Server Application Error, Authentication error in web.config file and more. I can’t really list all the errors that lie under this solution, but most problems are rooted from this very issue. (Im not really a professional solution provider or a troubleshooter, this post happens to come from my own experience. )

The issue is about multiple versions of ASP.Net installed on you machine. For example, you were working with Visual Studio 2003 and then switched to Visual studio 2005 or vice verse. You installed .Net platform 2.0 and even 3.0. Asp.net, usually, is left unconfigured during this configuration and if your very unfortunate, get misconfigured. One of the major root of this problem is, when people install IIS after they install the .Net Framework, or uninstall and reinstall it. If you are having problems with ASP.Net configurations, try the following method.

To solve this problem, following the following simple steps:

navigate to you Windows installation folder. Ill assume its C:\Windows\

navigate to Microsoft.Net\Framework\[version] (depending on which version of asp.net you want with IIS.)

You will see that there are many utilities in that folder. (in my case, it is C:\Windows\Microsoft.NET\Framework\v2.0.50727). The utility that we require is aspnet_iisreg.exe. (for a more technical introduction to this utility, click here). This utility reconfigures this version of asp.net for IIS. This utility requires command line arguments. Here’s what microsoft has to say about it:

When multiple versions of ASP.NET are installed on a computer, ASP.NET is said to be running side-by-side. In this setup, Internet Information Services (IIS) needs to know which version of the ASP.NET ISAPI (aspnet_isapi.dll) should process a page in an ASP.NET application. The ASP.NET ISAPI version that is associated with an ASP.NET application determines which version of the CLR is used for the application. An ASP.NET application is associated with an ASP.NET ISAPI version using a script map in IIS. To simplify the configuration process for an ASP.NET application, each version of ASP.NET comes with a linked version of Aspnet_regiis.exe.

Goto start menu and click Run and type “cmd”. A terminal window should open. Drag this aspnet_iisreg.exe file onto the terminal window. The terminal window will now be showing this file’s name and a prompt after it. type -i after it. Basically we need to run the command “aspnet_regiis.exe -i”. This will reinstall this version of asp.net for iis. It will take some time to configure IIS.

If the above solution does not provide the adequate result, or creates any problems feel free to ask.

A very good online magazine to read for IT professionals is the microsoft TechNet. For more information about configuring asp.net versions for IIS refer to this article on TechNet. For more information about .Net Framework tools check this out.

I hope this helps some poor souls out there who are too exhausted having to post on forums and searching on google.

Filed under: .net, Troubleshooter, , , , , , , ,

Blog Stats

  • 104,277 landed here so far...
July 2017
M T W T F S S
« Jan    
 12
3456789
10111213141516
17181920212223
24252627282930
31  

tweet

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

RSS Google Shared Items

  • An error has occurred; the feed is probably down. Try again later.

RSS Google Reader Starred Items

  • An error has occurred; the feed is probably down. Try again later.

Top Clicks

  • None