Skip to content

Adding Foundatio builders for configuring in IServiceCollection. Various other improvements.#393

Merged
ejsmith merged 7 commits intomainfrom
builders
Jul 10, 2025
Merged

Adding Foundatio builders for configuring in IServiceCollection. Various other improvements.#393
ejsmith merged 7 commits intomainfrom
builders

Conversation

@ejsmith
Copy link
Contributor

@ejsmith ejsmith commented Jul 9, 2025

// configure Foundatio services
builder.Services.AddFoundatio()
    .Storage.UseFolder()
    .Caching.UseInMemory()
    .Locking.UseCache()
    .Messaging.UseInMemory()
    .Queueing.UseInMemory<SampleWork>()
    .AddResilience(b => b.WithPolicy<Sample1Job>(p => p.WithMaxAttempts(5).WithLinearDelay().WithJitter()));


// override services
builder.Services.AddFoundatio()
  .Caching.UseRedis()
  .Messaging.UseRedis();

This pull request introduces resilience policies into the Sample1Job, enhances service configuration in the hosting sample, refines startup action methods, and adds utility methods for replacing singleton services. The changes improve job reliability, simplify service management, and enhance extensibility.

Resilience and Job Enhancements:

Service Configuration Improvements:

  • samples/Foundatio.HostingSample/Program.cs: Added Foundatio services configuration to streamline caching, messaging, locking, and resilience policies. Redis is used for distributed caching and messaging, replacing in-memory implementations. [1] [2]

Startup Action Refinements:

Utility Methods for Service Replacement:

Minor Fixes:

  • src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs and src/Foundatio.TestHarness/Jobs/ThrottledJob.cs: Removed redundant parameters in ThrottlingLockProvider instantiation, simplifying code. [1] [2]

@ejsmith ejsmith changed the title Adding Foundatio builders for configuring in IServiceCollection. Vari… Adding Foundatio builders for configuring in IServiceCollection. Various other improvements. Jul 9, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances Foundatio’s configuration and extensibility by introducing fluent builder APIs, integrating resilience policies, refining startup actions, and adding utilities for replacing singleton services.

  • Adds FoundatioBuilder extensions for configuring caching, storage, messaging, locking, resilience, and serialization via IServiceCollection.
  • Integrates IResiliencePolicyProvider into Sample1Job and refactors ResiliencePolicyProvider defaults and builder methods.
  • Refactors SystemTextJsonSerializer to support custom JsonSerializerOptions and updates related tests.
  • Updates startup action extension methods to return IServiceCollection for improved chaining.

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

File Description
tests/Foundatio.Tests/Serializer/SystemTextJsonSerializerTests.cs Added option-based serializer tests and a benchmark stub.
src/Foundatio/Serializer/SystemTextJsonSerializer.cs Refactored default serialization/deserialization option logic.
src/Foundatio/Resilience/ResiliencePolicyProvider.cs Changed default policy initialization and optional builders.
samples/Foundatio.HostingSample/Program.cs Updated sample to use FoundatioBuilder and resilience APIs.
Comments suppressed due to low confidence (3)

src/Foundatio/Resilience/ResiliencePolicyProvider.cs:24

  • The default policy no longer sets a non-zero MaxAttempts (previously 5), changing retry behavior. Consider restoring MaxAttempts = 5 or making it configurable to avoid unexpected single-attempt defaults.
        _defaultPolicy = new ResiliencePolicy(_loggerFactory.CreateLogger<ResiliencePolicy>(), _timeProvider);

samples/Foundatio.HostingSample/Program.cs:218

  • [nitpick] This second AddFoundatio() call inside ConfigureServices duplicates the builder registration from the top of Program.cs. Consider reusing the existing FoundatioBuilder instance instead of registering twice.
    builder.Services.AddFoundatio()

src/Foundatio/Serializer/SystemTextJsonSerializer.cs:18

  • [nitpick] Falling back to serializeOptions for deserialization may omit converters (e.g., type-inference logic). Consider always merging user-supplied options with _defaultDeserializeOptions to preserve required converters.
        _deserializeOptions = deserializeOptions ?? serializeOptions ?? _defaultDeserializeOptions;

@niemyjski
Copy link
Member

One question that I had is if you call AddQueueing(s => s.AddResilency(...options)) And then you could also override it like this. AddQueueing().UseAzureStorageQueue().UseSQSQueue(f => f.AddResilency(...options)) (example would work better on repos or queues. qon

@ejsmith
Copy link
Contributor Author

ejsmith commented Jul 9, 2025

I don't know what you are asking. Yes, we currently have a few different ways to configure things. This PR is trying to reduce those with the builders.

@ejsmith ejsmith merged commit 3931bc7 into main Jul 10, 2025
3 checks passed
@ejsmith ejsmith deleted the builders branch July 10, 2025 15:46
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