forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyInjection.cs
38 lines (33 loc) · 1.51 KB
/
DependencyInjection.cs
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
// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.KernelMemory.Orchestration.RabbitMQ;
using Microsoft.KernelMemory.Pipeline.Queue;
#pragma warning disable IDE0130 // reduce number of "using" statements
// ReSharper disable once CheckNamespace - reduce number of "using" statements
namespace Microsoft.KernelMemory;
public static partial class KernelMemoryBuilderExtensions
{
public static IKernelMemoryBuilder WithRabbitMQOrchestration(this IKernelMemoryBuilder builder, RabbitMqConfig config)
{
builder.Services.AddRabbitMQOrchestration(config);
return builder;
}
}
public static partial class DependencyInjection
{
public static IServiceCollection AddRabbitMQOrchestration(this IServiceCollection services, RabbitMqConfig config)
{
IQueue QueueFactory(IServiceProvider serviceProvider)
{
return serviceProvider.GetService<RabbitMQPipeline>()
?? throw new KernelMemoryException("Unable to instantiate " + typeof(RabbitMQPipeline));
}
// The orchestrator uses multiple queue clients, each linked to a specific queue,
// so it requires a factory rather than a single queue injected to the ctor.
return services
.AddSingleton<RabbitMqConfig>(config)
.AddTransient<RabbitMQPipeline>()
.AddSingleton<QueueClientFactory>(serviceProvider => new QueueClientFactory(() => QueueFactory(serviceProvider)));
}
}