DotNet4Neo4j / Neo4jClient

.NET client binding for Neo4j
https://www.nuget.org/packages/Neo4jClient
Microsoft Public License
431 stars 146 forks source link

Client has not connected to the Neo4j server #456

Closed Shishkovskiy closed 1 year ago

Shishkovskiy commented 1 year ago

Hi there, I'm using .Net 6.0 Neo4jClient 5.1.3 Neo4j 5.3.0

I've been trying to run my app in Docker and I've got the error :

Neo4j.Driver.ServiceUnavailableException: Connection with the server breaks due to ExtendedSocketException: Name or service not known Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in Neo4j 4.0.

Please, take a look at these configuration files. If someone knows where the issue is, please lemme know because, to tell the truth, I have no idea what is wrong here. I appreciate any help you can provide.

appsettings.json

{
  "Neo4jConfiguration": {
    "Uri": "bolt://neo4j:7687",
    "UserName": "neo4j",
    "Password": "6C2coi7kwnyYb7hNmfyvYNkyhxB!"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Information"
    }
  },
  "AllowedHosts": "*"
}
internal sealed record Neo4jConfiguration
{
    public string Uri { get; set; } = null!;
    public string UserName { get; set; } = null!;
    public string Password { get; set; } = null!;
}

Creating client

        var neo4jConfiguration = new Neo4jConfiguration();
        configuration.Bind(nameof(Neo4jConfiguration), neo4jConfiguration);

        var client = new BoltGraphClient(neo4jConfiguration.Uri, neo4jConfiguration.UserName, neo4jConfiguration.Password);

        await client.ConnectAsync();
        serviceCollection.AddSingleton<IGraphClient>(client);

docker-compose.yml

version: '3.4'

services:
  server:
    image: "webapi-image"
    network_mode: "bridge"
    ports:
      - "8080:80"
    build:
      context: .
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    environment:
      - CHOKIDAR_USEPOLLING=true
      - NEO4JCONFIGURATION__URI=bolt://neo4j:7687
    depends_on:
      - neo4j
  neo4j:
    image: "neo4j:latest"
    network_mode: "bridge"
    ports:
      - "7687:7687"
      - "7474:7474"
    volumes:
      - ./neo4j/data:/data
      - ./neo4j/logs:/logs
      - ./neo4j/import:/var/lib/neo4j/import
      - ./neo4j/plugins:/plugins
    environment:
      - NEO4J_AUTH=neo4j/6C2coi7kwnyYb7hNmfyvYNkyhxB!
cskardon commented 1 year ago

I think you're going to have much better luck on StackOverflow, as my only guesses are that this is related to the docker configuration.

Can you access the DB at: http://neo4j:7474?

Shishkovskiy commented 1 year ago

@cskardon Nope, http can't be used here. I have to use bolt or neo4j. In this case, neo4j is running and I can join from the browser. But when I tried to call my endpoint I've got the error 👎 and I have no clue what is wrong here.

cskardon commented 1 year ago

So that's what I'm asking - when you go to the browser what address are you using?

Shishkovskiy commented 1 year ago

@cskardon I`m using bolt://localhost:7687, but in the scope of docker, I can't use localhost because the container has no idea what the local host is, and each of them is isolated and has its host as I understood)

cskardon commented 1 year ago

So. From outside of docker - can you connect to the browser? i.e. http://SOMETHING:7474 ?

Shishkovskiy commented 1 year ago

@cskardon Yes, when I run the docker images, I can go to the browser and connect)

image

image

cskardon commented 1 year ago

So, the URI that you would use would be neo4j://localhost:7687 ? If that is how you would connect to the browser, that's how the code would also connect

Shishkovskiy commented 1 year ago

@cskardon I've already found the issues, to tell the truth there were several issues :) Now it works, maybe it'll be useful for someone who struggle with it as well.

  1. We didn't await when connect to the client and in this case we lost main exception
  2. Docker compose had wrong configuration
  3. When I run docker compose the app starts before the neo4j and of course we couldn't create a client because neo4j wasn't ready
version: '3.4'

services:
  server:
    image: "webapi-image"
    ports:
      - "8080:80"
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - NEO4JCONFIGURATION__URI=bolt://neo4j:7687
      - NEO4JCONFIGURATION__USERNAME=neo4j
      - NEO4JCONFIGURATION__PASSWORD=6C2coi7kwnyYb7hNmfyvYNkyhxB!
    depends_on:
      neo4j:
        condition: service_healthy
  neo4j:
    image: "neo4j:3.5"
    ports:
      - "7687:7687"
      - "7474:7474"
    volumes:
      - ./neo4j/data:/data
      - ./neo4j/logs:/logs
      - ./neo4j/import:/var/lib/neo4j/import
      - ./neo4j/plugins:/plugins
    environment:
      - NEO4J_AUTH=neo4j/6C2coi7kwnyYb7hNmfyvYNkyhxB!
    healthcheck:
      test: wget http://localhost:7474 || exit 1
      interval: 1s
      timeout: 10s
      retries: 10
      start_period: 3s