2

I want to catch exceptions that occurs when I try to connect to my neo4j database using the Neo4jClient.dll. If the database is offline I get the following error: "an exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code." My catch-block is never reached.

This is my code:

class Neo4JConnector
{
    private static GraphClient client = null;

    public Neo4JConnector(IniConfigSource configSource)
    {
        if (client == null)
        {
            client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));

            try
            {
                client.Connect();
            }
            catch (Exception ex) 
            {
                Console.WriteLine("Cannot connect"); // never reached :(
            }

Then I tried to use the "extern" modifier with this code:

class Neo4JConnector
{
    private static GraphClient client = null;

    [DllImport("Neo4jClient.dll", EntryPoint="Connect")]
    static extern void Connect();

    public Neo4JConnector(IniConfigSource configSource)
    {
        if (client == null)
        {
            client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));

            try
            {
                Connect();
            }
            catch (Exception ex) 
            {
                Console.WriteLine("Cannot connect");
            }
        }

But all I get is an Exception that says "[System.EntryPointNotFoundException] = {"Unable to find an entry point named 'Connect' in DLL 'Neo4jClient.dll'.":""}"

This is how the signature looks like in Neo4jClient.dll

public virtual void Connect();

What's wrong with my code? Is there a better way to catch external exceptions? Please help :(

markai
  • 101
  • 1
  • 8

2 Answers2

1

The solution was to uncheck "Break when this exception type is thrown" in the exception settings menu, when the exception is thrown in the external library. Afterwards my catch-block is reached. No need to use [DllImport]

markai
  • 101
  • 1
  • 8
0

According to the GitHub repo, the "Neo4jClient.dll" is a .NET assembly. You should just add reference to it from your project and use the methods.

Here's an example:

using Neo4jClient;
...
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
Clive DM
  • 551
  • 4
  • 13
  • Via `nuget` ideally to keep up to date with the changes – Charlotte Skardon Aug 15 '15 at 15:54
  • @ChrisSkardon Yes, they offer `nuget` package too. Thanks for the reminder. – Clive DM Aug 15 '15 at 15:57
  • Of course i can do that. The problem is that if I start the app but neo4j is not running an exception is thrown in Neo4jClient.dll. However, I want to catch the exception in my code and give a proper error message to the user instead... – markai Aug 16 '15 at 10:41
  • @markai First of all, that's a total unrelated question to your current one, edit it. Second of all, you can always catch the exception thrown by that `Connect` method. – Clive DM Aug 16 '15 at 10:51
  • It's not unrelated. The second code snippet was my attempt to fix the issue I described. However, it's solved. Thanks for the help – markai Aug 16 '15 at 11:33