I think this is an issue with how I am implementing my custom action, but I am 
looking for some assistance with an issue I have encountered.

I have built a number of C# (.NET 4) Custom Actions which all reside in a 
single DLL file. I am able to call all of these actions successfully and the 
processing logic runs as expected, unless an exception is encountered. Whenever 
an exception is encountered the custom action fails causing the whole 
installation to roll back without ever entering my catch block. I have 
witnessed this behaviour with a number of the actions that are scheduled to run 
during the UI sequence (immediate type 1 custom actions). The below details are 
for a specific new action I am developing to try and A: Verify correct 
connection details for a database and B: Retrieve a version number from the 
database. I am not looking for assistance with the C# code itself, but as to 
why the error handling is not firing when run via WiX.

Below is my scheduling and C# code. Can anyone provide an explanation as to why 
the exception handling is not being called? I have vetted my code with my .NET 
developer colleagues and they are at a loss to explain it as well. I have even 
added in the explicit SQL Exception handling (my error handling does not need 
to be this precise) with no affect.

Using WiX 3.7 with Visual Studio 2010.

C# Code:
    [CustomAction]
    public static ActionResult GetDBVersion(Session session)
    {
        try
        {
            session.Log("Begin retrieval of version number");
            string inputConnString = session["INPUT_CONN_STRING"];
            string dbConnectionString = "";
            if (string.IsNullOrEmpty(inputConnString))
            {
                string dataSource = session["INPUT_SQL_SERVER"];
                string DBName = session["INPUT_DB_NAME"];
                string User = session["INPUT_USER_NAME"];
                string Password = session["INPUT_PASSWORD"];
                if (!string.IsNullOrEmpty(dataSource) && 
!string.IsNullOrEmpty(DBName) && !string.IsNullOrEmpty(User) && 
!string.IsNullOrEmpty(Password))
                {
                    dbConnectionString = String.Format("Data Source={0};Initial 
Catalog={1};Persist Security Info=True;User ID={2};Password={3};", 
dataSource.ToString().Replace("^", ","), DBName, User, Password);
                }
            }
            else if (!string.IsNullOrWhiteSpace(inputConnString))
            {
                dbConnectionString = inputConnString;
            }
            else
            {
                throw new System.ApplicationException("Invalid Database 
Connection String details");
            }
            string checkVersionCommand = "select Max(Replace(Version, 
'AppName', '')) from Version";
            using (SqlConnection conn = new SqlConnection(dbConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(checkVersionCommand, 
conn))
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    if (rdr.HasRows)
                    {
                        string databaseVersion = rdr.GetString(0);
                        session["DATABASE_VERSION"] = databaseVersion;
                    }
                    else
                    {
                        session["DATABASE_VERSION"] = "NONE";
                    }
                }
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            session.Log("ERROR in custom action GetDBVersion {0}",
            ex.ToString());
            session["EXCEPTIONDETAILS"] = ex.ToString();
            string dir = session["CURRENTDIRECTORY"];
            string filepath = dir + "\\Logs\\CustomAction.log";
            File.AppendAllText(filepath, "ERROR in custom action GetDBVersion - 
" + ex.ToString() + "\n");
            session["DATABASE_VERSION"] = "ERROR";
        }
        catch (Exception ex)
        {
            session.Log("ERROR in custom action GetDBVersion-Generic {0}",
            ex.ToString());
            session["EXCEPTIONDETAILS"] = ex.ToString();
            string dir = session["CURRENTDIRECTORY"];
            string filepath = dir + "\\Logs\\CustomAction.log";
            File.AppendAllText(filepath, "ERROR in custom action 
GetDBVersion-Generic - " + ex.ToString() + "\n");
            session["DATABASE_VERSION"] = "ERROR";
        }
        return ActionResult.Success;
    }

CustomAction definition within WXS:
    <CustomAction Id="A.GetDatabaseVersion" Return="check" Execute="immediate" 
BinaryKey="CustomActions" DllEntry="GetDBVersion" /> 

Custom Action Scheduling within UI Sequence
    <UI>
        <Dialog Id="SQLUserLoginDlg" Width="370" Height="270" 
Title="!(loc.SQLUserLoginDlg_DialogTitle)">
            <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" 
Height="17" Default="yes" Text="!(loc.WixUINext)">

                
                <!-- Actions for Database version checking etc -->
                <Publish Event="DoAction" Value="A.SetInputSQLServer" 
Order="10">1</Publish>
                <Publish Event="DoAction" Value="A.SetInputSQLDBName" 
Order="10">1</Publish>
                <Publish Event="DoAction" Value="A.SetInputSQLUser" 
Order="10">1</Publish>
                <Publish Event="DoAction" Value="A.SetInputSQLPassword" 
Order="10">1</Publish>
                <Publish Event="DoAction" Value="A.GetDatabaseVersion" 
Order="11">1</Publish>
...

Thanks.

Regards.
                                          
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
WiX-users mailing list
WiX-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wix-users

Reply via email to