How can I get C# to interop with Javascript? - c#

I'd like to host Javascript in my C# program. I would like to allow users to write custom Javascript code, and have my C# program run their functions, as well as allow the users to use my framework code.
Is it possible to do this? If so, how?
Edit: To be clear, I am not using ASP.NET for this project.

you can also use a webbrowser control to host the javascript in a html document, to interact between the two you would make a COM visible class and set an instance of it to the ObjectForScripting property.
Any public members of the 'external' class are now accessible through window.external in javascript. Also, from the managed code side you can use the InvokeScript method of the document to call any javascript functions.
This way it is easy to pass complex objects between the two.
I used this idea quite a bit in this Google Earth Api application.
http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html

I'd highly recommend you use the Microsoft Dynamic Langauage Runtime (DLR). It's purpose in life is to facilitate scripting in a .Net environment. The DLR is not script specific so over time more script languages will be built on top of it but at the moment the ones I know about are IronRuby, IronPython and JScript.

Spidermonkey .NET Is probably what you are looking for.
I'm not sure how developed it is. Buts its basically a port of Mozilla's Spidermonkey Javascript parser engine which I like a lot.
Or you can evaluate it directly

The way to do this used to be the Windows Script Host, which permitted you to host either JSCript or VBScript.

You can certainly run Javascript natively from C# - see this article for a starter. Having the Javascript access your framework could be tricker, though. Perhaps Script# could help?

There are HTML components where you can embed JavaScript. For the other way the proper thing is an ASP.NET web service. Perhaps with a smart client you can combine both.

Related

C# API routines integrated into ASP Classic application?

We have a classic ASP application that isn't going away. We have a new marketing system that we need to access via their API (WDSL). All of their examples are in C#, which I'm trying to learn enough about today to pull this off.
I downloaded Visual Web Developer 2010 Express and have made some progress. Then I realized that I don't know if it's possible to call a C# .net routine from a classic ASP app. I'm a generalist. I know some classic ASP vbscript, some javascript, some AJAX, stuff like that. If it hasn't been necessary at my current job, then I don't know it.
Is it possible to call c# .net files from a classic ASP application? I'd love to know just this, because I'd hate to get it worked out where I can talk to the API, and then find out I can't use what I've written. Everything I've found on StackOverflow has been .net VBScript files off .asp apps. (Heh. And still have to figure out how to deploy what I've done. lol.)
If it's not possible to run C# from .asp pages, then I need to rewrite in VBScript, and I don't know how to convert C# to VBScript. If I can't run c# off .asp, any conversion utilities out there for short routines C#->VBScript? How difficult/stupid is conversion?
(The API url is in the form of https://secure.ourspiffynewmarketingapp.com/API/1.2/Service.svc?wsdl - don't know if that matters, but figured I'd say so, since I've run across some https issues on my searches.)
I've been on StackOverflow off and on for 11 hours, and I still don't know the answers to these questions.
I don't need to get the whole thing figured out tonight, but I do need to confirm that I can access the API, and since all of the examples are in C#, which I've never even LOOKED at before today, I can't even confirm that.
I couldn't find exactly how I implemented it but did find this article that is pretty much the same thing.
classic-asp-soap-request
Re : Is it possible to call c# .net files from a classic ASP application?
In order to access 'dll' (assembly code) you need to map your top level (exposed components) as COM callable wrappers, and register these as COM components. You can then use them in your ASP Classic just as any other COM component Server.CreateObject('MyProgIdGoesHere')
Have a look here if you need to call SOAP web services from ASP Classic, using the Microsoft.XMLHTTP component (from memory there might have been a server version of this as well). You will need to build up the SOAP request and parse the response yourself, however :(
Edit If you are feeling brave, you might also look at starting the conversion process from ASP Classic across to .NET.

How can we integrate our C# code in Drupal CMS

We have C# code, which is backend for us. Now we need it to be used in our website which is developed in Drupal.
As far as I know, there is no easy way to do it. You basically have 2 options:
Rewrite the code in php. It may be not possible to do it however, it depends on what the code does.
Expose c# code as an external webservice (e.g. with WCF) and then consume the webservice. Drupal is able to do it - more info.
A sloppy solution would also be to display your C# page in the Drupal page with an iframe. I do also agree with empi's answer to rewrite the code in php or a web service depending on your requirement.
You can build DLL project (class library) in C# and use the DLL as reference back in the drupal project.
Not sure about this, and never done before.
You better research more.
But, the best way to use the .NET code in another application is to build the required code into DLL project and use in whichever language you want to use.

Accessing WebScriptObject with MonoTouch

I am building a phonegap like solution using MonoTouch & UIWebView. I would like to expose a number of C# methods via Javascript. I know you can do this in Objective C using WebScriptObject but I can't see where/if this is exposed in the MonoTouch API's.
Is there a way to access WebScriptObject?
Is there a way to access WebScriptObject?
No, WebScriptObject is not part of MonoTouch (like it is part in MonoMac) since WebKit, as a framework, is not part of iOS.
UIWebView does allow you to evaluate JavaScript code but I'm not sure (never tried or even looked for) how you can, from there, get back into C# methods.
Just as as corollary to Sebastian's answer you can always create a simple binding that allows your UIWebView to interact with C# code via a HttpListener:
http://yobriefca.se/blog/2011/12/05/rolling-your-own-phonegap-with-monotouch/
You can also use iOS Background threads to interact with other apps via Javascript or C# using HttpListener:
http://blog.devnos.com/remember-when-apps-didnt-talk-to-each-other
As some additional side answers to this...
you can access javascriptcore - but you will have to work out the bindings for this
some people have managed to use Jint (0.9 or older - with no Reflection.Emit JIT code)

Is it possible to embeded js into c#

Currently I'm involved in a project using c# . I know some of the features of this project can be easily implemented using Javascript (I have been a js developer for some time, but totally new to c#). so I'm very eager to know if it is possible to reuse the available js code in the project, or put it another way, embeded js into c# .
thanks in advance.
I think you're asking if it is possible to run JavaScript on the CLR. Similar to Java 6's JVM script engine.
EcmaScript.net could be your best bet - http://code.google.com/p/ecmascript-net/ - see also this question - Are there any .NET CLR/DLR implementations of ECMAScript?
There is an extensive article written on how to embed Javascript within C# here. Is that what you are looking for?
Are you referring to ClientScriptManager RegisterClientScriptBlock Method
There is no reason you cant continue to use existing client side javascript code.
There is a still a clear distinction to whats required on the client and what can be called by the client.
Various possibilities include: calling web services, making ajax calls to WebMethods and injecting javascript code into pages.
The way you write it it sounds like a giant cop-out of learning C# properly. Unless you actually mean that you want to use JavaScript for client-side control of your project, this is a no-go (and it doesn’t sounds like you meant client-side scripting).
That said, .NET actually allows you to mix assemblies from different languages effortlessly, but this only applies to .NET languages which can be compiled to .NET IL (bytecode) and I’m not aware of a JS-to-IL-compiler.
Javascript runs in the browser, as long as it is a web project there is no problblem combining. C# code will run on the server, javascript code in the browser.
http://msdn.microsoft.com/en-us/library/aa479011.aspx

Clarion: Possible to use a C# Assembly?

Is it possible to use a C# assembly from Clarion? If not. Is it possible to run a simple console application written in C# and read the output from it back into Clarion? In both cases, how would you do it? In the case of the assembly, would you have to do something special with it for Clarion to get access to it? In both cases, how would you do it in Clarion?
The reason is we have two methods written in C#, which is used by an application written in C#. But we also need to use that same method in a Clarion application. And we really don't want to write the methods again for Clarion, since they are a bit complex...
Unless you need to display a .Net UserControl in your clarion program I would recommend using the technique described here:
Robert Giesecke - C# Project Template for Unmanaged Exports
On the Clarion side you then just link and use the .Net assembly as you would any other unmanaged DLL. I have found it convenient to load the DLL at runtime with the LoadLibrary API too.
A Clarion program will not easily or nicely call a C# DLL, though the reverse is not necessarily true.
In a project of mine, I decided to go with a minimum of coupling between a Clarion program and a C# assembly by using TCP/IP.
The idea is to have the Clarion program connect to the listening port of your mini-server, send a request and receive a reply, in the same way that a function is called with parameters and a result received.
It is easy to add a class in your C# assembly to act as a TCP/IP server. On the Clarion side, there are a handful of good templates that offer TCP/IP functionality. We have been using CapeSoft's NetTalk template for years and it is virtually rock-solid.
Depending on the urgency of your requirement, you might consider this route.
-Ray.
What is Clarion? Is it an unmanaged application? If it is, then the "something special" you would do to your assembly is called Com Interop: http://msdn.microsoft.com/en-us/magazine/cc163494.aspx
Clarion is a programming language. Don't know it myself. Parts of the old system where I work is made with it, and we are working on porting it to C# .NET. So for a while they have to live side by side. We found a work around so it is no longer needed though. But thanks for info :) Maybe we will need it further down the road instead. Although I hope not... hehe.
For those curious: en.wikipedia.org/wiki/Clarion_(programming_language)

Categories