0

I am trying to load javascript in my app using ChakraBridge and I followed the steps given in the following block

Using JavaScript Framework in UWP

and also on GitHub JsBridge

I added ChakraBridge.winmd as reference in my project and it works perfectly fine when debug mode is Script. When I change it to Managed Only, it gives me exception and same for Release mode.

Can someone suggest what is the issue?

Here is the example. I am trying to assign default date in DatePicker in my project using ChakraBridge API

if (!string.IsNullOrEmpty(javascriptFunction))
{
    try
    {
        ChakraHost dateHost = new ChakraHost();
        string dateValue = dateHost.RunScript(javascriptFunction);
        var finalDate = DateTime.ParseExact(dateValue, "dd-MM-yyyy", CultureInfo.InvariantCulture);
        datePicker.Date = finalDate.Date;
    }
    catch(Exception ex)
    {
         Debug.WriteLine(ex.Message);
    }
}

The javascript function is

function executeScript(){var now = new Date(new Date().getTime() - (7*24*60*60*1000)); return ('0'+now.getDate()).substr(-2)+'-'+('0'+(now.getMonth()+1)).substr(-2)+'-'+now.getFullYear();} executeScript();

StackTrace I get

at ChakraBridge.ChakraHost..ctor()
at MCS.MCSDynamicViewBuilder.<GenerateDynamicControlforMobile>d__10.MoveNext()

Also, it works fine when I debug in my laptop machine and When I debug in mobile or in Mobile Emulator, it gives me this exception. Is it due to dist folder available on my laptop machine from where I refrence ChakraBridge.winmd?

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
Kinjan Bhavsar
  • 1,439
  • 28
  • 58
  • I can't reproduce your issue in my side. Could you share a [mcve]? – Jay Zuo Aug 19 '16 at 08:53
  • @JayZuo-MSFT I have added example. Create a new project. Refrence `ChakraBridge.winmd` as refrence and try this with debug mode as *Managed Only*. This works when Debug of Application Process is *Script *but doesn't work with *Managed Only* and *Release mode.* – Kinjan Bhavsar Aug 19 '16 at 09:09
  • If it works in Script-mode debugging then you know it is not a critical exception. Be sure to configure the debugger correctly, use Debug > Windows > Exception Settings and reset the checkboxes if they are ticked. If you still have trouble the *do* post the exception's stack trace. – Hans Passant Aug 19 '16 at 09:12
  • In Exception Settings, you are asking me to uncheck `JavaScript Runtime Exceptions`? – Kinjan Bhavsar Aug 19 '16 at 09:14
  • I tried with your code, it also works well. What's the exception you've got? As Hans said, could you post the exception's stack trace. – Jay Zuo Aug 19 '16 at 09:44
  • @JayZuo-MSFT Did you tried in Emulator? – Kinjan Bhavsar Aug 19 '16 at 09:46

1 Answers1

2

Yeah, I can reproduce your issue while using Mobile Emulator and setting "Debugger type" to "Managed Only". Following is the exception throws at ChakraHost dateHost = new ChakraHost();.

An exception of type 'System.Exception' occurred in ChakraBridge.winmd but was not handled in user code

Additional information: failed to start debugging.

In ChakraBridge, it calls JsStartDebugging to start debugging in the current context if it's in debug mode.

#if DEBUG
    // Debug
    if (Native.JsStartDebugging() != JavaScriptErrorCode.NoError)
        throw new Exception("failed to start debugging.");
#endif

However, Native.JsStartDebugging method returns Fatal while using Mobile Emulator and setting "Debugger type" to "Managed Only". So we got above exception in our application.

This issue seem only happens in Mobile Emulator. While testing on Local Machine or Device, both can work. According to the code, this error should only occur in Debug Mode. And if we add the ChakraBridge project in our solution, the app can run in Release Mode in Emulator. For now, you can test your app on Local Machine or a real Mobile Device and keep track of this issue on GitHub.

Community
  • 1
  • 1
Jay Zuo
  • 15,653
  • 2
  • 25
  • 49
  • You are asking to add entire project or ChakraBridge.winmd file is fine? – Kinjan Bhavsar Aug 19 '16 at 12:57
  • @KinjanBhavsar Adding entire ChakraBridge project in your solution, then your app should be able to run in Release Mode on Emulator. – Jay Zuo Aug 20 '16 at 12:10
  • If I just use ChakraBridge.winmd then will it work in device in Release mode? – Kinjan Bhavsar Aug 20 '16 at 17:22
  • @KinjanBhavsar Yeah, as in my test, it works on device both in Debug or Release mode. – Jay Zuo Aug 21 '16 at 12:12
  • it is not working in Device with Debug-> ARM mode. Was it working in your device? – Kinjan Bhavsar Aug 23 '16 at 09:28
  • @KinjanBhavsar Strange, it's working in my Lumia 640. If you still has problem, I think you can refer to this [JSRT Universal Windows Apps Hosting Samples](https://github.com/Microsoft/Chakra-Samples/tree/master/Chakra%20Samples/JSRT%20Universal%20Windows%20Apps%20Hosting%20Samples/C%23) to implement your own bridge. – Jay Zuo Aug 23 '16 at 09:45
  • I have Lumia 640 XL and in that it is not working. Can you check in your code whether you have added entire project or just `ChakraBridge.winmd` file as reference? – Kinjan Bhavsar Aug 23 '16 at 09:57
  • @KinjanBhavsar Both of them work well if I debugging on Lumia 640. – Jay Zuo Aug 23 '16 at 12:14
  • not sure why not working for me. Is their any other setting that I need to change? Can you add screenshots of debug settings that you are using in your answer? – Kinjan Bhavsar Aug 23 '16 at 12:17
  • @KinjanBhavsar This is the [screenshots of my debug setting](http://i.stack.imgur.com/1Xzc7.png). – Jay Zuo Aug 23 '16 at 12:39
  • Ok I will check this with my project and can you also confirm whether you added entire ChakraHost Project or Chakrabridge.winmd – Kinjan Bhavsar Aug 23 '16 at 13:09
  • @KinjanBhavsar Yeah, I've confirmed either adding entire ChakraHost Project or referencing Chakrabridge.winmd file works in my side. – Jay Zuo Aug 24 '16 at 13:33
  • @KinjanBhavsar I think adding entire ChakraHost Project would be better as we can comment out `if (Native.JsStartDebugging() != JavaScriptErrorCode.NoError)` to avoid this issue. Actually, calling `Native.JsStartDebugging()` with "Managed Only" makes no sense. The script is debuggable only after **JsStartDebugging** is called. When debugging using Visual Studio, the “Script” debugger option must be enabled. So we should only call `Native.JsStartDebugging()` when we want to debug the JS code and in this scenario, the "Debugger type" should be set to "Script". – Jay Zuo Aug 30 '16 at 07:23
  • Thanks but ChakraHost project is the same link that I have added in my question or it is different.Can you send me? – Kinjan Bhavsar Aug 30 '16 at 07:26
  • @KinjanBhavsar Yeah, it's the same link in your question, you can add the ChakraHost project by adding **Existing Project** and select the "ChakraBridge.csproj" file under "JsBridge/src/ChakraBridge/" folder. – Jay Zuo Aug 30 '16 at 07:31
  • After adding project, I am getting error due to `using Microsoft.Graphics.Canvas;` – Kinjan Bhavsar Aug 30 '16 at 08:48
  • @KinjanBhavsar That's weird. I don't have this issue in my side. I've shared [my sample](https://github.com/Z-Jay/ChakraBridgeApp) on GitHub. You can have a test. – Jay Zuo Aug 30 '16 at 09:10