Quantcast
Are you the publisher? Claim or contact us about this channel


Embed this content in your HTML

Search

Report adult content:

click to rate:

Account: (login)

More Channels


Showcase


Channel Catalog



Channel Description:

ClearScript is a library that makes it easy to add scripting to your .NET applications. It currently supports JavaScript (via V8 and JScript) and VBScript.

older | 1 | .... | 48 | 49 | (Page 50) | 51 | 52 | .... | 115 | newer

    0 0

    Hi again, guys.

    There is some way to export a array of Number ([1,2]) from JS evaluation to C# array of integer?

    The JS code is:
    ```
    (function (data){ return new RuleExecutionResult(data.BodyFat > 2 && data.Weight > 85, new CLRArray(1,2)) })(data)
    ```

    RuleExecutionResult constructor is:

    ```
    public WorkoutTemplateAssociatedData(bool isValid, int[] associatedObjectiveIds)
    ```

    But ClearScript can't find it because "associatedObjectiveIds" is being binded to a dynamic (as you can see in the attached file), while isValid is correcly binded as true.



    Comments: Hello! This behavior is by design. ClearScript doesn't convert arrays (or most other types) automatically. In general it favors full host and script object access over data conversion. Because host and script objects (even simple collections such as arrays) behave very differently, automatic conversion can be ambiguous and problematic. Full access allows hosts to provide their own conversions that fit their needs. Here's an example of a host-provided array conversion function: ``` C# // C# engine.Script.toIntArray = new Func<dynamic, int[]>(scriptArray => { var length = scriptArray.length; var intArray = new int[length]; for (var i = 0; i < length; i++) intArray[i] = scriptArray[i]; return intArray; }); ``` And here's how you might use it from script code: ``` JavaScript // JavaScript someHostObject.ProcessData(toIntArray([9,8,7,6,5])); ``` Please send any further questions or concerns. Good luck!

    0 0

    Hi again, guys.

    There is some way to export a array of Number ([1,2]) from JS evaluation to C# array of integer?

    The JS code is:
    ```
    (function (data){ return new RuleExecutionResult(data.BodyFat > 2 && data.Weight > 85, [1,2]) })(data)
    ```

    RuleExecutionResult constructor is:

    ```
    public WorkoutTemplateAssociatedData(bool isValid, int[] associatedObjectiveIds)
    ```

    But ClearScript can't find it because "associatedObjectiveIds" is being binded to a dynamic (as you can see in the attached file), while isValid is correcly binded as true.




    0 0

    Hi
    I can successfully run a script, however I want to call another service via a hostfunction.
    the hostfunction works within C# since it instantiates a proxy dll and reads config from web.config... but I suspect one cannot do this via clearscript?

    0 0

    Hello!

    We're a bit unclear about what you mean by "hostfunction".

    There are exceptions, but if you can do it in C#, you should be able to do it in script code as long as you've exposed the relevant .NET types and/or objects to the script engine. Can you provide a sample that does in C# what you'd like to do in script code?

    Thanks!

    0 0

    Hi

    Ok this is the script code ...
                using (var engine = new  JScriptEngine())
                {
                    engine.AddHostObject("host", new RFManagerService());
    
                    StringBuilder script = new StringBuilder();
                    script.Append("function DoWork() {return host.GetCustomerInternal('ContractNo', '15405844');}");
                    
                    engine.Execute(script.ToString());
                    result = engine.Script.DoWork();
                }
    now this script is called in a function in a class that also contains the following method ... this method works from other C# code in the same class, but not from script?
            public Customer GetCustomerInternal(string searchBy, string searchVal)
            {
                string validationMsg = string.Empty;
                //return null;
                return GetCustomer(searchBy, searchVal, out validationMsg);
            }
    
            public Customer GetCustomer(string searchBy, string searchValue, out string validationMsg)
            {
                RFWellnessEngineProxy wellnessEngineProxy = null;
    
                Customer customer = null;
                validationMsg = string.Empty;
    
                try
                {
                    wellnessEngineProxy = new RFWellnessEngineProxy();
                    if (searchBy.Equals("ContractNo") || searchBy.Equals("Contract_No"))
                    {
                        customer = wellnessEngineProxy.GetCustomerByContractNo(int.Parse(searchValue), out validationMsg);
                    }
    
                    return customer;
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    //Close proxy(ies)
                    CloseProxy(wellnessEngineProxy);
                }
                return customer;
            }

    0 0

    Hi again,

    Thanks for posting the code.

    There doesn't appear to be anything wrong here. Your script calls a public method of an exposed object. What are you seeing? Does the script engine throw an exception?

    Thanks!

    0 0

    Here is the error i see ....
    works when calling that method directly though ...

    http://1drv.ms/1sazLYx

    0 0

    here is a link to prove that the method does work when called directly ...
    this i did just before the script attempt, and it does return a customer object. script generates socket timeouts but i do not know why

    http://1drv.ms/1sCeG3t

    0 0

    Hi iwcoetzer,

    At first glance socket timeouts would appear to be completely unrelated to script execution. On the other hand, you're using JScript, which is a COM component, so it may share infrastructure with WCF or one of its dependencies.

    Can you post a stack trace for the innermost (first-chance) exception you're seeing?

    Also, what does your setup look like? Is the service running on the same machine? What protocols or bindings are you using?

    Thanks!

    0 0

    Hi. services are currently all hosted on my dev box. will post innerexception when I am back behind my laptop thanks. would be nice if I can get this to work. I want the script to call that method for input parameters so that on can change the script in future to call other functions as need may arise.

    0 0

    Hi,

    Would it be possible to add a custom exception, so that when it is thrown by host it throws a script exception of the specified type and message.

    For example, in host code:

    ```
    throw new CustomScriptException("RangeError", "custom message");
    ```

    will cause script to execute:

    ```
    throw new RangeError('custom message');
    ```

    Specifically, for the example above I need the exception to be instanceof RangeError.

    Thanks again in advance,
    Ron


    Comments: I have tried this method, however I can only seem to get generic ScriptEngineExceptions with the message set to "Exception has been thrown by the target of an invocation". Any ideas how I could get the original exception from the tryCatch exception handler?

    0 0

    Hi,

    Would it be possible to add a custom exception, so that when it is thrown by host it throws a script exception of the specified type and message.

    For example, in host code:

    ```
    throw new CustomScriptException("RangeError", "custom message");
    ```

    will cause script to execute:

    ```
    throw new RangeError('custom message');
    ```

    Specifically, for the example above I need the exception to be instanceof RangeError.

    Thanks again in advance,
    Ron


    Comments: Hello! ClearScript uses exception chaining to preserve as much error information as possible. You should be able to use [`InnerException`](http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx) and/or [`GetBaseException()`](http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx) to traverse the exception chain. Good luck!

    0 0

    I would like to use ClearScript to compile handlebarsjs templates. Handlebars templates can be full html documents that as far as I know there's no good way to build up in js alone ( they have to be passed in via an API or grabbed via an ajax download or `elment.innerHTML`).

    Is there a way in ClearScript where I can inject a multiline string containing a document into a variable or am I barking up the wrong tree?

    0 0

    Togakangaroo wrote:

    I would like to use ClearScript to compile handlebarsjs templates. Handlebars templates can be full html documents that as far as I know there's no good way to build up in js alone ( they have to be passed in via an API or grabbed via an ajax download or elment.innerHTML).

    Is there a way in ClearScript where I can inject a multiline string containing a document into a variable or am I barking up the wrong tree?

    Sure, the host can pass any string to a script function:
    // C#string document = GetOrCreateDocument();
    engine.Script.processDocument(document);
    ... or store the string as a property in a script object (the global object in this case):
    // C#
    engine.Script.document = document;
    There are no restrictions on string size or contents.

    Good luck!

    0 0

    I would like to use ClearScript to compile handlebarsjs templates. Handlebars templates can be full html documents that as far as I know there's no good way to build up in js alone ( they have to be passed in via an API or grabbed via an ajax download or `elment.innerHTML`).

    Is there a way in ClearScript where I can inject a multiline string containing a document into a variable or am I barking up the wrong tree?
    Comments: Moved to a [discussion topic](https://clearscript.codeplex.com/discussions/568385). Please use the [Discussions](https://clearscript.codeplex.com/discussions) area for questions. Thanks!

    0 0

    I would like to use ClearScript to compile handlebarsjs templates. Handlebars templates can be full html documents that as far as I know there's no good way to build up in js alone ( they have to be passed in via an API or grabbed via an ajax download or `elment.innerHTML`).

    Is there a way in ClearScript where I can inject a multiline string containing a document into a variable or am I barking up the wrong tree?

    0 0

    Hi again, guys.

    There is some way to export a array of Number ([1,2]) from JS evaluation to C# array of integer?

    The JS code is:
    ```
    (function (data){ return new RuleExecutionResult(data.BodyFat > 2 && data.Weight > 85, [1,2]) })(data)
    ```

    RuleExecutionResult constructor is:

    ```
    public WorkoutTemplateAssociatedData(bool isValid, int[] associatedObjectiveIds)
    ```

    But ClearScript can't find it because "associatedObjectiveIds" is being binded to a dynamic (as you can see in the attached file), while isValid is correcly binded as true.




    0 0

    Hi again, guys.

    There is some way to export a array of Number ([1,2]) from JS evaluation to C# array of integer?

    The JS code is:
    ```
    (function (data){ return new RuleExecutionResult(data.BodyFat > 2 && data.Weight > 85, [1,2]) })(data)
    ```

    RuleExecutionResult constructor is:

    ```
    public WorkoutTemplateAssociatedData(bool isValid, int[] associatedObjectiveIds)
    ```

    But ClearScript can't find it because "associatedObjectiveIds" is being binded to a dynamic (as you can see in the attached file), while isValid is correcly binded as true.



    Comments: The behavior is by design, and marcusnaweb hasn't commented in 10 days. Marking as resolved.

    0 0

    oooh man. That's awesome.

    Another question, does Clearscript not provide the JSON object to stringify?

    0 0

    I wrote a simple application, which exposed a custom type (i.e. Foo) to outside. So the user could invoke it's method like this:
    var val1 = new Foo();
    Console.WriteLine(val1.Method());
    From the memory detecting tool, I found the memory increased a lot after each time I ran this simple script. It seems a lot of Microsoft.ClearScript.HostItem objects exists in memory, and never gone away.

    So I am quite worried about this, if a lot of users would create instance of Foo, it would consume very a lot of memories, which seems terrible.

    Any thoughts over this problem?

older | 1 | .... | 48 | 49 | (Page 50) | 51 | 52 | .... | 115 | newer