Skip to content

Conversation

g7ed6e
Copy link
Contributor

@g7ed6e g7ed6e commented May 29, 2024

Resolves #4323

I had to deal with RS0027 and RS0028 and finally moved from optional parameters to explicit parameters overloads as parameter types differ.

sample usage:

var builder = Host.CreateApplicationBuilder(args);

builder.AddServiceDefaults();

builder.Services.AddSingleton<ISchemaRegistryClient>(provider =>
{
    var schemaRegistryConfig = new SchemaRegistryConfig
    {
        Url = "http://localhost:8081"
    };
    return new CachedSchemaRegistryClient(schemaRegistryConfig);
});
builder.AddKafkaProducer<Null, User>("kafka", configureBuilder: (provider, producerBuilder) =>
{
    var schemaRegistryClient = provider.GetRequiredService<ISchemaRegistryClient>();
    producerBuilder.SetValueSerializer(new AvroSerializer<User>(schemaRegistryClient));
});

builder.Services.AddHostedService<ContinuousProducerWorker>();

builder.Build().Run();
Microsoft Reviewers: Open in CodeFlow

@ghost ghost added the area-integrations Issues pertaining to Aspire Integrations packages label May 29, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 29, 2024
@g7ed6e
Copy link
Contributor Author

g7ed6e commented May 30, 2024

The public API modification should be binary backward compatible I followed guidance from https://github.com/dotnet/roslyn/blob/main/docs/Adding%20Optional%20Parameters%20in%20Public%20API.md.

@davidfowl davidfowl requested a review from eerhardt June 4, 2024 07:27
@eerhardt
Copy link
Member

eerhardt commented Jun 4, 2024

Can you add some unit tests for all new API and functionality?

@eerhardt
Copy link
Member

eerhardt commented Jun 4, 2024

The public API modification should be binary backward compatible

@joperezr - thoughts on getting the package validation https://learn.microsoft.com/dotnet/fundamentals/apicompat/package-validation/overview hooked up in the repo now that we have shipped 8.0?

static Microsoft.Extensions.Hosting.AspireKafkaConsumerExtensions.AddKeyedKafkaConsumer<TKey, TValue>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Aspire.Confluent.Kafka.KafkaConsumerSettings!>? configureSettings = null, System.Action<Confluent.Kafka.ConsumerBuilder<TKey, TValue>!>? configureBuilder = null) -> void
static Microsoft.Extensions.Hosting.AspireKafkaProducerExtensions.AddKafkaProducer<TKey, TValue>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.Confluent.Kafka.KafkaProducerSettings!>? configureSettings = null, System.Action<Confluent.Kafka.ProducerBuilder<TKey, TValue>!>? configureBuilder = null) -> void
static Microsoft.Extensions.Hosting.AspireKafkaProducerExtensions.AddKeyedKafkaProducer<TKey, TValue>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! name, System.Action<Aspire.Confluent.Kafka.KafkaProducerSettings!>? configureSettings = null, System.Action<Confluent.Kafka.ProducerBuilder<TKey, TValue>!>? configureBuilder = null) -> void
static Microsoft.Extensions.Hosting.AspireKafkaConsumerExtensions.AddKafkaConsumer<TKey, TValue>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName) -> void
Copy link
Member

Choose a reason for hiding this comment

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

This is a lot of new APIs (we went from 4 to 24). Are all these needed?

Copy link
Contributor Author

@g7ed6e g7ed6e Jun 5, 2024

Choose a reason for hiding this comment

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

We previously have 4 APIs with 4 parameters from which 2 were optional. To ensure backwards compatibility I dropped the default values and exposed each combination of the previous APIs. we here fo from 4 to 4 * 4 APIs.

The new overloads replace Action<ProducerBuilder<TKey, TValue>> and Action<ConsumerBuilder<TKey, TValue>> parameters by Action<IServiceProvider, ProducerBuilder<TKey, TValue>> and Action<IServiceProvider, ConsumerBuilder<TKey, TValue>> which add 4 * 2 APIs.

At both side, consumer and producer, the APIs forward all the call to the same internal implementation.

@g7ed6e g7ed6e force-pushed the feature/pass-service-provider-to-configure-builder branch from 796ddc3 to 0f67a27 Compare June 5, 2024 09:26
@g7ed6e g7ed6e force-pushed the feature/pass-service-provider-to-configure-builder branch from 0f67a27 to e8ec081 Compare June 20, 2024 07:20
@g7ed6e g7ed6e requested a review from eerhardt June 20, 2024 07:26
@g7ed6e
Copy link
Contributor Author

g7ed6e commented Jun 20, 2024

@mitchdenny / @eerhardt : PR is updated with latest changed from main :)

edit: build is broken stating that some shipped public API no longer exist. I've followed https://github.com/dotnet/roslyn/blob/main/docs/Adding%20Optional%20Parameters%20in%20Public%20API.md but it looks that the analyzer is failing to map the new methods to existing ones. @joperezr may i ask some guidance about how to solve this.

@eerhardt
Copy link
Member

@mitchdenny / @eerhardt : PR is updated with latest changed from main :)

edit: build is broken stating that some shipped public API no longer exist. I've followed https://github.com/dotnet/roslyn/blob/main/docs/Adding%20Optional%20Parameters%20in%20Public%20API.md but it looks that the analyzer is failing to map the new methods to existing ones. @joperezr may i ask some guidance about how to solve this.

I resolved this build error by adding the *REMOVED* heading to the APIs. The analyzer considers this a change because the default values were dropped.

@g7ed6e
Copy link
Contributor Author

g7ed6e commented Jun 21, 2024

@mitchdenny / @eerhardt : PR is updated with latest changed from main :)
edit: build is broken stating that some shipped public API no longer exist. I've followed https://github.com/dotnet/roslyn/blob/main/docs/Adding%20Optional%20Parameters%20in%20Public%20API.md but it looks that the analyzer is failing to map the new methods to existing ones. @joperezr may i ask some guidance about how to solve this.

I resolved this build error by adding the *REMOVED* heading to the APIs. The analyzer considers this a change because the default values were dropped.

Thank you! i did not know that.

Comment on lines 68 to 82
/// <inheritdoc cref="AddKafkaProducerInternal{TKey, TValue}(IHostApplicationBuilder, string, Action{KafkaProducerSettings}?, Action{IServiceProvider, ProducerBuilder{TKey, TValue}}?, string, string?)"/>
public static void AddKeyedKafkaProducer<TKey, TValue>(this IHostApplicationBuilder builder, string name, Action<ProducerBuilder<TKey, TValue>>? configureBuilder)
{
ArgumentException.ThrowIfNullOrEmpty(name);
AddKafkaProducerInternal<TKey, TValue>(builder, $"{DefaultConfigSectionName}:{name}", null, Wrap(configureBuilder), connectionName: name, serviceKey: name);
}

/// <inheritdoc cref="AddKafkaProducerInternal{TKey, TValue}(IHostApplicationBuilder, string, Action{KafkaProducerSettings}?, Action{IServiceProvider, ProducerBuilder{TKey, TValue}}?, string, string?)"/>
public static void AddKeyedKafkaProducer<TKey, TValue>(this IHostApplicationBuilder builder, string name, Action<IServiceProvider, ProducerBuilder<TKey, TValue>>? configureBuilder)
{
ArgumentException.ThrowIfNullOrEmpty(name);
AddKafkaProducerInternal<TKey, TValue>(builder, $"{DefaultConfigSectionName}:{name}", null, configureBuilder, connectionName: name, serviceKey: name);
}

/// <inheritdoc cref="AddKafkaProducerInternal{TKey, TValue}(IHostApplicationBuilder, string, Action{KafkaProducerSettings}?, Action{IServiceProvider, ProducerBuilder{TKey, TValue}}?, string, string?)"/>
Copy link
Member

Choose a reason for hiding this comment

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

You don't want to inhertdoc from AddKafkaProducerInternal. That method doesn't have any xml doc.

Copy link
Contributor Author

@g7ed6e g7ed6e Jun 22, 2024

Choose a reason for hiding this comment

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

@eerhardt good catch. sorry for that. this is now fixed.

@dotnet-bot
Copy link
Contributor

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Aspire.Confluent.Kafka Line 90 87.63 🔻
Microsoft.Extensions.ServiceDiscovery Branch 81 80.13 🔻
Microsoft.Extensions.ServiceDiscovery Line 81 80.94 🔻

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=716654&view=codecoverage-tab

@mitchdenny
Copy link
Member

Looks like we took a dip in code coverage.

@g7ed6e
Copy link
Contributor Author

g7ed6e commented Jun 24, 2024

Looks like we took a dip in code coverage.

Hum, this PR replace methods with default parameters by methods with all combinations of those parameters. The code coverage was not really meaningful for some parameter combinations.
I can write test to reach 90% code coverage.

@g7ed6e g7ed6e force-pushed the feature/pass-service-provider-to-configure-builder branch from 059a6f3 to c368c79 Compare June 25, 2024 15:04
@g7ed6e
Copy link
Contributor Author

g7ed6e commented Jun 26, 2024

@eerhardt @mitchdenny i updated UT to cover all parameter combinations as it was before that PR.

@mitchdenny
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mitchdenny
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@mitchdenny mitchdenny left a comment

Choose a reason for hiding this comment

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

LGTM. The overload explosion is unfortunate but lessons learned about API design I say ;)

@g7ed6e
Copy link
Contributor Author

g7ed6e commented Jul 8, 2024

LGTM. The overload explosion is unfortunate but lessons learned about API design I say ;)

I agree.. I guess this is the drawback of defaulting multiple parameters!

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix @g7ed6e.

@eerhardt eerhardt merged commit 3fb0716 into dotnet:main Jul 8, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-integrations Issues pertaining to Aspire Integrations packages community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kafka component - using a service registered in DI to build serializer / deserializer is not possible
5 participants