Foundatio.TestHarness
11.0.5
Prefix Reserved
dotnet add package Foundatio.TestHarness --version 11.0.5
NuGet\Install-Package Foundatio.TestHarness -Version 11.0.5
<PackageReference Include="Foundatio.TestHarness" Version="11.0.5" />
paket add Foundatio.TestHarness --version 11.0.5
#r "nuget: Foundatio.TestHarness, 11.0.5"
// Install Foundatio.TestHarness as a Cake Addin #addin nuget:?package=Foundatio.TestHarness&version=11.0.5 // Install Foundatio.TestHarness as a Cake Tool #tool nuget:?package=Foundatio.TestHarness&version=11.0.5
Pluggable foundation blocks for building loosely coupled distributed apps.
Includes implementations in Redis, Azure, AWS, RabbitMQ, Kafka and in memory (for development).
Why Foundatio?
When building several big cloud applications we found a lack of great solutions (that's not to say there isn't solutions out there) for many key pieces to building scalable distributed applications while keeping the development experience simple. Here are a few examples of why we built and use Foundatio:
- Wanted to build against abstract interfaces so that we could easily change implementations.
- Wanted the blocks to be dependency injection friendly.
- Caching: We were initially using an open source Redis cache client but then it turned into a commercial product with high licensing costs. Not only that, but there weren't any in memory implementations so every developer was required to set up and configure Redis.
- Message Bus: We initially looked at NServiceBus (great product) but it had high licensing costs (they have to eat too) but was not OSS friendly. We also looked into MassTransit (another great product) but found Azure support lacking at the time and local set up a pain (for in memory). We wanted a simple message bus that just worked locally or in the cloud.
- Storage: We couldn't find any existing project that was decoupled and supported in memory, file storage or Azure Blob Storage.
To summarize, if you want pain free development and testing while allowing your app to scale, use Foundatio!
Implementations
- Redis - Caching, Storage, Queues, Messaging, Locks, Metrics
- Azure Storage - Storage, Queues
- Azure ServiceBus - Queues, Messaging
- AWS - Storage, Queues, Metrics
- Kafka - Messaging
- RabbitMQ - Messaging
- Minio - Storage
- Aliyun - Storage
- SshNet - Storage
Getting Started (Development)
Foundatio can be installed via the NuGet package manager. If you need help, please open an issue or join our Discord chat room. We’re always here to help if you have any questions!
This section is for development purposes only! If you are trying to use the Foundatio libraries, please get them from NuGet.
- You will need to have Visual Studio Code installed.
- Open the
Foundatio.sln
Visual Studio solution file.
Using Foundatio
The sections below contain a small subset of what's possible with Foundatio. We recommend taking a peek at the source code for more information. Please let us know if you have any questions or need assistance!
Caching
Caching allows you to store and access data lightning fast, saving you exspensive operations to create or get data. We provide four different cache implementations that derive from the ICacheClient
interface:
- InMemoryCacheClient: An in memory cache client implementation. This cache implementation is only valid for the lifetime of the process. It's worth noting that the in memory cache client has the ability to cache the last X items via the
MaxItems
property. We use this in Exceptionless to only keep the last 250 resolved geoip results. - HybridCacheClient: This cache implementation uses both an
ICacheClient
and theInMemoryCacheClient
and uses anIMessageBus
to keep the cache in sync across processes. This can lead to huge wins in performance as you are saving a serialization operation and a call to the remote cache if the item exists in the local cache. - RedisCacheClient: A Redis cache client implementation.
- RedisHybridCacheClient: An implementation of
HybridCacheClient
that uses theRedisCacheClient
asICacheClient
and theRedisMessageBus
asIMessageBus
. - ScopedCacheClient: This cache implementation takes an instance of
ICacheClient
and a stringscope
. The scope is prefixed onto every cache key. This makes it really easy to scope all cache keys and remove them with ease.
Sample
using Foundatio.Caching;
ICacheClient cache = new InMemoryCacheClient();
await cache.SetAsync("test", 1);
var value = await cache.GetAsync<int>("test");
Queues
Queues offer First In, First Out (FIFO) message delivery. We provide four different queue implementations that derive from the IQueue
interface:
- InMemoryQueue: An in memory queue implementation. This queue implementation is only valid for the lifetime of the process.
- RedisQueue: An Redis queue implementation.
- AzureServiceBusQueue: An Azure Service Bus Queue implementation.
- AzureStorageQueue: An Azure Storage Queue implementation.
- SQSQueue: An AWS SQS implementation.
Sample
using Foundatio.Queues;
IQueue<SimpleWorkItem> queue = new InMemoryQueue<SimpleWorkItem>();
await queue.EnqueueAsync(new SimpleWorkItem {
Data = "Hello"
});
var workItem = await queue.DequeueAsync();
Locks
Locks ensure a resource is only accessed by one consumer at any given time. We provide two different locking implementations that derive from the ILockProvider
interface:
- CacheLockProvider: A lock implementation that uses cache to communicate between processes.
- ThrottlingLockProvider: A lock implementation that only allows a certain amount of locks through. You could use this to throttle api calls to some external service and it will throttle them across all processes asking for that lock.
- ScopedLockProvider: This lock implementation takes an instance of
ILockProvider
and a stringscope
. The scope is prefixed onto every lock key. This makes it really easy to scope all locks and release them with ease.
It's worth noting that all lock providers take a ICacheClient
. This allows you to ensure your code locks properly across machines.
Sample
using Foundatio.Lock;
ILockProvider locker = new CacheLockProvider(new InMemoryCacheClient(), new InMemoryMessageBus());
var testLock = await locker.AcquireAsync("test");
// ...
await testLock.ReleaseAsync();
ILockProvider throttledLocker = new ThrottlingLockProvider(new InMemoryCacheClient(), 1, TimeSpan.FromMinutes(1));
var throttledLock = await throttledLocker.AcquireAsync("test");
// ...
await throttledLock.ReleaseAsync();
Messaging
Allows you to publish and subscribe to messages flowing through your application. We provide four different message bus implementations that derive from the IMessageBus
interface:
- InMemoryMessageBus: An in memory message bus implementation. This message bus implementation is only valid for the lifetime of the process.
- RedisMessageBus: A Redis message bus implementation.
- RabbitMQMessageBus: A RabbitMQ implementation.
- KafkaMessageBus: A Kafka implementation.
- AzureServiceBusMessageBus: An Azure Service Bus implementation.
Sample
using Foundatio.Messaging;
IMessageBus messageBus = new InMemoryMessageBus();
await messageBus.SubscribeAsync<SimpleMessageA>(msg => {
// Got message
});
await messageBus.PublishAsync(new SimpleMessageA { Data = "Hello" });
Jobs
Allows you to run a long running process (in process or out of process) without worrying about it being terminated prematurely. We provide three different ways of defining a job, based on your use case:
- Jobs: All jobs must derive from the
IJob
interface. We also have aJobBase
base class you can derive from which provides a JobContext and logging. You can then run jobs by callingRunAsync()
on the job or by creating a instance of theJobRunner
class and calling one of the Run methods. The JobRunner can be used to easily run your jobs as Azure Web Jobs.
Sample
using Foundatio.Jobs;
public class HelloWorldJob : JobBase {
public int RunCount { get; set; }
protected override Task<JobResult> RunInternalAsync(JobContext context) {
RunCount++;
return Task.FromResult(JobResult.Success);
}
}
var job = new HelloWorldJob();
await job.RunAsync(); // job.RunCount = 1;
await job.RunContinuousAsync(iterationLimit: 2); // job.RunCount = 3;
await job.RunContinuousAsync(cancellationToken: new CancellationTokenSource(10).Token); // job.RunCount > 10;
- Queue Processor Jobs: A queue processor job works great for working with jobs that will be driven from queued data. Queue Processor jobs must derive from
QueueJobBase<T>
class. You can then run jobs by callingRunAsync()
on the job or passing it to theJobRunner
class. The JobRunner can be used to easily run your jobs as Azure Web Jobs.
Sample
using Foundatio.Jobs;
public class HelloWorldQueueJob : QueueJobBase<HelloWorldQueueItem> {
public int RunCount { get; set; }
public HelloWorldQueueJob(IQueue<HelloWorldQueueItem> queue) : base(queue) {}
protected override Task<JobResult> ProcessQueueEntryAsync(QueueEntryContext<HelloWorldQueueItem> context) {
RunCount++;
return Task.FromResult(JobResult.Success);
}
}
public class HelloWorldQueueItem {
public string Message { get; set; }
}
// Register the queue for HelloWorldQueueItem.
container.AddSingleton<IQueue<HelloWorldQueueItem>>(s => new InMemoryQueue<HelloWorldQueueItem>());
// To trigger the job we need to queue the HelloWorldWorkItem message.
// This assumes that we injected an instance of IQueue<HelloWorldWorkItem> queue
IJob job = new HelloWorldQueueJob();
await job.RunAsync(); // job.RunCount = 0; The RunCount wasn't incremented because we didn't enqueue any data.
await queue.EnqueueAsync(new HelloWorldWorkItem { Message = "Hello World" });
await job.RunAsync(); // job.RunCount = 1;
await queue.EnqueueAsync(new HelloWorldWorkItem { Message = "Hello World" });
await queue.EnqueueAsync(new HelloWorldWorkItem { Message = "Hello World" });
await job.RunUntilEmptyAsync(); // job.RunCount = 3;
- Work Item Jobs: A work item job will run in a job pool among other work item jobs. This type of job works great for things that don't happen often but should be in a job (Example: Deleting an entity that has many children.). It will be triggered when you publish a message on the
message bus
. The job must derive from theWorkItemHandlerBase
class. You can then run all shared jobs viaJobRunner
class. The JobRunner can be used to easily run your jobs as Azure Web Jobs.
Sample
using System.Threading.Tasks;
using Foundatio.Jobs;
public class HelloWorldWorkItemHandler : WorkItemHandlerBase {
public override async Task HandleItemAsync(WorkItemContext ctx) {
var workItem = ctx.GetData<HelloWorldWorkItem>();
// We can report the progress over the message bus easily.
// To receive these messages just inject IMessageSubscriber
// and Subscribe to messages of type WorkItemStatus
await ctx.ReportProgressAsync(0, "Starting Hello World Job");
await Task.Delay(TimeSpan.FromSeconds(2.5));
await ctx.ReportProgressAsync(50, "Reading value");
await Task.Delay(TimeSpan.FromSeconds(.5));
await ctx.ReportProgressAsync(70, "Reading value");
await Task.Delay(TimeSpan.FromSeconds(.5));
await ctx.ReportProgressAsync(90, "Reading value.");
await Task.Delay(TimeSpan.FromSeconds(.5));
await ctx.ReportProgressAsync(100, workItem.Message);
}
}
public class HelloWorldWorkItem {
public string Message { get; set; }
}
// Register the shared job.
var handlers = new WorkItemHandlers();
handlers.Register<HelloWorldWorkItem, HelloWorldWorkItemHandler>();
// Register the handlers with dependency injection.
container.AddSingleton(handlers);
// Register the queue for WorkItemData.
container.AddSingleton<IQueue<WorkItemData>>(s => new InMemoryQueue<WorkItemData>());
// The job runner will automatically look for and run all registered WorkItemHandlers.
new JobRunner(container.GetRequiredService<WorkItemJob>(), instanceCount: 2).RunInBackground();
// To trigger the job we need to queue the HelloWorldWorkItem message.
// This assumes that we injected an instance of IQueue<WorkItemData> queue
// NOTE: You may have noticed that HelloWorldWorkItem doesn't derive from WorkItemData.
// Foundatio has an extension method that takes the model you post and serializes it to the
// WorkItemData.Data property.
await queue.EnqueueAsync(new HelloWorldWorkItem { Message = "Hello World" });
File Storage
We provide different file storage implementations that derive from the IFileStorage
interface:
- InMemoryFileStorage: An in memory file implementation. This file storage implementation is only valid for the lifetime of the process.
- FolderFileStorage: An file storage implementation that uses the hard drive for storage.
- AzureFileStorage: An Azure Blob storage implementation.
- S3FileStorage: An AWS S3 file storage implementation.
- RedisFileStorage: An Redis file storage implementation.
- MinioFileStorage An Minio file storage implementation.
- AliyunFileStorage: An Aliyun file storage implementation.
- SshNetFileStorage: An SFTP file storage implementation.
We recommend using all of the IFileStorage
implementations as singletons.
Sample
using Foundatio.Storage;
IFileStorage storage = new InMemoryFileStorage();
await storage.SaveFileAsync("test.txt", "test");
string content = await storage.GetFileContentsAsync("test.txt")
Metrics
We provide five implementations that derive from the IMetricsClient
interface:
- InMemoryMetricsClient: An in memory metrics implementation.
- RedisMetricsClient: An Redis metrics implementation.
- StatsDMetricsClient: An statsd metrics implementation.
- MetricsNETClient: An Metrics.NET implementation.
- AppMetricsClient: An AppMetrics implementation.
- CloudWatchMetricsClient: An AWS CloudWatch implementation.
We recommend using all of the IMetricsClient
implementations as singletons.
Sample
IMetricsClient metrics = new InMemoryMetricsClient();
metrics.Counter("c1");
metrics.Gauge("g1", 2.534);
metrics.Timer("t1", 50788);
Sample Application
We have both slides and a sample application that shows off how to use Foundatio.
Thanks to all the people who have contributed
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- BenchmarkDotNet (>= 0.14.0)
- Exceptionless.RandomData (>= 1.2.2)
- Foundatio (>= 11.0.5)
- Foundatio.Xunit (>= 11.0.5)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.TimeProvider.Testing (>= 8.9.1)
- xunit (>= 2.9.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
11.0.5 | 529 | 9/27/2024 |
11.0.4 | 121 | 9/27/2024 |
11.0.3 | 123 | 9/16/2024 |
11.0.2 | 236 | 8/31/2024 |
11.0.1 | 104 | 8/31/2024 |
11.0.0 | 144 | 8/30/2024 |
10.7.1 | 468 | 3/27/2024 |
10.7.0 | 544 | 1/5/2024 |
10.6.1 | 858 | 6/23/2023 |
10.6.0 | 1,025 | 1/1/2023 |
10.5.0 | 1,178 | 5/18/2022 |
10.4.0 | 1,202 | 3/7/2022 |
10.3.1 | 1,081 | 1/20/2022 |
10.3.0 | 1,058 | 1/20/2022 |
10.2.5 | 798 | 12/7/2021 |
10.2.4 | 1,168 | 12/3/2021 |
10.2.3 | 794 | 11/22/2021 |
10.2.2 | 986 | 9/23/2021 |
10.2.1 | 955 | 7/19/2021 |
10.2.0 | 934 | 7/8/2021 |
10.1.4 | 935 | 6/16/2021 |
10.1.3 | 881 | 4/23/2021 |
10.1.2 | 833 | 4/23/2021 |
10.1.1 | 894 | 4/15/2021 |
10.1.0 | 933 | 4/13/2021 |
10.0.2 | 1,024 | 1/20/2021 |
10.0.1 | 1,078 | 11/2/2020 |
10.0.0 | 1,212 | 9/16/2020 |
10.0.0-beta9 | 881 | 8/25/2020 |
10.0.0-beta8 | 989 | 8/3/2020 |
10.0.0-beta7 | 917 | 7/29/2020 |
10.0.0-beta6 | 921 | 7/7/2020 |
10.0.0-beta5 | 1,072 | 6/20/2020 |
10.0.0-beta3 | 916 | 6/14/2020 |
10.0.0-beta2 | 1,002 | 6/6/2020 |
10.0.0-beta10 | 933 | 9/15/2020 |
10.0.0-beta1 | 1,086 | 5/26/2020 |
10.0.0-alpha3 | 852 | 5/5/2020 |
10.0.0-alpha2 | 876 | 4/27/2020 |
10.0.0-alpha1 | 834 | 4/25/2020 |
9.1.1 | 999 | 4/28/2020 |
9.1.0 | 1,052 | 4/28/2020 |
9.0.0 | 1,514 | 1/16/2020 |
8.1.2126 | 1,345 | 8/30/2019 |
8.1.2123 | 1,154 | 8/27/2019 |
8.1.2120 | 1,156 | 8/27/2019 |
8.1.2115 | 1,089 | 8/27/2019 |
8.1.2109 | 1,109 | 8/26/2019 |
8.1.2058 | 1,176 | 5/14/2019 |
8.1.2027 | 1,168 | 4/16/2019 |
8.0.1965 | 1,245 | 2/24/2019 |
8.0.1948 | 1,249 | 2/22/2019 |
7.1.1845 | 1,428 | 11/3/2018 |
7.1.1841 | 1,380 | 11/3/2018 |
7.1.1837 | 1,328 | 11/1/2018 |
7.1.1833 | 1,379 | 11/1/2018 |
7.0.1831 | 1,360 | 11/1/2018 |
7.0.1818 | 1,307 | 10/30/2018 |
7.0.1738 | 1,429 | 9/7/2018 |
7.0.1706 | 1,567 | 5/9/2018 |
6.0.1586 | 1,708 | 11/30/2017 |
5.1.1521 | 1,638 | 9/27/2017 |
5.1.1515 | 1,533 | 9/26/2017 |
5.1.1501 | 1,555 | 9/14/2017 |
5.1.1498 | 1,632 | 8/28/2017 |
5.1.1492 | 1,550 | 8/28/2017 |
5.1.1490 | 1,494 | 8/16/2017 |
5.1.1474 | 1,646 | 8/1/2017 |
5.1.1470 | 1,650 | 7/31/2017 |
5.1.1457 | 1,594 | 6/23/2017 |
5.1.1448 | 1,531 | 5/5/2017 |
5.1.1443 | 1,568 | 5/5/2017 |