{"id":37227153,"url":"https://github.com/kmcclellan/kafka-di","last_synced_at":"2026-01-15T03:19:08.269Z","repository":{"id":63568238,"uuid":"271915833","full_name":"kmcclellan/kafka-di","owner":"kmcclellan","description":"An extension of Confluent's Kafka client for use with Microsoft.Extensions.DependencyInjection (and friends).","archived":false,"fork":false,"pushed_at":"2026-01-10T15:40:50.000Z","size":180,"stargazers_count":15,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-01-14T07:14:18.967Z","etag":null,"topics":["confluent-kafka","dependency-injection","ioc","kafka","logging"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Confluent.Kafka.DependencyInjection","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kmcclellan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-06-13T00:51:50.000Z","updated_at":"2026-01-10T15:35:25.000Z","dependencies_parsed_at":"2025-06-05T21:35:18.348Z","dependency_job_id":null,"html_url":"https://github.com/kmcclellan/kafka-di","commit_stats":{"total_commits":38,"total_committers":3,"mean_commits":"12.666666666666666","dds":0.3421052631578947,"last_synced_commit":"06b07bc683cc612a96775d134af36b1ece881b46"},"previous_names":["kmcclellan/kafka-hosting"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/kmcclellan/kafka-di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmcclellan%2Fkafka-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmcclellan%2Fkafka-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmcclellan%2Fkafka-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmcclellan%2Fkafka-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kmcclellan","download_url":"https://codeload.github.com/kmcclellan/kafka-di/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmcclellan%2Fkafka-di/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28442262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:55:22.719Z","status":"online","status_checked_at":"2026-01-15T02:00:08.019Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["confluent-kafka","dependency-injection","ioc","kafka","logging"],"created_at":"2026-01-15T03:19:07.538Z","updated_at":"2026-01-15T03:19:08.265Z","avatar_url":"https://github.com/kmcclellan.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kafka Dependency Injection\nAn extension of [Confluent's Kafka client](https://github.com/confluentinc/confluent-kafka-dotnet) for use with `Microsoft.Extensions.DependencyInjection` (and friends).\n\n### Features\n* Configure/resolve Kafka clients using the service container.\n* Load client config properties using `Microsoft.Extensions.Configuration`.\n* Automatically log client events using `Microsoft.Extensions.Logging`.\n* Extend a base hosted service to consume/process Kafka messages with `Microsoft.Extensions.Hosting`.\n\n## Installation\n\nAdd the NuGet package to your project:\n\n    $ dotnet add package Confluent.Kafka.DependencyInjection\n\n## Usage\n\n### Resolving clients\n\nKafka DI works out-of-the-box after registering services with an `IServiceCollection`.\n\n```c#\nservices.AddKafkaClient();\nservices.AddTransient\u003cMyService\u003e();\n```\n\nInject Kafka clients via constructor.\n\n```c#\npublic MyService(IProducer\u003cstring, byte[]\u003e producer, IConsumer\u003cIgnore, MyType\u003e consumer, IAdminClient adminClient)\n{\n    // Clients are singletons managed by the container.\n    Producer = producer;\n    Consumer = consumer;\n    AdminClient = adminClient;\n}\n```\n\n### Configuring clients\n\nClient configuration properties are bound to the `Kafka` section of .NET configuration providers, such as `appsettings.json`.\n\n```json\n{\n  \"Kafka\": {\n    \"Producer\": {\n      \"bootstrap.servers\": \"localhost:9092\",\n      \"transactional.id\": \"example\"\n    },\n    \"Consumer\": {\n      \"bootstrap.servers\": \"localhost:9092\",\n      \"group.id\": \"example\"\n    },\n    \"Admin\": {\n      \"bootstrap.servers\": \"localhost:9092\"\n    }\n  }\n}\n```\n\nYou can also specify configuration properties using the options pattern.\n\n```c#\n// Prepare consumers for manual offset storage.\nservices.Configure\u003cConsumerConfig\u003e(x =\u003e x.EnableAutoOffsetStore = false);\n```\n\nConfigure serialization by registering the appropriate interface.\n\n```c#\n// \"Open\" generic registrations apply to all key/value types (except built-in types).\nservices.AddTransient(typeof(IAsyncDeserializer\u003c\u003e), typeof(JsonDeserializer\u003c\u003e));\n\n// Configure schema registry (required by Confluent serializers).\nservices.AddSingleton\u003cISchemaRegistryClient\u003e(\n    x =\u003e new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = \"localhost:8081\" }));\n```\n\nFor advanced scenarios, implement `IClientBuilderSetup` to customize clients further.\n\n```c#\nclass MyClientSetup : IClientBuilderSetup\n{\n    public void Apply\u003cTKey, TValue\u003e(ProducerBuilder\u003cTKey, TValue\u003e builder)\n    {\n        builder.SetStatisticsHandler(OnStatistics);\n    }\n\n    public void Apply\u003cTKey, TValue\u003e(ConsumerBuilder\u003cTKey, TValue\u003e builder)\n    {\n        builder.SetStatisticsHandler(OnStatistics);\n    }\n\n    public void Apply(AdminClientBuilder builder)\n    {\n        builder.SetStatisticsHandler(OnStatistics);\n    }\n\n    void OnStatistics(IClient client, string statistics)\n    {\n        Console.WriteLine($\"New statistics available for {client.Name}\");\n    }\n}\n```\n\nRegister custom setup with services.\n\n```c#\nservices.AddTransient\u003cIClientBuilderSetup, MyClientSetup\u003e();\n```\n\n### Consumer hosting\n\nOnce the client is configured, implement `ConsumerService` to integrate with the .NET host.\n\n```c#\nclass MyWorker(\n    IConsumer\u003cIgnore, MyType\u003e consumer,\n    IOptions\u003cConsumerHostingOptions\u003e options,\n    ILogger\u003cMyWorker\u003e logger) :\n    ConsumerService\u003cIgnore, MyType\u003e(consumer, options, logger)\n{\n    protected override ValueTask ProcessAsync(\n        ConsumeResult\u003cIgnore, MyType\u003e result,\n        CancellationToken cancellationToken)\n    {\n        // Process the message.\n        return ValueTask.CompletedTask;\n    }\n}\n```\n\nRegister the service with the container.\n\n```c#\nbuilder.Services.AddHostedService\u003cMyWorker\u003e();\n\n// Bind consumer hosting configuration.\nvar hostingOptions = builder.Services.AddOptions\u003cConsumerHostingOptions\u003e()\n    .BindConfiguration(\"Kafka:Hosting\");\n```\n\nHosted consumer features:\n\n* Configurable parallelism while preserving order for each Kafka message key.\n* Parallel-safe offset storage to achieve [\"at least once\" delivery semantics](https://docs.confluent.io/kafka/design/delivery-semantics.html#consumer-receipt).\n\n```json\n{\n  \"Kafka\": {\n    \"Consumer\": {\n      \"bootstrap.servers\": \"localhost:9092\",\n      \"group.id\": \"example\",\n      \"enable.auto.offset.store\": \"false\"\n    },\n    \"Hosting\": {\n      \"Disabled\": false,\n      \"Subscription\": [ \"my-topic\" ],\n      \"MaxDegreeOfParallelism\": 10,\n      \"StoreProcessedOffsets\": true\n    }\n  }\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmcclellan%2Fkafka-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkmcclellan%2Fkafka-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmcclellan%2Fkafka-di/lists"}