I have a set up similar to this:
Program.cs
{
public static void Main(string[] args)
{
XYZ.SomeMethod(); //--> XYZ is accessed for the firsttime here
}
private void OnDBInstall(object sender, EventArgs e)
{
//after Db install is complete
if(success)
{
InvokeMethod(SomeEvent) //-->this is the second time XYZ is accessed
}
}
}
public static class Utility
{
public static void InvokeSomeMEthods(EventHandler<T> h)
{
if(h!=null)
{
foreach(EventHandler<T> eh in h.GetInvocationList())
{
eh.Invoke(null, EventArgs.Empty);
}
}
}
}
public static class XYZ
{
private static XYZCache _cache = new XYZCache();
static XYZ()
{
SomeEvent += OnSomeEvent;
_cache.Initialize();
}
static void OnSomeEvent(object sender, EventArgs e)
{
_cache.Initialize();
}
static void SomeMethod()
{
//some db call
}
}
internal class XYZCache
{
internal void Initialize()
{
//some db call
}
}
I get multiple exceptions at different stages. When XYZ.SomeMethod() is called the first time, I get, "Cannot open database ".." requested by the login. The login failed". OK this is good because the database doesn't exist at that point. I should have checked if the DB exist first before making any Db calls. And this exception is left unhandled.
Then the other exceptions I get at eh.Invoke (DB is created and available at this point): "The type initializer for 'XYZ' threw an exception", "Method may only be called on a Type for which Type.IsGenericParameter is true.", and I also get "Cannot open database ".." requested by the login. The login failed". When I get these exception, OnSomeEvent in XYZ class is not even invoked so _cache is not accessed.
"The type initializer for 'XYZ' threw an exception" is very misleading since this is not the first time XYZ is accessed.
if XYZ.SomeMethod() is wrapped by a try catch, then everything is good. Is there a way to handle this situation without try catch? I also cannot check for the DB existence because of some weird reason.
Note: I already read this post this is very useful: Method may only be called on a Type for which Type.IsGenericParameter is true
Aucun commentaire:
Enregistrer un commentaire