Recently I was debugging the issue with DotNetBrowser and external call from JavaScript. I had the following:
1 2 3 4 5 6 7 8 9 10 |
FleepBrowser.Browser.DocumentLoadedInMainFrameEvent += delegate(object sender, DotNetBrowser.Events.LoadEventArgs e) { FleepBrowser.Browser.ExecuteJavaScriptAndReturnValue("window").AsObject().SetProperty("external", new ExternalCodeHandler { NewMessagesCountCallback = delegate(int count, int allCount) { // ... } }); } |
So I configure the browser and then set external
object to handle callback. The callback is a method accepting two integers. It was called using this JS code:
1 |
window.external.NewMessagesCount(conversations.not(".muted").length, conversations.length); |
It worked correctly on multiple machines for over a year. One day I ran the application on new machine and it stopped working. I added handler:
1 2 3 4 5 |
try{ window.external.NewMessagesCount(conversations.not(".muted").length, conversations.length); }catch(e){ alert(e); } |
It showed dialog with mscorlib: Input string was not in a correct format
. C# handler wasn’t called at all.
I attached dnSpy and it showed exception thrown in mscorlib Number.cs.
Okay, so what is wrong here? Notice that C# handler expects integers but JS doesn’t have them. So it passes numbers as doubles. It worked on one machine and didn’t work on the other so it immediately suggested there was something wrong with locales. Indeed, the former had English locale and the double was passed as 1.00000
. The latter was running with Polish locale so it expected the coma and hence failed.
I don’t know exactly why it didn’t work but it wasn’t hard to fix it. First, change the C# handler to accept strings. Second, change JS code to pass the argument as window.external.NewMessagesCount("" + conversations.not(".muted").length, "" + conversations.length);