### C# Foundatio JobBase Example Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Demonstrates creating a basic job by inheriting from JobBase, implementing RunInternalAsync, and running the job. ```csharp using System.Threading.Tasks; using Foundatio.Jobs; public class HelloWorldJob : JobBase { public int RunCount { get; set; } protected override Task RunInternalAsync(JobContext context) { RunCount++; return Task.FromResult(JobResult.Success); } } // Usage examples: // 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; ``` -------------------------------- ### C# Foundatio QueueJobBase Example Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Illustrates a job that processes entries from a queue, inheriting from QueueJobBase and implementing ProcessQueueEntryAsync. Includes setup and triggering via a queue. ```csharp using System.Threading.Tasks; using Foundatio.Jobs; using Foundatio.Queues; public class HelloWorldQueueItem { public string Message { get; set; } } public class HelloWorldQueueJob : QueueJobBase { public int RunCount { get; set; } public HelloWorldQueueJob(IQueue queue) : base(queue) {} protected override Task ProcessQueueEntryAsync(QueueEntryContext context) { RunCount++; return Task.FromResult(JobResult.Success); } } // Usage examples: // Register the queue for HelloWorldQueueItem. // container.AddSingleton>(s => new InMemoryQueue()); // IJob job = new HelloWorldQueueJob(queue); // await job.RunAsync(); // job.RunCount = 0; (no data enqueued) // await queue.EnqueueAsync(new HelloWorldQueueItem { Message = "Hello World" }); // await job.RunAsync(); // job.RunCount = 1; // await queue.EnqueueAsync(new HelloWorldQueueItem { Message = "Hello World" }); // await queue.EnqueueAsync(new HelloWorldQueueItem { Message = "Hello World" }); // await job.RunUntilEmptyAsync(); // job.RunCount = 3; ``` -------------------------------- ### C# Foundatio WorkItemHandlerBase Example Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Shows how to create a work item handler by inheriting from WorkItemHandlerBase, processing data, and reporting progress. ```csharp using System; using System.Threading.Tasks; using Foundatio.Jobs; public class HelloWorldWorkItem { public string Data { get; set; } } public class HelloWorldWorkItemHandler : WorkItemHandlerBase { public override async Task HandleItemAsync(WorkItemContext ctx) { var workItem = ctx.GetData(); // To receive progress messages, inject IMessageSubscriber and subscribe to WorkItemStatus messages. 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, "Completed Hello World Job"); } } ``` -------------------------------- ### Registering and Running Foundatio Jobs Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Demonstrates how to register work item handlers and dependency injection for Foundatio's job processing system. It shows the setup for a shared job runner and how to enqueue messages. ```csharp var handlers = new WorkItemHandlers(); handlers.Register(); // Register the handlers with dependency injection. container.AddSingleton(handlers); // Register the queue for WorkItemData. container.AddSingleton>(s => new InMemoryQueue()); // The job runner will automatically look for and run all registered WorkItemHandlers. new JobRunner(container.GetRequiredService(), instanceCount: 2).RunInBackground(); ``` -------------------------------- ### IFileStorage Sample Usage Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md A basic example demonstrating the usage of the IFileStorage interface, specifically with InMemoryFileStorage. It shows how to save a file and retrieve its contents. ```csharp using Foundatio.Storage; IFileStorage storage = new InMemoryFileStorage(); await storage.SaveFileAsync("test.txt", "test"); string content = await storage.GetFileContentsAsync("test.txt"); ``` -------------------------------- ### C# Queue Client Usage Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Illustrates the fundamental operations of the IQueue interface for enqueuing and dequeuing work items. This example shows how to add data to a queue and process it asynchronously. ```csharp using Foundatio.Queues; // Define a simple work item structure public class SimpleWorkItem { public string Data { get; set; } } // Initialize an in-memory queue for demonstration IQueue queue = new InMemoryQueue(); // Enqueue a work item await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello" }); // Dequeue a work item var workItem = await queue.DequeueAsync(); // Process the dequeued work item (e.g., workItem.Data will be "Hello") // Remember to complete the work item to remove it from the queue // await queue.CompleteAsync(workItem); ``` -------------------------------- ### IMetricsClient Sample Usage Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Illustrates the basic usage of the IMetricsClient interface with InMemoryMetricsClient. It shows how to record counters, gauges, and timers. ```csharp IMetricsClient metrics = new InMemoryMetricsClient(); metrics.Counter("c1"); metrics.Gauge("g1", 2.534); metrics.Timer("t1", 50788); ``` -------------------------------- ### Foundatio Messaging Overview Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Illustrates publishing and subscribing to messages using Foundatio's messaging implementations. The sample uses an InMemoryMessageBus for local process communication. ```csharp using Foundatio.Messaging; // Define a sample message type public class SimpleMessageA { public string Data { get; set; } } // Initialize an in-memory message bus IMessageBus messageBus = new InMemoryMessageBus(); // Subscribe to messages of type SimpleMessageA await messageBus.SubscribeAsync(msg => { // Process received message Console.WriteLine($"Received: {msg.Data}"); }); // Publish a message of type SimpleMessageA await messageBus.PublishAsync(new SimpleMessageA { Data = "Hello" }); ``` -------------------------------- ### C# Cache Client Usage Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Demonstrates basic usage of the ICacheClient interface for setting and retrieving cached values asynchronously. This snippet highlights the core operations available across different cache implementations. ```csharp using Foundatio.Caching; // Initialize an in-memory cache client for demonstration ICacheClient cache = new InMemoryCacheClient(); // Set a value with a key await cache.SetAsync("test", 1); // Get the value associated with the key var value = await cache.GetAsync("test"); // The 'value' variable will now hold the integer 1. ``` -------------------------------- ### Foundatio Locks Overview Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Demonstrates the usage of Foundatio's locking providers like CacheLockProvider and ThrottlingLockProvider. These implementations ensure exclusive resource access across processes, utilizing an ICacheClient for inter-process communication. ```csharp using Foundatio.Lock; // Initialize with InMemoryCacheClient and InMemoryMessageBus ILockProvider locker = new CacheLockProvider(new InMemoryCacheClient(), new InMemoryMessageBus()); var testLock = await locker.AcquireAsync("test"); // ... perform operations while holding the lock await testLock.ReleaseAsync(); // Initialize ThrottlingLockProvider to limit access to 1 per minute ILockProvider throttledLocker = new ThrottlingLockProvider(new InMemoryCacheClient(), 1, TimeSpan.FromMinutes(1)); var throttledLock = await throttledLocker.AcquireAsync("test"); // ... perform operations with throttling await throttledLock.ReleaseAsync(); ``` -------------------------------- ### Enqueuing a Work Item Message Source: https://github.com/foundatiofx/foundatio.azurestorage/blob/main/README.md Shows how to enqueue a custom work item message, such as HelloWorldWorkItem, into the Foundatio queue. It highlights the use of an extension method for serializing custom models to WorkItemData. ```csharp // To trigger the job we need to queue the HelloWorldWorkItem message. // This assumes that we injected an instance of IQueue 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" }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.