public async Task <decimal?> CalculateValueAddedServiceAsync(QuotationRequest request, Product product, ValueAddedService vas) { await Task.Delay(2000); if (string.IsNullOrWhiteSpace(vas.Formula)) { return(new Random(DateTime.Now.Second).Next(DateTime.Now.Millisecond)); } return(vas.Formula.Length); }
public async Task<decimal?> CalculateValueAddedServiceAsync(QuotationRequest request, Product product, ValueAddedService vas) { var userInputs = string.Join("\r\n", vas.UserInputs.Select(x => $"public const decimal {x.Name} = {x.Value}m;")); var options = ScriptOptions.Default .WithImports("System") .WithImports("System.Math"); var script = CSharpScript .Create<decimal>($@" using System; public class CalcHost {{ const string ITEM_CATEGORY_NAME = ""{request.ItemCategory}""; const bool IS_INTERNATIONAL = {(request.ReceiverCountry != "MY" ? "true" : "false")}; {userInputs} // TODO : like BASE_RATE and stuff public decimal Evaluate() {{ {vas.Formula} }} }} ").ContinueWith(@" var calc = new CalcHost(); ").ContinueWith(@" return calc.Evaluate(); ").WithOptions(options); var result = await script.RunAsync(); return (decimal?)result.ReturnValue; }