Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing some script docs is outdated #11814

Closed
MikeAlhayek opened this issue Jun 6, 2022 · 1 comment · Fixed by #11829
Closed

Executing some script docs is outdated #11814

MikeAlhayek opened this issue Jun 6, 2022 · 1 comment · Fixed by #11829
Milestone

Comments

@MikeAlhayek
Copy link
Member

MikeAlhayek commented Jun 6, 2022

The example in the docs on how to evaluate code on this page seems to be outdated or does not work.

The example shows this way is to get current date using script.

var scriptingManager = serviceProvider.GetService<IScriptingManager>();
var date = scriptingManager.Evaluate("js: new Date().toISOString()");

However, the IScriptingManager does not expose a method that accepts string only.

Maybe the example should be as follow? Not sure if there is an easier way.

var scriptingManager = serviceProvider.GetService<IScriptingManager>();
var engine = scriptingManager.GetScriptingEngine("js");
var scope = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), serviceProvider, null, null);
var date = engine.Evaluate("js: new Date().toISOString()");

Also it may be helpful to explain how to evaluate parameters using code? For example, how can I get the AdminUsername?

Executing engine.Evaluate(scope, "js: parameters('AdminUsername')" throws an exception `parameters is not defined. I added the following code to get it to work. Not sure if this is the right/best way to do it

var engine = _scriptingManager.GetScriptingEngine("js");
var methodProviders = _scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()).ToList();
var environment = new Dictionary<string, object>();
await _environmentProviders.OrderBy(x => x.Order).InvokeAsync((provider, env) => provider.PopulateEnvironmentAsync(env), environment, _logger);

 methodProviders.AddRange(new ParametersMethodProvider(environment).GetMethods());

 var scope = engine.CreateScope(methodProviders, _serviceProvider, null, null);

var adminId = engine.Evaluate(scope, "js: parameters('AdminUserId')");
var adminName = engine.Evaluate(scope, "js: parameters('AdminUsername')");
@hishamco
Copy link
Member

hishamco commented Jun 7, 2022

Could you please submit a PR to make it up to date?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment