JavaScript and C#


I was wondering if there would be any demand for a product that allowed you to call managed code from JavaScript.  It seems like it should be doable, using a technology like Mirror, and would allow you to rapidly create apps with a rich HTML UI and deploy them over the web.  However, you could also argue that .Net CF and WinForms also allow rapid application development, and provide much the same benefit, and with better debugging support.  (CF apps don't deploy over the web, but then again, it's not that hard to click on a CAB on a web page.)


So what do you think?  Would there be any use for such a technology (beyond "Hey, that's a cool proof-of-concept")?


- Jason Fuller


Comments (13)

  1. Adam says:

    I think it would be awesome to be able to put exploit code on the web, and have it access C# classes on people’s phones via Javascript.

    Well, maybe only awesome in the original sense of the term, tied closely to awful.

    If you do this, please have a way to control the list of accessible classes foreach bit of Javascript.

  2. Neil Thompson says:

    Yes definately. I can think of a number of applications that we are developing that could make really good use of such an implementation.

  3. windowsmobile says:

    Adam, obviously, security would have to be considered.

    Chris, Ajax.Net is for talking to a server, not managed code running locally on the device. Also, Ajax.net apparently does not support Windows Mobile.

  4. bennie says:

    I was involved in work at my company a while back that ported JScript to the Compact Framework, to allow Users to write small pieces of logic in a language they already understood. But it wasn’t really focused on the UI side of things.

  5. Adam says:

    I’m glad to hear that security must be considered. I think the first consideration should be the attack surface. This change would open an entirely new set of vectors to the device. Any security features you add to it would be narrowing that vector. But the permissioning model on my 2003 device seems pretty simple (which is good, except everything needs permissions).

    I’d really like to know that there’s a large audience of developers that would make the increase in risk worthwhile.

  6. micmickimo says:

    question: you mentioned that we can use Mirror to let Jscript call managed code at teh device,

    can you be more specific on that? If I have a winodes mobile 5 device and I have a managed code that use camera function , can I call this code using JScript at IE mobile (w/ support of "Mirror")? how?

    Thanks

  7. micmickimo says:

    question: you mentioned that we can use Mirror to let Jscript call managed code at the device,

    can you be more specific on that? If I have a winodes mobile 5 device and I have a managed code that use camera function , can I call this code using JScript at IE mobile (w/ support of "Mirror")? how?

    Thanks

  8. Pan-Wei Ng says:

    So,

    So, what is the answer? How do you call C# from javascript? I am not talking about connecting to the server, but C# code that have already been installed on the same machine as the browser that runs Javascript.

  9. Manjul says:

    what is the answer? How do you call C# from javascript? I am not talking about connecting to the server, but C# code that have already been installed on the same machine as the browser that runs Javascript.

  10. Dave says:

    The Anser is "You don’t," unless you happen to be using ActiveX in a microsoft browser or other host… you can’t.  If you are using ActiveX, you can compile your C# to a COM Visible dll, which you must also register on the target host using something with Admin rights, or the MSI installation framework (or a direct registry hack.) Then, and only in this microsoft activeX capable environment, can you create an instance of your registered class and use it in web-based script.   The restrictions put on normal (non-microsoft) Javascript are there for a reason, exactly so that you CANT go calling arbitrary executable code from wherever you like.  It’s a security measure, and though it might be inconvenient for you, the internet is still working now (on microsoft devices too) because you can’t do that. Trust me on this one.  And you’re asking about ‘calling c#’ when I think you mean calling some program that you wrote in C#… I mean you can’t just ‘call c#’ you have to compile it first.

  11. Sander Postma says:

    Its a lot of work, but it might be possible to implement a web service or other http based interface on the mobile device. At least TCPListener class is availible, so the quick’n dirty way would be to listen on localhost and parse the http form/url and execute what you want on the local device.

  12. I was thinking of the possiblity too. It would be nice if it could be done.

Skip to main content