Sunday, February 28, 2010

Exception Handling Best Practices

 

  • Serialize exceptions, so that they are available across app domains.
  • As far as possible, let the exception be in the same assembly that throws it.
  • Use at least the 3 common constructors that Exception class provides.
  • Use pre-defined exceptions if possible instead of custom exceptions.
  • Return null instead of exception for common errors
  • Categorize exceptions in a hierarchy based on modules.
  • Include localized description string for exception.
  • Provide Exception properties for programmatic access.
  • Do not catch generic exceptions when specific exceptions can be caught.
  • Check for ErrorCode property in COMException for HResult value.
  • Make COM objects implement IErrorInfo, so that they can be access in .NET

Tuesday, February 23, 2010

Race condition and Deadlock

 

A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.

The typical solution to a race condition is to ensure that your program has exclusive rights to something while it's manipulating it, such as a file, device, object, or variable. The process of gaining an exclusive right to something is called locking.

A deadlock occurs when two threads each lock a different variable at the same time and then try to lock the variable that the other thread already locked. As a result, each thread stops executing and waits for the other thread to release the variable. Because each thread is holding the variable that the other thread wants, nothing occurs, and the threads remain deadlocked.

Deadlock Prevention

1. Remove mutual exclusion – no process should have exclusive access to a resource
2. Process should acquire all required resources before starting
3. By releasing resources after a certain amount of time
4. By assigning precedence to resources, so process an only try to acquire lower precedence resource

Managed Threading Best Practices

 

We use threading to increase responsiveness of our application and to do multiple tasks simultaneously(typically IO and CPU bound tasks).

  • Don't use Thread.Abort
    to terminate other threads. Calling Abort on another thread is akin to throwing an exception on that thread, without knowing what point that thread has reached in its processing.
  • Don't use Thread.Suspend and Thread.Resume
    to synchronize the activities of multiple threads. Do use Mutex, ManualResetEvent, AutoResetEvent, and Monitor.
  • Don't control the execution of worker threads from your main program (using events, for example).
    Instead, design your program so that worker threads are responsible for waiting until work is available, executing it, and notifying other parts of your program when finished. If your worker threads do not block, consider using thread pool threads. Monitor..::.PulseAll is useful in situations where worker threads block.
  • Don't use types as lock objects.
    That is, avoid code such as lock(typeof(X)), or the use of Monitor..::.Enter with Type objects. For a given type, there is only one instance of System..::.Type per application domain. If the type you take a lock on is public, code other than your own can take locks on it, leading to deadlocks.
  • Use caution when locking on instances, for example lock(this) in C#. If other code in your application, external to the type, takes a lock on the object, deadlocks could occur.
  • Do ensure that a thread that has entered a monitor always leaves that monitor, even if an exception occurs while the thread is in the monitor. The C# lock statement provide this behavior automatically, employing a finally block to ensure that Monitor..::.Exit is called. If you cannot ensure that Exit will be called, consider changing your design to use Mutex. A mutex is automatically released when the thread that currently owns it terminates.
  • Do use multiple threads for tasks that require different resources, and avoid assigning multiple threads to a single resource. For example, any task involving I/O benefits from having its own thread, because that thread will block during I/O operations and thus allow other threads to execute. User input is another resource that benefits from a dedicated thread. On a single-processor computer, a task that involves intensive computation coexists with user input and with tasks that involve I/O, but multiple computation-intensive tasks contend with each other.
  • Consider using methods of the Interlocked class for simple state changes,
    instead of using the lock statement. The lock statement is a good general-purpose tool, but the Interlocked class provides better performance for updates that must be atomic. Internally, it executes a single lock prefix if there is no contention. In code reviews, watch for code like that shown in the following examples.
  • Avoid the need for synchronization, if possible.
    This is especially true for heavily used code. For example, an algorithm might be adjusted to tolerate a race condition rather than eliminate it. Unnecessary synchronization decreases performance and creates the possibility of deadlocks and race conditions.
  • Make static data thread safe by default.
  • Do not make instance data thread safe by default.
    Adding locks to create thread-safe code decreases performance, increases lock contention, and creates the possibility for deadlocks to occur. In common application models, only one thread at a time executes user code, which minimizes the need for thread safety. For this reason, the .NET Framework class libraries are not thread safe by default.
  • Avoid providing static methods that alter static state.
    In common server scenarios, static state is shared across requests, which means multiple threads can execute that code at the same time. This opens up the possibility of threading bugs. Consider using a design pattern that encapsulates data into instances that are not shared across requests. Furthermore, if static data are synchronized, calls between static methods that alter state can result in deadlocks or redundant synchronization, adversely affecting performance.

Monday, November 02, 2009

Don’t write shell extensions in .net

Technorati Tags:

I was going to write a shell extension in C# (i wanted to extend the SendTo menu), but before I could finish it, I came across  this post http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/1428326d-7950-42b4-ad94-8e962124043e/

Looks like I will have to live without my custom extensions – coz I cannot write unmanaged code – or maybe I can – will definitely give it a try… lets see

Wednesday, June 10, 2009

XML Element or Attribute


elements are for data, attributes are for metadata

If data in question is a core content (problem domain) then it should be element.

If data is peripheral (intended to help the main communication) then it should be attributes.

If data has fixed format and cannot be extended should be attributes – ex Dates

If data has variable format and can be extended in future it should be element

If data is going to be read by human then put it in element

If data is going to be read by machines then put it as attribute

If data can be qualified by some attribute then it should be in element.

Friday, May 22, 2009

Putting C# attributes to use


Recently in an interview I was asked if I had worked with attributes, I had, and I told them how, I don’t know why I didn’t put it on my blog, anyways now I will do it.

What I have done is that, I have created 2 attribues [notNULL] and [notNIL] these can be applied to any function parameter, what happens is, if you pass null to a function that has the parameter decorated as [notNULL] then a ArgumentNullException is thrown and for [notNIL] ArgumentException is thrown.

To achieve this, the class which is going to use these attributes must be decorated with another attribute ([the] in our case) this attribute adds a property for itself and this property adds a sink where you get a hook and can query the method/parameter/etc and execute any business logic you want – in my case I am simply checking if the parameter has the said attributes and throw exception.

The Client Program (Program.cs)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.Runtime.Remoting.Activation;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
theClass theObject = new theClass();
theObject.operate("a",3,null);

theChild theChildObject = new theChild();
theChildObject.working("");
}
}
}

The Class (theClass.cs)

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

namespace ConsoleApplication1
{
[the]
class theClass:ContextBoundObject
{
public void operate([notNULL]string s, int a, [notNIL]string ss)
{
Console.WriteLine("theClass did its work");
}
}
[the]
class theChild : theClass
{
public void working([notNULL] string s)
{
Console.WriteLine("theChild did its work");
}
}
}

NotNULL/NotNIL Attributes (notNULL.cs)

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

namespace ConsoleApplication1
{
[AttributeUsage(AttributeTargets.Parameter)]
class notNULLAttribute:Attribute
{
public notNULLAttribute() { }

}
[AttributeUsage(AttributeTargets.Parameter)]
class notNILAttribute : Attribute
{
public notNILAttribute() { }

}
}

The Attribute (theAttribute.cs)

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Text;

namespace ConsoleApplication1
{
[AttributeUsage(AttributeTargets.Class,Inherited=true)]
class theAttribute:Attribute,IContextAttribute
{
public theAttribute()
{ }

public void GetPropertiesForNewContext(IConstructionCallMessage msg)
{
theProperty property = new theProperty();
msg.ContextProperties.Add(property);
}

public bool IsContextOK(Context ctx, IConstructionCallMessage msg)
{
return false;
}
}
}

The Property (theProperty.cs)

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace ConsoleApplication1
{
class theProperty:IContextProperty,IContributeServerContextSink
{
public theProperty() { }

public string Name { get { return "MyProperty"; } }

public bool IsNewContextOK(Context newctx) { return true; }

public void Freeze(Context newContext){}

public IMessageSink GetServerContextSink(IMessageSink nextSink)
{
return new theSink(nextSink);
}
}
}

The Sink (theSink.cs)

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
using System.Reflection;
using System.Text;

namespace ConsoleApplication1
{
class theSink:IMessageSink
{
private IMessageSink next;

public theSink(IMessageSink next) { this.next = next; }

public IMessage SyncProcessMessage(IMessage msg)
{
if (msg is IMethodMessage)
{
IMethodMessage methodMessage = msg as IMethodMessage;

MethodBase methodInfo = methodMessage.MethodBase;

ParameterInfo[] parameters = methodInfo.GetParameters();
foreach (ParameterInfo p in parameters)
{
object[] attributes = p.GetCustomAttributes(true);
foreach (Attribute a in attributes)
{
if (a.GetType() == typeof(notNULLAttribute))
{
//Console.WriteLine("found " + p.ToString() + " at position "+p.Position+" which has notnull attribute");
if (null == methodMessage.GetArg(p.Position))
{
Console.WriteLine("Error : " + p.ToString() + " cannot be null in "+methodInfo.Name);
throw new ArgumentNullException("Not null attribute violated");
}
}
if (a.GetType() == typeof(notNILAttribute))
{
//Console.WriteLine("found " + p.ToString() + " at position "+p.Position+" which has notnull attribute");
if(null == methodMessage.GetArg(p.Position) ||
"" == methodMessage.GetArg(p.Position).ToString())
{
Console.WriteLine("Error : " + p.ToString() + " cannot be nil in "+methodInfo.Name);
throw new ArgumentException("Not nil attribute violated");
}
}
}
}
}
IMessage replyMessage = next.SyncProcessMessage(msg);
return replyMessage;
}

public IMessageSink NextSink { get { return this.next; } }

public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
}
}
}

Wednesday, May 20, 2009

IIS, WCF apps, ports, etc


I have very little experience on Web development/IIS, and it was difficult for me to understand how self hosting was working in WCF, heres what I thought :

I start my application that is hosting the service, now when i request its metadatabehaviour through my web browser, I get the wsdl, however at this time my IIS is not running and the URL is something like http://localhost:8001/service. According to me the moment I say localhost the request should go to my IIS server and from here a response should be generated, but how was this working even when IIS was stopped, that means my application was serving the wsdl response, now how can 2 applications respond to HTTP, only one can have complete control. However I soon found out that my assumptions were wrong.

Heres what really happens, when you start a website on IIS, it is typically on port 80, and when my application hosted the service I had hosted it on 8001, and hence they work independently of each other, try starting the app on port 80 and then start IIS – it throws error, so it means there can be multiple apps that can handle HTTP, just that each handles a unique set of port numbers.