Archive

Archive for January, 2006

The Interop dream

January 31, 2006 Leave a comment
Web Services are definitely a step forward in making things work across disparate platforms. The promise is there, but even a trivial thing on how dates are handled across diparate platforms is an issue. In java datetime is a nullable type and in .NET it is not. This is enough to give you headaches. Add to it the timezones and locales another set of issues crop up. Googling lead to some solutions which i am bookmarking for furture references.
 
Categories: .NET Framework

WSDL.exe Sucks

January 30, 2006 Leave a comment
The Client side code generated by WSDL.exe(VS.NET Add Web Reference) from a WSDL file is not something which everyone will like. The classes generated for the Web Service parameters and return types have public fields instead of properties.
 
This may not be desirable if
  • You do some databinding stuff
  • Or add some validations on the accessor methods

After googling, the following tools generate a class with getter setter methods.

XSDObjectGen: This generates objects from a XML Schema. It generates property accessors backed up by public fields. It adds an XmlIgnore attribute on the generated property accessors which can be used by your application.

WSContractFirst: A Visual Studio Addin and Command Line utility to enable Contract (WSDL/XSD) first web services development.

svcutil: A Windows Communication Foundation tool. Though this generates classes targetted to be used from WCF this can be customized a bit.

WSContractFirst looks like a good solution to my requirement.

Also on the list is the WSDL tool customized from Mono by Brian Ritchie. But the download link seems to be down. Anyway WSContractFirst looks like a good option. Anything like a CodeSmith Template on this side which is more customizable will be great, and I am googling without much luck on this.

Categories: .NET Framework

Talking about How to Use Design Patterns? A Discussion with GAMMA

January 30, 2006 Leave a comment

Prakash is linking to some great stuff on Artima. A good read.

Quote

How to Use Design Patterns? A Discussion with GAMMA

Among developers, design patterns are a popular way to think about design, but what is the proper way to think about design patterns? In this interview, Erich Gamma, co-author of the landmark book, Design Patterns, talks with Bill Venners about the right way to think about and use design patterns.
 
More here….
 
Categories: Uncategorized

Talking about How to learn, write and think more clearly?

January 24, 2006 Leave a comment

 Nice entry from Prakash. It takes atleast 30 minutes though

Quote

How to learn, write and think more clearly?

It is well worth ten minutes as it tells you how to learn, write and think more clearly
 
Categories: Uncategorized

Waiting for something to happen – Manual and AutoResetEvents

January 19, 2006 2 comments
Assume we have a scenario like this. A component has to call 3 web services to get the data. Instead of calling them one by one in a serial fashion, the component calls them asynchronously. After the component has all the three sets of data, let us say that the component processes the data. It needs to know when all the three method calls complete. One way of doing this is by polling for completion of the three async calls.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        while(service1Done == false && service2Done  == false && service3Done == false) {
            Thread.Sleep(500);
        }
 
        // Do the processing
 
    }
    private bool service1Done = false;
    private bool service2Done = false;
    private bool service3Done = false;
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done == true;
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done == true;
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done == true;
    }
}
 
But polling is generally not a good solution this problem. This can be solved using ManualResetEvents. The main thread calls WaitAll with the three ManualResetEvent objects (one each for a web service call). The same code using ManualResetEvent is given below.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        WaitHandle[] handles = new WaitHandle[] { service1Done, service2Done , service3Done }
        WaitHandle.WaitAll(handles);
         
        // Do the processing
 
    }
    private ManualResetEvent service1Done = new MaunalResetEvent(false);
    private ManualResetEvent service2Done =  = new MaunalResetEvent(false);
    private ManualResetEvent service3Done =  = new MaunalResetEvent(false);
 
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done.Set();
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done.Set();
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done.Set();
    }
}
 
When a ManualResetEvent is created it is created in non-signalled state. Calling the static method WaitAll on the WaitHandle class passing the three manual reset events in non-signalled state puts the calling thread into WaitSleepJoin state. Once all the three service calls are done (and all the set methods are called) all the manual reset events are signalled now. This makes the main thread schedulable again.
 
Assume the samething happens in a loop using a auto reset event eliminates the need for calling reset.
 
public class ABusinessComponent {
    public void DoSomething() {
        // Do the statements below in a loop
     // begin loop
        // Call Web Service 1 Asyncly
        // Call Web Service 2 Asyncly
        // Call Web Service 3 Asyncly
 
        WaitHandle[] handles = new WaitHandle[] { service1Done, service2Done , service3Done }
        WaitHandle.WaitAll(handles);
         
        // Do the processing
      // end loop
 
    }
    private AutoResetEvent service1Done = new AutoResetEvent(false);
    private AutoResetEvent service2Done =  = new AutoResetEvent(false);
    private AutoResetEvent service3Done =  = new AutoResetEvent(false);
 
    public void Service1DoneCallback(IAsyncResult arResult) {
       service1Done.Set();
    }
    public void Service2DoneCallback(IAsyncResult arResult) {

       service2Done.Set();
    }
    public void Service3DoneCallback(IAsyncResult arResult) {

       service3Done.Set();
    }
}

 

Categories: .NET Framework

A cross process synchronization – Mutex

January 19, 2006 2 comments
Let us take the same Logger class. Assume the library is used by multiple processes, That would still result in out-of order execution. How can we synchronize across processes? The Mutex class comes to our rescue. The Logger example rewritten using a mutex is given below:
public class Logger {
    private static StreamWriter writer;
    private static Mutex writerLock;
    static Logger() {
        // read fileName from config or something else
        writer = new StreamWriter(fileName);
        writerLock = new Mutex(false, "Global\WriterLock");
    }
    public static void Log(string source, string category, string message) {
        try {
            writerLock.WaitOne();
            writer.WriteLine("—————————————————————————-");
            writer.WriteLine("Source: " + source);
            writer.WriteLine("Date & Time: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
            writer.WriteLine("Category: " + category);
            writer.WriteLine("Message: " + message);
            writer.WriteLine("—————————————————————————-");
        }
        finally {
           writerLock.ReleaseMutex();
        }
    }
}
This is similar to the Monitor.Enter and monitor.Leave. WaitOne checks to see if another thread owns the mutex. If not this thread can enter the critical section and becomes the owner of the mutex. If another thread owns the mutex the calling thread is put into a WaitSleepJoin state and remains in that state till the original owner of the mutex call ReleaseMutex.

Categories: .NET Framework

Waiting for something to happen – WaitHandles

January 19, 2006 2 comments

Assume two threads want to acquire a lock. Say thread 1 aquires it first. Thread 2 would like to know when Thread 1 has released the lock. This is the second case of inter thread communication issue which we discussed in the Multi-Threading basics – Setting the state article.

One way is to poll continuously to check if Thread 1 has released the lock. This solution would result in wasting precious CPU cycles. There should be some way to make a thread wait till someting of interest happens and be notified of the event of interest. The waiting thread becomes notified and awakens when the event occurs without resorting to polling.

As we discussed earlier kernel objects are either signalled or non-signalled. The Win32 API WaitForSingleObject puts the calling thread into Wait till the kernel object passed becomes signalled. If you call WaitForSingleObject passing a signalled object, the calling thread can run. If you call WaitForSingleObject passing a non-signalled kernel object the calling thread has to wait till the object becomes signalled.

The same is encapsulated in the object oriented .NET Framework by the WaitHandle class. A WaitHandle can be either in a signalled or non-signalled state. WaitOne stands in as a equivalent for the WaitForSingleObject Win32 API. If you think the discussion is at a too abstract level then that’s because this WaitHandle is an Abstract class. Mutex, AutoResetEvent and ManualResetEvent are concrete classes which derive from Waithandle and provide you waiting abilities.

 

Categories: Uncategorized