I have a COM that implement an interface IEventHandler and i want that exception return to principal application!
This is my code:
using System;
using System.IO;
using interop.OSHandler;
using System.Data;
using System.Data.OleDb;
using Microsoft.Win32;
using System.Web.Mail;
using System.Web;
namespace CustomHandlerCOM
{
///
/// Summary description for Class1.
///
//Sample2
public class Sample2: IEventHandler
{
private int m_Instance;
private string m_strOperation = "";
private string m_strObjectType = ""; //1=Documento 2=Cartella 3=Link
private string m_strEventName = "";
private string m_strUserName = "";
private string m_strObjectID = "";
private string m_strParentObjID = "";
private string m_strCurDirPath = "";
private string m_strCurFolderPath = "";
private string m_strNativeDocument = "";
private string m_strConvertedDocument = "";
private string m_strWorkflowStage = "";
private string m_title = "";
private string m_author = "";
private string m_descr = "";
//connessione
OleDbConnection conn = new OleDbConnection();
public Sample2()
{
//Constractor
}
/****************************************/
/* IMPLEMENTO I METODI DELL'INTERFACCIA */
/****************************************/
/* METODO INIT:
* al suo interno richiamo la funz. GetConnection()
* passandogli l'ID dell' istanza e compongo la stringa
* di connessione.*/
public void Init(int InstanceID)
{
m_Instance = InstanceID;
try
{
conn.ConnectionString = GetConnection(InstanceID);
conn.Open();
}
catch (Exception e)
{
Console.WriteLine("");
//Console.ReadLine();
//MyException("Failed to connect to data source: ", e);
}
}
/* METODO ADDPARAM:
* memorizza tutti i parametri in variabili locali di classe.*/
public void AddParam(string strParamName, string strParamValue)
{
switch(strParamName)
{
case "Operation":
m_strOperation = strParamValue;
break;
case "ObjectType":
m_strObjectType = strParamValue;
break;
case "UserName":
m_strUserName = strParamValue;
break;
case "ObjectID":
m_strObjectID = strParamValue;
break;
case "ParentObjectID":
m_strParentObjID = strParamValue;
break;
case "CurDirPath":
m_strCurDirPath = strParamValue;
break;
case "CurFolderPath":
m_strCurFolderPath = strParamValue;
break;
case "NativeDocument":
m_strNativeDocument = strParamValue;
break;
case "ConvertedDocument":
m_strConvertedDocument = strParamValue;
break;
case "WorkflowStage":
m_strWorkflowStage = strParamValue;
break;
}
}
/* METODO HANDLEEVENT:
* a seconda dell' nEventID mi crea la select*/
public void HandleEvent(int nEventID)
{
string SQL = "";
GetEventName(nEventID);
if(nEventID == 1)
{
if(m_strOperation == "CreateFolder" || m_strOperation == "PublishDocument" || m_strOperation == "PublishLink")
{
//niente object Id niente select
}
}
else
{
switch(m_strObjectType)
{
case "1": //Caso in cui l'oggetto sia un DOCUMENTO
SQL ="SELECT szTitle, szAuthor, szDescr FROM Documents "+
"WHERE ID = " + m_strObjectID;
break;
case "2": //Caso in cui l'oggetto sia una CARTELLA
SQL ="SELECT szTitle, szUser, szDescr FROM Dirs "+
"WHERE ID = " + m_strObjectID;
break;
}
}
try
{
//Creo l'ogg ADO.NET
OleDbCommand cmd = new OleDbCommand(SQL, conn);
OleDbDataReader r = cmd.ExecuteReader();
//Leggo la prima riga
r.Read();
//Leggo le tre colonne
m_title = r.GetValue(0).ToString();
m_author = r.GetValue(1).ToString();
if(r.IsDBNull(2))
{
m_descr = "nessuna descrizione";
}
else
{
m_descr = r.GetValue(2).ToString();
}
}
catch(Exception e)
{
MyException("Error in DataReader: " , e);
}
finally
{
conn.Close();
}
//Mando la mial...
try
{
SmtpMail.Send(GetMail());
}
catch(Exception e)
{
MyException("Error Send Mail:" , e);
}
}
//Metodo per la connessione al DB
private string GetConnection(int IDInstance)
{
string connection = "";
RegistryKey rk = Registry.LocalMachine;
RegistryKey tempKey = rk.OpenSubKey("SOFTWARE\\Infosquare\\WebFrame");
tempKey = rk.OpenSubKey("SOFTWARE\\Infosquare\\WebFrame\\" + IDInstance);
string DbsName = tempKey.GetValue("DbsName").ToString();
string DbsPwd = tempKey.GetValue("DbsPwd").ToString();
string DbsVer = tempKey.GetValue("DbsVer").ToString(); //0=ORA9;1=ORA7.3;2=ORA8
string DbsServer = tempKey.GetValue("DbsServer").ToString();
string DbsUser = tempKey.GetValue("DbsUser").ToString();
string Product = tempKey.GetValue("Product").ToString(); //2=Access;3=SQL;4=ORA
string Root = tempKey.GetValue("Root").ToString();
Root = Root.Substring(0,Root.LastIndexOf("\\"));
switch(Product)
{
case "2":
connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data SSource="+Root+"\\dbs\\WebFrameDB.mdb" +
";User ID=;Password=;";
break;
case "3":
connection = "Provider=SQLOleDB; DRIVER={SQL Server}; DATABASE=" + DbsName +
"; SERVER=" + DbsServer +
"; UID=" + DbsUser +
"; PWD=" + DbsPwd + ";";
break;
case "4":
connection = "Provider=MSDAORA;Data Source=" + DbsName +
";User ID=" + DbsUser +
";Password=" + DbsPwd + ";";
break;
}
return (connection);
}
//Metodo che passato l'id dell'evento mi restituisce il nome dell'evento
private void GetEventName(int eID)
{
switch(eID)
{
case 1:
m_strEventName = "OnInitFunction";
break;
case 2:
m_strEventName = "OnLeaveFunction";
break;
case 3:
m_strEventName = "OnBeforeHtmlConversion";
break;
case 4:
_strEventName = "OnAfterHtmlConversion";
break;
case 5:
m_strEventName = "OnBeforePdfConversion";
break;
case 6:
m_strEventName = "OnAfterPdfConversion";
break;
case 7:
m_strEventName = "OnBeforeSiteIndexGeneration";
break;
case 8:
m_strEventName = "OnAfterSiteIndexGeneration";
break;
case 9:
m_strEventName = "OnBeforeSiteDocGeneration";
break;
case 10:
m_strEventName = "OnAfterSiteDocGeneration";
break;
case 13:
m_strEventName = "OnEnterStage";
break;
case 14:
m_strEventName = "OnLeaveStage";
break;
case 15:
m_strEventName = "OnVerified";
break;
case 16:
m_strEventName = "OnApproved";
break;
case 17:
m_strEventName = "OnRejected";
break;
case 18:
m_strEventName = "OnExpiring";
break;
case 19:
m_strEventName = "OnPublishing";
break;
case 20:
m_strEventName = "OnObsolete";
break;
case 21:
m_strEventName = "OnAfterHtmlFilter";
break;
case 22:
m_strEventName = "OnRemovable";
break;
}
}
//Metodo per la costruzione della Mail
private MailMessage GetMail()
{
//Costruisco la mail...
SmtpMail.SmtpServer = "asterix.home.it";
MailMessage mail = new MailMessage();
mail.To = "
[email protected]";
mail.Cc = "";
mail.Bcc = "";
mail.From = "
[email protected]";
mail.Subject = "Test_Event_Handler";
mail.Body = "
Function:" + m_strEventName +
"
Operation:" + m_strOperation +
"
Title:" + m_title +
"
Author:" + m_author +
"
Desc:" + m_descr;
mail.BodyFormat = MailFormat.Html;
return(mail);
}
//Metodo che restituisce il path del file di log
private string GetFileLog(int Instance)
{
RegistryKey rk = Registry.LocalMachine;
RegistryKey tempKey = rk.OpenSubKey("SOFTWARE\\Infosquare\\WebFrame");
tempKey = rk.OpenSubKey("SOFTWARE\\Infosquare\\WebFrame\\" + Instance);
string root = tempKey.GetValue("Root").ToString();
root = root.Substring(0,root.LastIndexOf("\\"));
//Creo il path del file di Log
string path = @root + "\\logs\\Handle_Sample2.log";
return(path);
}
//Custom delle Exception che si verificano durante l'esecuzione
private void MyException(string message, Exception inner)
{
string path = GetFileLog(m_Instance);
//File di log
using (StreamWriter log = File.AppendText(path))
{
log.WriteLine("### Event_Log ###");
log.WriteLine(message.ToString());
log.WriteLine(inner.ToString());
}
}
}
}
Sorry for my English byebye