Error handling Reference DLL not found

S

Sig

How to I detect reference library is in place. If not, it will prompt
dll not found and terminate the program?

Following is my sample program:

using System;
using MySql.Data.MySqlClient;

class mysql
{

public static void Main()
{
try
{
MySqlConnection con = new MySqlConnection
("server=localhost;database=test;uid=root;pwd=;");
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader rdr;

con.Open();

cmd.Connection = con;
cmd.CommandText = "SHOW TABLES";

rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0].ToString());
}
Console.WriteLine();
rdr.Close();

con.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}

Compiling options:
csc /optimize /nologo /target:exe /reference:MySql.Data.dll /
out:mysql.exe mysql.cs

If I were to rename or remove the MySql.Data.dll file, then my program
will have error even though I already catch all exceptions.

So, how do I solve this problem?
Thanks a lot!


Sig
 
G

Gregory A. Beamer

If I were to rename or remove the MySql.Data.dll file, then my program
will have error even though I already catch all exceptions.

So, how do I solve this problem?

What do you mean?

How to compile when the assembly has been removed or name changed? You
can't.

How to detect a missing assembly? Put a startup "wrapper" around the
program that checks for assemblies via the file system and reflection
and alert if it is wrong or missing. This is very similar to the method
Microsoft uses for updates to a windows program (Updater Block,
ClickOnce update, etc).

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
S

Sig

Thanks for your reply.
What do you mean?

How to compile when the assembly has been removed or name changed? You
can't.

Well, what I mean is after I successfully compiled the program. But if
the program can't find the required *.dll when it execute, it will
crash. Even after I put in error handling. So, I was searching for
solution how to detect the dlls then prompt for error instead of
crashing.
How to detect a missing assembly? Put a startup "wrapper" around the
program that checks for assemblies via the file system and reflection
and alert if it is wrong or missing. This is very similar to the method
Microsoft uses for updates to a windows program (Updater Block,
ClickOnce update, etc).

How do I create this wrapper with c# code? Can you show me some sample
code or resources where I can refer to?
Googling for assembly wrapper seems not much help.

Thanks.
 
K

kndg

Sig said:
How to I detect reference library is in place. If not, it will prompt
dll not found and terminate the program?

Following is my sample program:

using System;
using MySql.Data.MySqlClient;

class mysql
{

public static void Main()
{
try
{
MySqlConnection con = new MySqlConnection
("server=localhost;database=test;uid=root;pwd=;");
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader rdr;

con.Open();

cmd.Connection = con;
cmd.CommandText = "SHOW TABLES";

rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0].ToString());
}
Console.WriteLine();
rdr.Close();

con.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}

Compiling options:
csc /optimize /nologo /target:exe /reference:MySql.Data.dll /
out:mysql.exe mysql.cs

If I were to rename or remove the MySql.Data.dll file, then my program
will have error even though I already catch all exceptions.

So, how do I solve this problem?
Thanks a lot!


Sig

Hi Sig,

You can't catch the above exception because the exception is thrown
during assembly binding process (before your Main method ever executed).
If you want to catch this exception, you can do it through late binding
(which involves Reflection) or by loading your executable through
another Application Domain. Reflection is fairly complex for such a
simple purpose, so I show you the second method instead;

public class ApplicationWrapper
{
public static void Main(string[] args)
{
AppDomain newDomain = AppDomain.CreateDomain("My Domain");

try
{
newDomain.ExecuteAssembly("Path to your executable");
}
catch (System.IO.FileNotFoundException)
{
Console.WriteLine("Required assembly not found!");
}
}
}

Regards.
 
G

Gregory A. Beamer

How do I create this wrapper with c# code? Can you show me some sample
code or resources where I can refer to?
Googling for assembly wrapper seems not much help.

Create another program to spin up the main program, using a Process
object. Then check and make sure everything is there that it needs to
spin the main program up.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top