So here's the concept. I'm trying to write a C# backend for CouchDB (keep reading, this has little or nothing to do with CouchDB).
Here's the basic idea:
1) The DB Driver uploads an arbitrary string to the DB Server.
2) The DB Driver tells the DB server to run the code and produce a view
3) The DB Server loads my C# backend, hands it the input string
4) The DB Server hands the function to the C# backend
5) The DB Server hands the C# backend data to execute the function over
6) The C# Backend executes the code and result the data to the DB Server
7) The DB Server stores, or otherwise sorts and sends the results back to the DB Driver
So the problem I'm having is with the remote code execution. All my models are C# objects. So it'd be really nice to be able to write the code on the front end, and have the code transfered to the back end for execution. Without having to copy DLLs etc. Now currently in CouchDB this query language is JavaScript so it's super simple to just copy the code into a string.
Any options like this in C#? Can I get a string version of a function? Should I create a external server that hosts these DLL assemblies and serves them up to the CouchDB backends? Should I compile them on the front end, and embed the .cs files as a resource? I'm just brainstorming at this point. Any thoughts/ideas/questions are welcome.
Would you be able to achieve this using Expression trees (System.Linq.Expressions)?
Perhaps some means of passing a serialised expression tree to the back end, compiling this into a lambda/Func<> oin the server side and then executing it?
Have not played with them except for some curiosity research when Linq was unveiled, but it may help.
It sounds like what you're looking for is a Javascript style eval method for C#. Unfortunately that does not exist today and is not trivial to implement. The two main approaches for dynamically executing C# code is
Building lightweight methods via Reflection.Emit
Compiling assemblies on the fly (via CodeDom or straight calls to csc).
Another option to consider here is to take advantage of a dynamic language. Starting with C# 4.0, it's very easy to interop with dynamic languages. Instead of the backend being passed C# code, why not pass Python or Ruby and take advantage of the DLR?
Define "function." Is this arbitrary code (which must be dynamically compiled and executed), or a list of methods that could be attached to a class?
If the latter (which is what you should be doing - allowing arbitrary dynamic code execution not only violates the .NET development model (in my opinion - no flames please), it's a huge security risk), you can use a web service.
Web services and WCF services are an excellent way to distribute processing between machines.
There's a couple of projects I've used in the past that might help you out:
Vici Parser
Vici Parser is a .NET library for
late-bound expression parsing and
template rendering. It also includes
an easy to use tokenizer that can be
used for parsing all kinds of file
formats (a full-featured JSON parser
is included).
This Codeproject article
This example shows an "Eval" function
for C# and implements different usages
of this evaluate function. The library
is written in C# and can be tested
with the MainForm in the solution.
Other alternatives include using XSLT scripting (something Umbraco CMS makes use of) and perhaps something like LUA.NET which would enable you to sandbox what the code can do.
Related
I know this is wrong (trying to code in C# with a C/C++ mindset).
But is it possible to create inline functions / inline recursive functions (til the Nth call) / macros in C#?
Because if I intend to call the same function 1000 times, but it's a simple function, the overhead of creating the stack is quite big... And unnecessary.
In C i could use inline functions. Is there something like that in C#?
Again... I'm not saying that C/C++ is better... I'm just new to C# and have none to ask those simple questions...
Inline functions in C#?
Finally in .NET 4.5, the CLR allows one to force1 method inlining
using MethodImplOptions.AggressiveInlining value. It is also available
in the Mono's trunk (committed today).
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
void Func()
{
Console.WriteLine("Hello Inline");
}
The answer should be: don't worry about it. No need to start with micro-optimizations unless you've tested it and it's actually a problem. 1000 function calls is nothing until it's something. This is majorly overshadowed by a single network call. Write your program. Check the performance against the goals. If it's not performant enough, use a profiler and figure out where your problem spots are.
Yes, C# is a very powerful general purpose language that can do nearly anything. While, inline functions / macros are generally frowned upon, C# does provide you with multiple tools that can accomplish this in a more concise and precise fashion. For example, you may consider using template files which can be used (and reused) in nearly all forms of .NET applications (web, desktop, console, etc).
http://msdn.microsoft.com/en-us/data/gg558520.aspx
From the article:
What Can T4 Templates Do For Me?
By combining literal text, imperative code, and processing directives, you can transform data in your environment into buildable artifacts for your project. For example, inside a template you might write some C# or Visual Basic code to call a web service or open an Excel spreadsheet. You can use the information you retrieve from those data sources to generate code for business rules, data validation logic, or data transfer objects. The generated code is available when you compile your application.
If you're new to programming I would recommend against implementing templates. They are not easy to debug in an N-Tier application because they get generated and ran at run-time. However, it is an interesting read and opens up many possibilities.
Trying to understand how to use WF rule engine outside of a WWF application. I've only read a blog post on the topic. But I have certain doubts on the feasibility.
My application requirements are as follows:
Web-based UI for writing the rules, and storing them in db.
A windows service will download the rule. A rule execution engine will gather data it requires and execute the rule, and give a result as output.
The rule is simply a set of instructions which must act on a row of a table. The schema of the table is dynamic; however there is some metadata which tells the rule execution engine how to gather the necessary inputs from that row.
I know that rules are usually input using a rules editor; this is a windows form application. This usually generates a *.rules file. The WF rules engine, as per my knowledge, evaluates this file and does the execution of the rule.
The *.rules file is an xml representation of the rule.
Is there any api in the dotnet framework which generates this xml representation? And, can we build a web-based front end for inputting the rules?
Was wondering if there was an api kind of thingy and I came across the following:
http://code.msdn.microsoft.com/windowsdesktop/Creating-Rules-Using-the-23c5d561
It is an api like interface. However it consumes objects from the System.CodeDom namespace (usually various kinds of CodeExpression objects).
To go about using it you have to correctly represent the code expression objects in your front-end via some mechanism (by using xml or json). In the server, i.e. when you post the rule to the server, you have to create the necessary code expression correctly (via parsing) and feed those things to the api.
You need two types of code expression objects - one for evaluation of the rule condition, and the other for the stuff to execute when the rule passes or fails. (I only need stuff to execute when rule passes).
The sample provided there gives you an idea of how to use the api. The rest is something you'd have to build on.
A program source code is commonly represented in memory using an AST. All you'd have to design for is making your own implementation.
If you want to use Windows Workflow Foundation (WF), you must have WF XAML generated - not .rules or XML representation of that (whatever that is). Here is an example of a Workflow XAML file. There are basically three ways to generate this (starting with the least complex method)
By using the designer in Visual Studio
By generating a DynamicActivity-instance runtime and serializing it
By implementing your own generator
To answer your question: Yes - it's option #2. If your rules are very simple (if-then-else) and you don't anticipate them to increase in complexity in the future, option #3 may be a viable alternative as well.
During development of a new application, I was wondering how the most flexible solution of a dynamic, let's say ‘scriptable’, system would look like.
In general, I would need a file in plain text (e.g. TXT or XML) wherein I define a trigger (in my example a hex string) and a corresponding action (open a window, execute a SQL transaction, …). I should be able to add new entries without recompiling the whole application.
As you see, it's not really scriptable this way, but I need a solution to define what happens with which input.
Has anyone got some experience with this?
There are various scripting languages you can use inside your .NET application; IronPython being one obvious example, but others are available - javascript for example (talking to your .NET objects; not in a browser). One of these might have some application here?
If you have only simple program flow you can use Windows Workflow Foundation Rules Engine.
Features
Inlcuded in dotnet3.0 runtime, no extra costs
RuleSetDialog can be integrated into your code to edit rules including intellisense
persistable as xml-files.
ruleengine can evaluate expressions and can perform actions
See also
A quick and dirty Rules Engine using Windows Workflow Part1 and Part2
I first found this topic when reading the german languaged magazine dotnetpro 10/2010 on page 30 "Die Rule-Engine aus .NET ohne Workflow benutzen"
Well you can embed a .NET language like IronPython or possibly Boo
Does anyone know if there is a way to evaluate c# code at runtime.
eg. I would like to allow a user to enter DateTime.Now.AddDays(1), or something similar, as a string and then evaluate the string to get the result.
I woder if it is possible to access the emmediate windows functionality, since it seems that is evaluates every line entered dynamically.
I have found that VB has an undocumented EbExecuteLine() API function from the VBA*.dll and wonder if there is something equivalent for c#.
I have also found a custom tool https://github.com/DavidWynne/CSharpEval (it used to be at kamimucode.com but the author has moved it to GitHub) that seems to do it, but I would prefer something that comes as part of .NET
Thanks
Mono has the interactive command line (csharp.exe)
You can look at it's source code to see exactly how it does it's magic:
https://github.com/mono/mono/raw/master/mcs/tools/csharp/repl.cs
As you've probably already seen, there is no built-in method for evaluating C# code at runtime. This is the primary reason that the custom tool you mentioned exists.
I also have a C# eval program that allows for evaluating C# code. It provides for evaluating C# code at runtime and supports many C# statements. In fact, this code is usable within any .NET project, however, it is limited to using C# syntax. Have a look at my website, http://csharp-eval.com, for additional details.
Microsoft's C# compiler don't have Compiler-as-a-Service yet (Should come with C# 5.0).
You can either use Mono's REPL, or write your own service using CodeDOM
Its not fast but you can compile the code on the fly, see my previous question,
Once you have the assembly and you know the type name you can construct an instance of your compiled class using reflection and execute your method..
The O2 Platform's C# REPL Script Environment use the Fluent# APIs which have a real powerful reflection API that allows you do execute code snippets.
For example:
"return DateTime.Now.ToLongTimeString();".executeCodeSnippet();
will return
5:01:22 AM
note that the "...".executeCodeSnippet(); can actually execute any valid C# code snippet (so it is quite powerful).
If you want to control what your users can execute, I could use AST trees to limite the C# features that they have access to.
Also take a look at the Microsoft's Roslyn, which is VERY powerful as you can see on Multiple Roslyn based tools (all running Stand-Alone outside VisualStudio)
I would like to build an application framework that is mainly interpreted.
Say that the source code would be stored in the database that could be edited by the users and always the latest version would be executed.
Can anyone give me some ideas how does one implement sth like this !
cheers,
gabor
In .Net, you can use reflection and CodeDOM to compile code on the fly. But neither approach is really very simple or practical. Mono has some ability to interpret c# on the fly as well, but I haven't looked closely at it yet.
Another alternative is to go with an interpreted .Net language like Boo or IronPython as the language for your database code.
Either way, make sure you think long and hard about the security of your platform. Allowing users to execute arbitrary code is always an exercise fraught with peril. It's often too tempting to look for a simple eval() method, and even if one exists, that is not good enough for this kind of scenario.
Try Mono ( http://www.monoproject.org ). It supports many scripting languages including JavaScript.
If you don't want to use any scripting you can use CodeDOM or Reflection (see Reflection.Emit).
Here are really useful links on the topic :
Dynamically executing code in .Net (Here you can find a tool which can be very helpul)
Late Binding and On-the-Fly Code
Generation Using Reflection in C#
Dynamic Source Code Generation and
Compilation
Usually the Program uses a scripting language for the scriptable parts, i.e. Lua or Javascript.
To answer your technical question: You don't want to write your own language and interpreter. That's too much work for you to do. So pick some other language, say Python or Lua, and look for the documentation that lets your C program hand it blocks of code to execute. Of course, the script needs to be able to do something, so you'll need to find how to expose your program's objects to the script. Also, what will happen if a client is running the program when you update its source code in the database? Should the client restart? Are you going to store the entire program as a single row in this database, or did you want to store individual functions? That affects how you structure your updates.
To address other issues with your question: Why do you want to do this? Making "interpreted language" part of your design spec for a system is not often a good sign. Is the real requirement something like this: "I update the program often and I want users to always have the latest copy?" If so, there are other, better ways to go about this (just give us your actual scenario and requirements).