Skip to content

Parallel Rule Configuration

Arif Yayalar (@ayayalar) edited this page Aug 24, 2018 · 1 revision

Parallel rules can be configured via the ParallelConfiguration property. The decision to Cancel on CancellationTokenSource must be made in the BeforeInvokeAsync method. Otherwise it will be ignored.

The following are the available configuration options that can be set in the InitializeAsync method:

TaskCreationOptions : set to TaskCreationOptions.None by default.

TaskScheduler : set to TaskScheduler.Default by default.

CancellationTokenSource : is not set.

NestedParallelRulesInherits : if set to true, nested parallel rule(s) inherits the TaskCreationOptions and the TaskScheduler from the containing parallel rule.

Example
class UpdateTaxAsync : RuleAsync<Order>
{
    public async override Task InitializeAsync()
    {	
	IsParallel = true;
     
        ParellelConfiguration.TaskCreationOptions = TaskCreationOptions.PreferFairness;
	ParellelConfiguration.CancellationTokenSource = new CancellationTokenSource();
        ParellelConfiguration.TaskScheduler = TaskScheduler.Current;

        await base.InitializeAsync();
    }

    public async override Task BeforeInvokeAsync()
    {
        if (Model.Price == 0)
	{
            // Cancels calling InvokeAsync() if Price equal 0
	    ParellelConfiguration.CancellationTokenSource.Cancel();
	}
        await base.BeforeInvokeAsync();
    }

    public async override Task<IRuleResult> InvokeAsync()
    {		
	Model.Price = UpdatePriceWithTax();			
        return await RuleResult.Nil();
    }
}

Invoke Asynchronous/Parallel Rule(s)

var ruleResults = await RuleEngine<Order>.GetInstance(order)
    .ApplyRules(new UpdateTaxAsync())
    .ExecuteAsync()