Archive

Archive for March, 2009

Custom Exception

March 31, 2009 amiraryani Leave a comment

When you develop a n-tier application, it is quite common to create a custom exception class to inform the upper level application layers from an error.

The followings are the list of the important (in my opinion) things to be  considered for creating such a class. 

public class NewException : BaseException, ISerializable
{
    public NewException()
    {
        // Add implementation.
    }
    public NewException(string message)
    {
        // Add implementation.
    }
    public NewException(string message, Exception inner)
    {
        // Add implementation.
    }

    // This constructor is needed for serialization.
   protected NewException(SerializationInfo info, StreamingContext context)
   {
        // Add implementation.
   }
}

References: 

  1. Designing Custom Exceptions: 
    http://msdn.microsoft.com/en-us/library/ms229064.aspx
  2. Design Guidelines for Exceptions
    http://msdn.microsoft.com/en-us/library/ms229014.aspx

Read/write Excel and Access files without installing MS Office

March 25, 2009 amiraryani Leave a comment

I faced an issue in multiple projects where a software application uses Microsoft Office libraries to read/write Excel or Access files, but the Microsoft Office can not be installed to the target (application) environment.  In such cases the missing DLLs stop application from working. 

To work around this problem there is a installset for 2007 Office System Driver: Data Connectivity Components which can install required DLLs on any computer.  You can download and deploy this package as part of your product. At this stage I am not aware of any restriction on using this installset, and if you find such a restriction please leave me a comment on this post. 

To download the package please refer to Microsoft Download Centre: 

URL: http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

File name: AccessDatabaseEngine.exe

MSDN: To use this download

  1. If you are the user of an application, consult your application documentation for details on how to use the appropriate driver.
  2. If you are an application developer using OLEDB, set the Provider argument of the ConnectionString property to “Microsoft.ACE.OLEDB.12.0”
  3. If you are connecting to Microsoft Office Excel data, add “Excel 12.0” to the Extended Properties of the OLEDB connection string.
  4. If you are application developer using ODBC to connect to Microsoft Office Access data, set the Connection String to “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file”
  5. If you are application developer using ODBC to connect to Microsoft Office Excel data, set the Connection String to “Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file”

Additional note: If in the source code one of Office components is instantiated then 2007 Office System Driver: Data Connectivity Components will not help. For example to run the following code it is required to have MS Excel installed on the system.

Dim objExcel As Object = CreateObject("Excel.Application")

Error Message: External component has thrown an exception. Exception Type: System.Runtime.InteropServices.SEHException

March 17, 2009 amiraryani Leave a comment

I recently got a case that a .Net 1.1 windows application occasionally crashes while switching between forms.  The error’s source is System.Windows.Forms and it is a SEHException type which has nothing to do with the software source code. The following shows the error description and the stack trace:

Source: System.Windows.Forms
Message: External component has thrown an exception.
Exception Type: System.Runtime.InteropServices.SEHException
Occurred at Assembly: System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Stack: at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at … line 92

Further research on the web showed that we are not the only one facing the problem.  Kevin Gearing commented on this issues as a confirmed bug in .Net 1.1. He found that this problem is related to Application.EnableVisualStyles().  

I checked the maintenance history logs, and find out the EnableVisualStyles() added as part of recent change to the source code.  Removing this function from application start up rectified the problem.  So I concluded that using Application.EnableVisualStyles() introduced the error.

Now that we know what causes the error, the next question is that how to work around the problem, and have a XP style for .Net 1.1 application.  I checked MSDN documentation on Application.EnableVisualStyles Method, and I noticed that there is a note about setting the FlatStyle property of the ButtonBase controls to FlatStyle.System. So I did as it is recommended, and unfortunately no effect on the problem. It seems using that the manifest is the only option. Enabling Visual Styles article in MSDN contains a detailed instruction on how to create a manifest for an application.

Categories: .Net CLR

SOA knowledge is a prerequisite for WCF

March 16, 2009 amiraryani Leave a comment

In the introduction of  Inside Microsoft Windows Communication Foundation, Smith explained the true need for understanding SOA principal for developing an efficent WCF application.  His exmple of the historical shifting paradaim between procedural programming to object oriented programming is a simple description why an indept knowledge about services is needed prior to start de signing applications based on WCF.

This sort of coupling is not a new idea; it comes from past experience. When object orientation was gaining popularity, developers and architects making the transition from procedural programming to an object-oriented language needed to know more than just the new syntax of the language. If procedural programmers began using a more modern language without understanding how to design objects, they simply created procedural applications in the new language. Although these applications could be compiled and run, they did not take advantage of the functionality offered through object orientation. It is my view that the same will be true of developers who start to use WCF without a clear picture of how to leverage the power of service-oriented application designs.

byJustin Smith, Inside Microsoft Windows Communication Foundation, Part I – Introduction to WCF

 

I also find the other example quite interesting about a sterio type of C++ programmers who add finalizer to all classes.  I think it was one of my typical mistakes in programming when I start developing in .Net for the first time.

A C++ developer moving to C# without any knowledge of the garbage collector will instinctively add a finalizer to all type declarations. Unknowingly, this developer will have increased the time required to allocate these objects and increased the lifetime of these objects. For most C++ developers, simply saying “don’t do it” isn’t enough. They want to know why. Technically, adding a finalizer to a type is not a bug, but it is certainly an inefficiency that could have been averted through a couple of hours spent with a book or in a good training course.

byJustin Smith, Inside Microsoft Windows Communication Foundation, Part I – Introduction to WCF

Code structure and namespace best practinces

March 11, 2009 amiraryani Leave a comment

In a team development environment, it is important to have a best practice guideline. Firstly to avoid confusion for the beginners in the group, secondly to stop people arguing about matters based on their personal opinion. 

Today, my colleage and I had a discussion about defining namespace contents, and I decided to search the web to find the answer in Microsoft recommended best practices. The best document which I got is .NET Framework Developer’s Guide, Guidelines for Names,and I could not find the answer of following questions:

  • Should software architecture tiers match separate name spaces? example: Does CompanyName.ProductName.UI contain all the codes that running in the UI layer, or it contain only UI controls?
  • Where should Exception Handling class be located? in Business namespace or UI or a new namespace such as CompanyName.ProductName.ExceptionHandling?
Categories: Software Architecture