Skip to content

Conversation

@mpartipilo
Copy link
Collaborator

@mpartipilo mpartipilo commented Nov 26, 2025

Introduce an async initialization pattern and dependency injection support for the Weaviate client, ensuring non-blocking operations. Implement a factory for managing multiple named clients with independent configurations. Enhance existing methods for consistency and add comprehensive documentation and examples. No breaking changes introduced (maybe?).

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@mpartipilo mpartipilo self-assigned this Nov 27, 2025
@mpartipilo mpartipilo requested a review from g-despot November 27, 2025 13:02
@mpartipilo mpartipilo force-pushed the feat/typed-clients branch 2 times, most recently from 57ae691 to 2551654 Compare December 2, 2025 09:07
Base automatically changed from feat/typed-clients to main December 2, 2025 09:15
Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

Implements async initialization pattern with IHostedService for proper
DI integration without blocking calls or GetAwaiter().GetResult().
Key Features:
- Async initialization using Lazy<Task> pattern
- Eager initialization via IHostedService (default)
- Lazy initialization option for on-demand scenarios
- Full backward compatibility with Connect.Local/Cloud helpers
- Follows existing REST → Meta → gRPC initialization flow
New Components:
- WeaviateOptions: Configuration class for DI scenarios
- WeaviateServiceCollectionExtensions: AddWeaviate() extension methods
- WeaviateInitializationService: IHostedService for eager initialization
- DI-friendly constructor: WeaviateClient(IOptions<WeaviateOptions>)
Public API Changes:
- Added: WeaviateClient.InitializeAsync() - manually trigger initialization
- Added: WeaviateClient.IsInitialized - check initialization status
- Added: EnsureInitializedAsync() - internal helper for async methods
- Modified: GetMeta(), Live(), IsReady() - now await initialization
Dependencies Added:
- Microsoft.Extensions.Hosting.Abstractions 9.0.8
- Microsoft.Extensions.Options 9.0.8
Documentation:
- DEPENDENCY_INJECTION.md - comprehensive DI usage guide
- DependencyInjectionExample.cs - example demonstrating all patterns
Benefits:
✅ No more GetAwaiter().GetResult() anti-pattern
✅ Works seamlessly with ASP.NET Core DI
✅ Thread-safe initialization with Lazy<Task>
✅ Keeps REST → Meta → gRPC flow for max message size
✅ Connect.Local() and Connect.Cloud() still work unchanged
✅ Proper async/await throughout
Breaking Changes: None
- All existing constructors and patterns continue to work
- Connect helpers unchanged
- WeaviateClientBuilder unchanged
Implements IWeaviateClientFactory for managing multiple Weaviate clients
with independent configurations. Each client can have different hosts,
ports, credentials, timeouts, and other settings.
Key Features:
- Named client pattern similar to IHttpClientFactory
- Each client has independent configuration
- Lazy initialization and caching
- Thread-safe client creation
- Automatic disposal of all clients
New Components:
- IWeaviateClientFactory: Interface for client factory
- WeaviateClientFactory: Implementation with lazy creation and caching
- AddWeaviateClient(): Register named clients
- AddWeaviateCloudClient(): Helper for cloud clients
Public API:
- IWeaviateClientFactory.GetClient(name): Get client synchronously
- IWeaviateClientFactory.GetClientAsync(name): Get client asynchronously
Extension Methods:
- AddWeaviateClientFactory(): Register the factory
- AddWeaviateClient(name, configureOptions): Register named client
- AddWeaviateClient(name, hostname, ports...): Helper for local clients
- AddWeaviateCloudClient(name, endpoint, apiKey): Helper for cloud clients
Use Cases:
- Multi-environment (prod, staging, local)
- Multi-region deployments
- Multi-tenant architectures
- Different databases/clusters
- Analytics vs operational databases
Documentation:
- MULTIPLE_CLIENTS.md: Comprehensive guide with patterns
- MultipleClientsExample.cs: Complete working examples
- DifferentConfigsExample.cs: Shows independent configurations
Example Usage:
```csharp
// Register multiple clients
services.AddWeaviateClient("prod", options => { ... });
services.AddWeaviateClient("staging", options => { ... });
services.AddWeaviateClient("local", "localhost", 8080, 50051);
// Use in service
public class MyService
{
    private readonly IWeaviateClientFactory _factory;
    public async Task ProcessAsync()
    {
        var prod = await _factory.GetClientAsync("prod");
        var staging = await _factory.GetClientAsync("staging");
        // Each has completely different config
    }
}
```
Breaking Changes: None
- Single client pattern (AddWeaviate) still works
- Factory is opt-in via AddWeaviateClient
…er support

Adds headers and timeout parameters to match the Connect.Local() and
Connect.Cloud() helper API signatures, providing consistency between
DI registration and direct client creation patterns.
Changes:
- AddWeaviateLocal(): Added headers, defaultTimeout, initTimeout, dataTimeout, queryTimeout
- AddWeaviateCloud(): Added headers, defaultTimeout, initTimeout, dataTimeout, queryTimeout
Now these methods have the same configuration options as Connect.Local/Cloud:
```csharp
// Single client DI registration - now with full options
builder.Services.AddWeaviateLocal(
    hostname: "localhost",
    headers: new Dictionary<string, string> { ["X-Custom"] = "value" },
    queryTimeout: TimeSpan.FromSeconds(120),
    eagerInitialization: true
);
builder.Services.AddWeaviateCloud(
    clusterEndpoint: "my-cluster.weaviate.cloud",
    apiKey: "key",
    headers: customHeaders,
    defaultTimeout: TimeSpan.FromMinutes(1)
);
// Matches the pattern of:
var client = await Connect.Local(
    hostname: "localhost",
    headers: new Dictionary<string, string> { ["X-Custom"] = "value" },
    queryTimeout: TimeSpan.FromSeconds(120)
);
```
Benefits:
✅ API consistency between DI and Connect helpers
✅ Full control over timeouts in DI scenarios
✅ Support for custom headers
✅ No need to use Configure<WeaviateOptions> for simple cases
Breaking Changes: None
- All new parameters are optional with sensible defaults
- Existing code continues to work unchanged
- Add AddWeaviateLocal(name) with default localhost:8080 configuration
- Add AddWeaviateLocal(name, Action<WeaviateOptions>) for custom configuration
- Add AddWeaviateLocal(name, hostname, ports, ...) with full parameter support
- Add equivalent AddWeaviateCloud(name, ...) overloads for cloud clients
- Mark old AddWeaviateClient() and AddWeaviateCloudClient() as obsolete
This provides a more intuitive API where users can write:
  services.AddWeaviateLocal("local-rbac");
  services.AddWeaviateLocal("local-test", options => { ... });
  services.AddWeaviateCloud("prod", "my-cluster.weaviate.cloud", "api-key");
Reorganized the Example project to make all examples easily accessible:

- Added interactive menu in Program.cs to choose which example to run
- Support for command-line arguments to run specific examples directly
- Moved original example code to TraditionalExample.cs
- Renamed Main() to Run() in MultipleClientsExample and DifferentConfigsExample
- Created comprehensive README.md with:
  - How to run each example (interactive or command-line)
  - Detailed overview of all 7 examples
  - When to use each pattern
  - Prerequisites and troubleshooting
  - Common patterns for ASP.NET Core and background services

Examples now available:
1. Traditional - Basic usage with Connect.Local()
2. Dependency Injection - ASP.NET Core-style DI
3. Multiple Clients - Working with multiple named clients
4. Different Configs - Per-client custom settings
5. Configuration - Reading from appsettings.json
6. Lazy Initialization - On-demand client creation
7. Connect Helper - Backward compatibility

Run interactively: dotnet run --project src/Example
Run specific: dotnet run --project src/Example -- di
The DependencyInjectionExample requires Microsoft.Extensions.Hosting
for Host.CreateDefaultBuilder() but the package reference was missing
from the project file.
@mpartipilo mpartipilo changed the title Enhance Weaviate client with async initialization and multiple client support Dependency Injection and Async initialization Dec 2, 2025
@mpartipilo mpartipilo merged commit b4b5d1e into main Dec 2, 2025
13 checks passed
@mpartipilo mpartipilo deleted the feat/async-di branch December 2, 2025 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants