Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Polly-Contrib/Simmy/llms.txt

Use this file to discover all available pages before exploring further.

MonkeyPolicy is the single static entry-point you use to build every Simmy chaos policy. It is a partial class split across several files in the Polly.Contrib.Simmy namespace, but from a consumer’s perspective it exposes one consistent fluent factory API: call the appropriate Inject* method, pass a configuration delegate, and receive a ready-to-use Polly policy. Every factory method validates that the options object is fully configured before constructing the policy, so misconfigured policies are detected at build-time rather than at execution-time.

Namespace

namespace Polly.Contrib.Simmy

Base classes

Simmy defines four abstract base classes that the concrete policy types inherit from. You do not construct these directly; they are the common building blocks that carry the InjectionRate and Enabled delegates evaluated on every policy execution.

MonkeyPolicy

public abstract partial class MonkeyPolicy : Policy, IMonkeyPolicy
Synchronous base for non-generic monkey policies. Inherits from Polly’s Policy and stores synchronous InjectionRate and Enabled delegates.
PropertyTypeDescription
InjectionRateFunc<Context, CancellationToken, double>Returns the current chaos injection rate (0–1).
EnabledFunc<Context, CancellationToken, bool>Returns whether the policy is active for this execution.

MonkeyPolicy<TResult>

public abstract class MonkeyPolicy<TResult> : Policy<TResult>, IMonkeyPolicy<TResult>
Synchronous base for generic monkey policies that wrap delegates returning TResult.

AsyncMonkeyPolicy

public abstract class AsyncMonkeyPolicy : AsyncPolicy, IMonkeyPolicy
Asynchronous base for non-generic monkey policies. The InjectionRate and Enabled delegates both return Task<T> to allow awaitable configuration logic.
PropertyTypeDescription
InjectionRateFunc<Context, CancellationToken, Task<double>>Async delegate returning the injection rate.
EnabledFunc<Context, CancellationToken, Task<bool>>Async delegate returning whether the policy is active.

AsyncMonkeyPolicy<TResult>

public abstract class AsyncMonkeyPolicy<TResult> : AsyncPolicy<TResult>, IMonkeyPolicy<TResult>
Asynchronous base for generic monkey policies wrapping delegates that return TResult.

Interfaces

IMonkeyPolicy

public interface IMonkeyPolicy : IsPolicy { }
Marker interface implemented by all Simmy policies. Extends Polly’s IsPolicy so monkey policies can be treated uniformly alongside other Polly policies.

IMonkeyPolicy<TResult>

public interface IMonkeyPolicy<TResult> : IMonkeyPolicy { }
Generic marker interface for monkey policies that handle executions returning TResult.

Synchronous factory methods

InjectException

Builds an InjectOutcomePolicy that throws an exception before the wrapped delegate executes whenever the policy is enabled and a random number falls within the configured injection rate.
public static InjectOutcomePolicy InjectException(
    Action<InjectOutcomeOptions<Exception>> configureOptions)
configureOptions
Action<InjectOutcomeOptions<Exception>>
required
A callback that receives a fresh InjectOutcomeOptions<Exception> and must configure at minimum .Fault(...), .InjectionRate(...), and .Enabled(...) before returning.
return
InjectOutcomePolicy
A synchronous Polly policy that injects the configured exception.
var policy = MonkeyPolicy.InjectException(with =>
    with.Fault(new TimeoutException("chaos timeout"))
        .InjectionRate(0.05)
        .Enabled());

policy.Execute(() => CallDependency());

InjectResult<TResult> (result overload)

Builds an InjectOutcomePolicy<TResult> that returns a fake result value instead of executing the wrapped delegate.
public static InjectOutcomePolicy<TResult> InjectResult<TResult>(
    Action<InjectOutcomeOptions<TResult>> configureOptions)
configureOptions
Action<InjectOutcomeOptions<TResult>>
required
A callback that configures a fake TResult value via .Result(...), plus .InjectionRate(...) and .Enabled(...).
return
InjectOutcomePolicy<TResult>
A typed synchronous policy that short-circuits with the configured result.
var policy = MonkeyPolicy.InjectResult<HttpResponseMessage>(with =>
    with.Result(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable))
        .InjectionRate(0.1)
        .Enabled());

HttpResponseMessage response = policy.Execute(() => CallHttpEndpoint());

InjectResult<TResult> (exception overload)

Builds an InjectOutcomePolicy<TResult> that throws an exception in a context where a TResult is expected, configured from an InjectOutcomeOptions<Exception>.
public static InjectOutcomePolicy<TResult> InjectResult<TResult>(
    Action<InjectOutcomeOptions<Exception>> configureOptions)
configureOptions
Action<InjectOutcomeOptions<Exception>>
required
A callback that configures an exception fault via .Fault(...) while the returned policy is typed as InjectOutcomePolicy<TResult>.
return
InjectOutcomePolicy<TResult>
A typed synchronous policy that throws the configured exception.
var policy = MonkeyPolicy.InjectResult<string>(with =>
    with.Fault(new InvalidOperationException("injected fault"))
        .InjectionRate(0.2)
        .Enabled());

InjectLatency

Builds an InjectLatencyPolicy that sleeps for the configured duration before the wrapped delegate executes.
public static InjectLatencyPolicy InjectLatency(
    Action<InjectLatencyOptions> configureOptions)
configureOptions
Action<InjectLatencyOptions>
required
A callback that sets a TimeSpan delay via .Latency(...), plus .InjectionRate(...) and .Enabled(...).
return
InjectLatencyPolicy
A synchronous policy that injects latency before each execution.
var policy = MonkeyPolicy.InjectLatency(with =>
    with.Latency(TimeSpan.FromSeconds(5))
        .InjectionRate(0.3)
        .Enabled());

policy.Execute(() => CallDependency());

InjectLatency<TResult>

Builds an InjectLatencyPolicy<TResult> — the typed variant for delegates returning TResult.
public static InjectLatencyPolicy<TResult> InjectLatency<TResult>(
    Action<InjectLatencyOptions> configureOptions)
configureOptions
Action<InjectLatencyOptions>
required
Same configuration callback as the non-generic overload; the latency configuration is identical, only the policy type differs.
return
InjectLatencyPolicy<TResult>
A typed synchronous latency policy.
var policy = MonkeyPolicy.InjectLatency<string>(with =>
    with.Latency(TimeSpan.FromMilliseconds(500))
        .InjectionRate(0.25)
        .Enabled());

string result = policy.Execute(() => FetchData());

InjectBehaviour

Builds an InjectBehaviourPolicy that executes an arbitrary side-effect action before the wrapped delegate.
public static InjectBehaviourPolicy InjectBehaviour(
    Action<InjectBehaviourOptions> configureOptions)
configureOptions
Action<InjectBehaviourOptions>
required
A callback that registers a side-effect via .Behaviour(...), plus .InjectionRate(...) and .Enabled(...).
return
InjectBehaviourPolicy
A synchronous policy that runs the configured behaviour before every guarded call.
var policy = MonkeyPolicy.InjectBehaviour(with =>
    with.Behaviour(() => Console.WriteLine("chaos behaviour triggered"))
        .InjectionRate(0.1)
        .Enabled());

policy.Execute(() => CallDependency());

Asynchronous factory methods

InjectExceptionAsync

Builds an AsyncInjectOutcomePolicy that asynchronously throws a configured exception.
public static AsyncInjectOutcomePolicy InjectExceptionAsync(
    Action<InjectOutcomeAsyncOptions<Exception>> configureOptions)
configureOptions
Action<InjectOutcomeAsyncOptions<Exception>>
required
A callback that configures an exception via .Fault(...), plus async-capable .InjectionRate(...) and .Enabled(...) delegates.
return
AsyncInjectOutcomePolicy
An asynchronous Polly policy that injects the configured exception.
var policy = MonkeyPolicy.InjectExceptionAsync(with =>
    with.Fault(new HttpRequestException("injected network error"))
        .InjectionRate(0.05)
        .Enabled());

await policy.ExecuteAsync(async ct => await CallApiAsync(ct), CancellationToken.None);

InjectResultAsync<TResult> (result overload)

Builds an AsyncInjectOutcomePolicy<TResult> that returns a fake result asynchronously.
public static AsyncInjectOutcomePolicy<TResult> InjectResultAsync<TResult>(
    Action<InjectOutcomeAsyncOptions<TResult>> configureOptions)
configureOptions
Action<InjectOutcomeAsyncOptions<TResult>>
required
A callback that configures a fake result via .Result(...) and sets async injection-rate and enabled delegates.
return
AsyncInjectOutcomePolicy<TResult>
A typed asynchronous policy that short-circuits with the configured result.
var policy = MonkeyPolicy.InjectResultAsync<HttpResponseMessage>(with =>
    with.Result(new HttpResponseMessage(HttpStatusCode.TooManyRequests))
        .InjectionRate(0.15)
        .Enabled());

var response = await policy.ExecuteAsync(
    async ct => await httpClient.GetAsync("/api/data", ct),
    CancellationToken.None);

InjectResultAsync<TResult> (exception overload)

Builds an AsyncInjectOutcomePolicy<TResult> that throws an exception in a context where a TResult is expected, configured from an InjectOutcomeAsyncOptions<Exception>.
public static AsyncInjectOutcomePolicy<TResult> InjectResultAsync<TResult>(
    Action<InjectOutcomeAsyncOptions<Exception>> configureOptions)
configureOptions
Action<InjectOutcomeAsyncOptions<Exception>>
required
A callback that configures an exception fault via .Fault(...) while the returned policy is typed as AsyncInjectOutcomePolicy<TResult>.
return
AsyncInjectOutcomePolicy<TResult>
A typed asynchronous policy that throws the configured exception.
var policy = MonkeyPolicy.InjectResultAsync<string>(with =>
    with.Fault(new InvalidOperationException("injected async fault"))
        .InjectionRate(0.2)
        .Enabled());

InjectLatencyAsync

Builds an AsyncInjectLatencyPolicy that introduces an async delay before execution.
public static AsyncInjectLatencyPolicy InjectLatencyAsync(
    Action<InjectLatencyAsyncOptions> configureOptions)
configureOptions
Action<InjectLatencyAsyncOptions>
required
A callback that sets a TimeSpan or async delegate via .Latency(...), plus .InjectionRate(...) and .Enabled(...).
return
AsyncInjectLatencyPolicy
An asynchronous policy that injects a delay before each execution.
var policy = MonkeyPolicy.InjectLatencyAsync(with =>
    with.Latency(TimeSpan.FromSeconds(3))
        .InjectionRate(0.2)
        .Enabled());

await policy.ExecuteAsync(async ct => await CallServiceAsync(ct), CancellationToken.None);

InjectLatencyAsync<TResult>

Builds an AsyncInjectLatencyPolicy<TResult> — the typed async variant.
public static AsyncInjectLatencyPolicy<TResult> InjectLatencyAsync<TResult>(
    Action<InjectLatencyAsyncOptions> configureOptions)
configureOptions
Action<InjectLatencyAsyncOptions>
required
Same as the non-generic async overload; returns a policy typed to TResult.
return
AsyncInjectLatencyPolicy<TResult>
A typed asynchronous latency policy.
var policy = MonkeyPolicy.InjectLatencyAsync<string>(with =>
    with.Latency(TimeSpan.FromMilliseconds(800))
        .InjectionRate(0.1)
        .Enabled());

string result = await policy.ExecuteAsync(
    async ct => await FetchStringAsync(ct),
    CancellationToken.None);

InjectBehaviourAsync

Builds an AsyncInjectBehaviourPolicy that runs an async side-effect before the wrapped delegate.
public static AsyncInjectBehaviourPolicy InjectBehaviourAsync(
    Action<InjectBehaviourAsyncOptions> configureOptions)
configureOptions
Action<InjectBehaviourAsyncOptions>
required
A callback that registers an async side-effect via .Behaviour(...), plus .InjectionRate(...) and .Enabled(...).
return
AsyncInjectBehaviourPolicy
An asynchronous policy that executes the configured behaviour before each guarded call.
var policy = MonkeyPolicy.InjectBehaviourAsync(with =>
    with.Behaviour(async () =>
        {
            await Task.Delay(50);
            logger.LogWarning("Chaos behaviour injected");
        })
        .InjectionRate(0.1)
        .Enabled());

await policy.ExecuteAsync(async ct => await CallServiceAsync(ct), CancellationToken.None);

Build docs developers (and LLMs) love