Search code examples
dependency-injectionhazelcasthazelcast.net

What's the right way to register Hazelcast .NET Client version 5.2 with dependency injection?


I'm upgrading a .NET 6.0 project from Hazelcast .NET Client 5.1.1 to Hazelcast .NET Client 5.2.1, but after upgrading the NuGet package my configuration settings are no longer recognized and Hazelcast tries to connect using its built-in defaults.

Specifically, the NuGet package is Hazelcast.Net.DependencyInjection:

<PackageReference Include="Hazelcast.Net.DependencyInjection" Version="5.2.1" />

In my code, I log the configuration before trying to create the client. Before upgrading, the ClusterName is what I have in my config ("my-cluster-name"). After upgrading, it's "dev".

My config is stored in appsettings.json:

{
    "hazelcast": {
        "clusterName": "my-cluster-name",
        "networking": {
            "addresses": [
                "myserver:myport"
            ],
        }
    }
}

Here is the code that builds the configuration:

IConfiguration config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", false)
    .Build();

Here's how I register Hazelcast:

IHostBuilder builder = new HostBuilder()
    .ConfigureServices(services =>
    {
        services.AddOptions();
        services.Configure<CacheOptions>(config.GetSection(CacheOptions.SectionName));

        services.AddHazelcast(config); // <------------------

        services.AddTransient<HazelcastTester>();
    })
    .UseConsoleLifetime();

The class that creates the Hazelcast client receives HazelcastOptions via the constructor:

public HazelcastTester(ILogger<HazelcastTester> logger, IOptions<HazelcastOptions> hazelcastOptions, IOptions<CacheOptions> cacheOptions)
    {
        _logger = logger;
        _hazelcastOptions = hazelcastOptions.Value;
        _cacheOptions = cacheOptions.Value;
    }

And, later, creates the client like this:

IHazelcastClient client = await HazelcastClientFactory.StartNewClientAsync(_hazelcastOptions).ConfigureAwait(false);

Changing the PackageReference Version back to 5.1.1 everything works fine. What am I missing?


Solution

  • There was nothing wrong with my code.

    A change was introduced in version 5.2.0 that caused issues with the dependency injection package. An issue was opened on github and was fixed in version 5.2.2 which was released May 29th.

    Updating my package reference to 5.2.2 solved the problem.