locked
serialise and deserialse executable code RRS feed

  • Question

  • User-1982615125 posted

    is it possible to make C#/VB code in some kind of container. such as an Action type.

    and then to serialise that into a string, send it over tcp, and run it in another process?

    i have already developed all these steps, except for how to store the code, and how to swap it between executable/string.

    so how to store the code, and how to swap it between executable/string.

    thank youw

    Monday, June 11, 2018 3:09 PM

All replies

  • User475983607 posted

    is it possible to make C#/VB code in some kind of container. such as an Action type.

    and then to serialise that into a string, send it over tcp, and run it in another process?

    Yes, it's called a remote procedure call.  However, I'm not sure what an "executable string" is.  It sounds like something you might have made up.

    https://en.wikipedia.org/wiki/Remote_procedure_call

    Since this is a website support forum the common approach is implementing an HTTP endpoint that invokes binaries on the target machine.  Web API is the latest .NET framework for this sort of thing but there is also WCF which can handle transport protocols other than HTTP like TCP or named pipes.

    Perhaps if you explain the problem you are trying to solve or better yet provide code that shows what you are trying to do.  

    Monday, June 11, 2018 3:20 PM
  • User-1982615125 posted

     i dont want to CALL a procedure, i litteraly want to send code that i can make up, and send as a string. and then when the target program receives that string, to execute that actual code.

    Here is a VERY mythical example:

    On The website:

    dim CodeStringas string = "messagebox.show("hello")"      <-- somehow serialise code into a string
    mypipemechanic.send(CodeString)                                      <-- send that string to my windows service

    On my windows service

    Sub NewMessage(message as string) handles newmessage       <--- the message comes in
              task.run(message.totask)                                                    <-- the string containing the code, is deserialised, and is executed
    end sub

    And the service runs the code on the machine, and shows this messagebox:

    ---------------------------------------
    |                    My service                  |
    ---------------------------------------
    |                                                       |
    |                        Hello                       |
    |                                                       |
    ---------------------------------------

    I do not need any help figuring out the communication. I only need ideas how to pack-up/serialise/deserialise code into and from strings, and to run it. once its a string, i can use any transport system i want anyhow.

    Monday, June 11, 2018 8:53 PM
  • User475983607 posted

    This kind of thing is already possible with JavaScript and node.js. 

    Monday, June 11, 2018 11:46 PM
  • User-1982615125 posted

    i can surely imagine. however, im not making a front end mechanic. im creating a whoel cloudcomputed system.

    i need in in code behind, in actual C#. i want the ASP.NET code behind (server side) to send executable code to another process. because asp.net does not have continues state. thats why i need another process that continuesly runs.

    so i need pure C#/VB code to be run / converted to and from strings

    Tuesday, June 12, 2018 4:11 PM
  • User475983607 posted

    i can surely imagine. however, im not making a front end mechanic. im creating a whoel cloudcomputed system.

    JavaScript runs on the server too and has for many years now.  I've used JavaScript on mainframe systems to execute CICS functions.

    i need in in code behind, in actual C#. i want the ASP.NET code behind (server side) to send executable code to another process. because asp.net does not have continues state. thats why i need another process that continuesly runs.

    I don't understand your comment.

    so i need pure C#/VB code to be run / converted to and from strings

    Seems like you are making up a problem that does not exist. 

    Can you explain the state problem you are trying to solve?

    Tuesday, June 12, 2018 4:55 PM
  • User753101303 posted

    Hi,

    You could use https://docs.microsoft.com/en-us/dotnet/api/microsoft.csharp.csharpcodeprovider?view=netframework-4.7.2 to compile source code. You don't know before your ASP.NET app runs which code should run ???

    Tuesday, June 12, 2018 5:35 PM
  • User-1982615125 posted

    this does not suit my needs, because it compiles it too another process file.

    the problem is, that my asp.net website contains all code. but this code has to be executed by another cloudcomputing process in .exe form.

    also, compiling would be too slow.

    i thought, maybe there is come kind of Action/task object, that allows to be serialised.

    Tuesday, June 12, 2018 5:50 PM
  • User753101303 posted

    Even if you have to compile this code one time only ??? If you absolutely have to run C# source code, I don't see how you could skip an implicit or explicit compilation.

    Maybe if you explain what you are really trying to do (not how you think it should be done). You want to be able to run user defined code ?

    Tuesday, June 12, 2018 7:53 PM
  • User-1982615125 posted

    can't a Task hold code? https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

    its not entirely about user defined code. The code is in codebehind in another process/machine than the process/machine that will execute it.

    1. One of my many ASP.NET websites will creates a task (defined in codebehind)
    2. That task gets send to another process (on another machine)
    3. the other machine executes the task (this machine should not be responable for have pre-written code for all my projects, which is why the other process has to store the code)
    4. The output of the task gets send back to the ASP.NET site.
    5. The ASP.NET site shows the result to the user.

    this all cannot take longer than 300 ms.

    Tuesday, June 12, 2018 9:19 PM
  • User475983607 posted

    can't a Task hold code? https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx

    its not entirely about user defined code. The code is in codebehind in another process/machine than the process/machine that will execute it.

    1. One of my many ASP.NET websites will creates a task (defined in codebehind)
    2. That task gets send to another process (on another machine)
    3. the other machine executes the task (this machine should not be responable for have pre-written code for all my projects, which is why the other process has to store the code)
    4. The output of the task gets send back to the ASP.NET site.
    5. The ASP.NET site shows the result to the user.

    this all cannot take longer than 300 ms.

    This problem has been solved.  It's called a service.

    Tuesday, June 12, 2018 9:53 PM
  • User-1982615125 posted

    OMG.. duhhhhhhh..   thats like saying, 'this problem has been solved, its called a computer'.

    but what im saying is.. i want to send code over the internet to that service.

    how to serialise and deserialise C# code. so i can send it over pipes. and how to execute that code.

    Tuesday, June 12, 2018 11:02 PM
  • User753101303 posted

    So it is actually already compiled ? The compiled ASP.NET side code won't actually run ? 

    For now the usual way to do that would be :
    - to just have the source code in the VS project tier where it will run (ie inside your web or Windows service or whatever is processing this offloaded work)
    - to compile common code to a DLL that can then be used on both sides

    For now it doesn't seems you really  have to compile source code ? Seems still some kind of overdesign ?

    You are still explaining HOW you plan to do something. When I meant WHAT I meant the final goal not the technical description of your solution to this goal. ie I expected something such as "I want to offload heavy video file conversion to another machine" or whatever is the actual need.

    When you need to process work remotely you usually transmit data from local to remote (could be tagged or queued in a db or transmitted with the call triggering the work) and then the other side either unqueue the work or start to work right away on directly transmitted data).

    I don't remember to ever have something where the code was transmitted as well (sometimes the process can be customized in which case you likely have a one time compilation step ie the code is not changing wich every run so comiling it one time is perfectly fine)

    So first do you HAVE to compile code at runtime. If yes, do you have to do this ONE TIME only. If yes it could work but until now I'm really not convinced that you are not trying once again to do something that is more complex than really needed (for now it seems the code could be just compiled "as usual").

    Wednesday, June 13, 2018 7:50 AM
  • User-1982615125 posted

    i just wanted to know if it was possible, and if it was an easy thing to do.

    i would use it for several projects:

    1) remote control scripts

    2) i was thinking of rebuilding Entityframework, in a way where the database would actually do the processing instead of the website, to avoid data transfers. Local processing in the database runs under 4 ms. while remote processing in the website itself takes over 2 seconds of datatransfer. and that is on the SAME machine.

    3) cheap aftermarket hosting since the webservers barely need power.

    4) load balancing

    5) databases are based on technology from 1970 and have barely developed. everything about it seems odd to me. and entityframework is just a hack to solve it. and unfortunately its so standardised that nobody could imagine agreeing with me. however, this is just research. and when im ever able to think of a secure design. i will make it.

    Wednesday, June 13, 2018 11:22 AM
  • User753101303 posted

    1) Persisting source code is called "compiling". Note though that you don't have to compile the same source code multiple times. One time is enough. You also have "containers" maybe or serverless computing depending on the exact goal (you want to spread existing code on other servers ?)

    2) Which kind of "processing" are you talking about ? EF generates a SQL statement on the web server side which still runs on the sdb server side. This is not because EF exists that you have to move all the data on the web server side. You can still use a server side stored procedure if it makes sense for the kind of "processing" you are doing. Retrieval time seems huge (this is about a single row, once again you have to pay the price if you retrieve hundreds or thousands of rows which is usually  not needed ???)

    3) ???

    4) Still seems something with proven solution. Not sure what is the point?

    5) You do have all kind of databases including https://en.wikipedia.org/wiki/Object_database if this is what you mean (in addition to those mentionned earlier). It changed a lot since 1970 (but once again the point would be to see first what you really miss from existing stuff).

    Now it won't go anywhere as you keep discussing your own technical solutions to reach a goal we have no real idea about. For now it doesn't seems to be something such unique that existing and proven solutions couldn't be used so we have not much to say that if your goal requires compilling source code then you have to compile source code.

    If  you need further help and generally speaking, I would strongly suggest to start by describing first the exact problem you are trying to solve rather than starting right away to describe an unusual and complex solutions to some unclear problema dn with unclear constraints (you can't just deploy the code on the machine that will run it ?)

    Wednesday, June 13, 2018 12:19 PM