-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferentConfigsExample.cs
More file actions
233 lines (208 loc) · 9.9 KB
/
DifferentConfigsExample.cs
File metadata and controls
233 lines (208 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weaviate.Client;
using Weaviate.Client.DependencyInjection;
using Weaviate.Client.Managed.Extensions;
namespace Example;
/// <summary>
/// Example showing how each named client can have completely different configuration.
/// Each client has its own: host, port, credentials, timeouts, SSL settings, etc.
/// </summary>
public class DifferentConfigsExample
{
public static async Task Run()
{
var host = Host.CreateDefaultBuilder()
.ConfigureServices(
(context, services) =>
{
// Client 1: DemoProduction cloud with SSL and API key
services.AddWeaviateClient(
"production",
options =>
{
options.RestEndpoint = "prod.weaviate.cloud";
options.GrpcEndpoint = "grpc-prod.weaviate.cloud";
options.RestPort = 443;
options.GrpcPort = 443;
options.UseSsl = true;
options.Credentials = Auth.ApiKey("prod-api-key-here");
options.DefaultTimeout = TimeSpan.FromSeconds(60);
options.QueryTimeout = TimeSpan.FromSeconds(30);
options.Headers = new Dictionary<string, string>
{
["X-Environment"] = "production",
};
}
);
// Client 2: Local development, no SSL, no auth, longer timeouts
services.AddWeaviateClient(
"local",
options =>
{
options.RestEndpoint = "localhost";
options.GrpcEndpoint = "localhost";
options.RestPort = 8080;
options.GrpcPort = 50051;
options.UseSsl = false;
options.Credentials = null; // No auth for local
options.DefaultTimeout = TimeSpan.FromSeconds(120); // Longer for debugging
options.QueryTimeout = TimeSpan.FromSeconds(300); // Very long for local testing
}
);
// Client 3: Staging with OAuth credentials
services.AddWeaviateClient(
"staging",
options =>
{
options.RestEndpoint = "staging.weaviate.cloud";
options.GrpcEndpoint = "grpc-staging.weaviate.cloud";
options.RestPort = 443;
options.GrpcPort = 443;
options.UseSsl = true;
options.Credentials = Auth.ClientCredentials(
"staging-client-secret",
"weaviate.read",
"weaviate.write"
);
options.DefaultTimeout = TimeSpan.FromSeconds(45);
}
);
// Client 4: Analytics cluster with custom ports and retry policy
services.AddWeaviateClient(
"analytics",
options =>
{
options.RestEndpoint = "analytics.internal.company.com";
options.GrpcEndpoint = "analytics.internal.company.com";
options.RestPort = 9090; // Custom port
options.GrpcPort = 9091; // Custom port
options.UseSsl = true;
options.Credentials = Auth.ApiKey("analytics-key");
options.QueryTimeout = TimeSpan.FromSeconds(120); // Slow analytics queries
options.RetryPolicy = new RetryPolicy
{
MaxRetries = 5, // More retries for unreliable network
InitialDelay = TimeSpan.FromSeconds(2),
};
}
);
// Client 5: Legacy system with password auth
services.AddWeaviateClient(
"legacy",
options =>
{
options.RestEndpoint = "legacy.oldserver.com";
options.GrpcEndpoint = "legacy.oldserver.com";
options.RestPort = 8081;
options.GrpcPort = 50052;
options.UseSsl = false; // Old server doesn't support SSL
options.Credentials = Auth.ClientPassword(
"legacy-username",
"legacy-password"
);
options.InitTimeout = TimeSpan.FromSeconds(10); // Slow to start
}
);
services.AddSingleton<MultiConfigService>();
}
)
.Build();
await host.StartAsync();
var service = host.Services.GetRequiredService<MultiConfigService>();
await service.ShowDifferentConfigsAsync();
await host.StopAsync();
}
}
public class MultiConfigService
{
private readonly IWeaviateClientFactory _factory;
public MultiConfigService(IWeaviateClientFactory factory)
{
_factory = factory;
}
public async Task ShowDifferentConfigsAsync()
{
Console.WriteLine("=== Different Client Configurations ===\n");
// Get all clients - each with different configuration
var prodClient = await _factory.GetClientAsync("production");
var localClient = await _factory.GetClientAsync("local");
var stagingClient = await _factory.GetClientAsync("staging");
var analyticsClient = await _factory.GetClientAsync("analytics");
var legacyClient = await _factory.GetClientAsync("legacy");
// Show each client's configuration
Console.WriteLine($"DemoProduction:");
Console.WriteLine(
$" - Endpoint: {prodClient.Configuration.RestAddress}:{prodClient.Configuration.RestPort}"
);
Console.WriteLine($" - SSL: {prodClient.Configuration.UseSsl}");
Console.WriteLine($" - Version: {prodClient.WeaviateVersion}");
Console.WriteLine($" - Query Timeout: {prodClient.Configuration.QueryTimeout}");
Console.WriteLine();
Console.WriteLine($"Local:");
Console.WriteLine(
$" - Endpoint: {localClient.Configuration.RestAddress}:{localClient.Configuration.RestPort}"
);
Console.WriteLine($" - SSL: {localClient.Configuration.UseSsl}");
Console.WriteLine($" - Version: {localClient.WeaviateVersion}");
Console.WriteLine($" - Query Timeout: {localClient.Configuration.QueryTimeout}");
Console.WriteLine();
Console.WriteLine($"Staging:");
Console.WriteLine(
$" - Endpoint: {stagingClient.Configuration.RestAddress}:{stagingClient.Configuration.RestPort}"
);
Console.WriteLine($" - SSL: {stagingClient.Configuration.UseSsl}");
Console.WriteLine($" - Version: {stagingClient.WeaviateVersion}");
Console.WriteLine();
Console.WriteLine($"Analytics:");
Console.WriteLine(
$" - Endpoint: {analyticsClient.Configuration.RestAddress}:{analyticsClient.Configuration.RestPort}"
);
Console.WriteLine(
$" - Custom Ports: REST={analyticsClient.Configuration.RestPort}, gRPC={analyticsClient.Configuration.GrpcPort}"
);
Console.WriteLine($" - Version: {analyticsClient.WeaviateVersion}");
Console.WriteLine();
Console.WriteLine($"Legacy:");
Console.WriteLine(
$" - Endpoint: {legacyClient.Configuration.RestAddress}:{legacyClient.Configuration.RestPort}"
);
Console.WriteLine($" - SSL: {legacyClient.Configuration.UseSsl}");
Console.WriteLine($" - Version: {legacyClient.WeaviateVersion}");
// Now use them with completely different configurations
await UseDemoProductionClient(prodClient);
await UseLocalClient(localClient);
await UseAnalyticsClient(analyticsClient);
}
private async Task UseDemoProductionClient(WeaviateClient client)
{
// DemoProduction has strict timeouts and requires auth
var collection = client.Collections.UseManaged<DemoProduct>();
var results = await collection.Query().Limit(10); // Execute() is optional!
// This will use 30s query timeout configured above
}
private async Task UseLocalClient(WeaviateClient client)
{
// Local has no auth and longer timeouts for debugging
var collection = client.Collections.UseManaged<DemoProduct>();
var results = await collection.Query().Limit(100); // Execute() is optional!
// This will use 300s query timeout - perfect for debugging
}
private async Task UseAnalyticsClient(WeaviateClient client)
{
// Analytics has custom ports and longer timeouts for slow queries
var collection = client.Collections.UseManaged<Metric>();
var results = await collection.Query().Limit(10000); // Execute() is optional!
// This will use 120s query timeout and retry 5 times if it fails
}
}
public class DemoProduct
{
public string? Name { get; set; }
public decimal Price { get; set; }
}
public class Metric
{
public string? Name { get; set; }
public double Value { get; set; }
}