I am using Asp.Net 5 CTP6. I need to execute some c# code at runtime. I know there is some solution and I used some of them but I want to know how can I do this by Roslyn because of platform independency? By the old solution that I found may be exists some problems about multi platform independency. In fact I want to run the web app on windows and Ubuntu. Thanks
Asked
Active
Viewed 486 times
1
-
can you give us a sample of what you want to do please such as the old solution you found – agua from mars Mar 12 '15 at 11:10
-
Consider you have a string of code : – Mohsen Mirshahreza Mar 13 '15 at 03:41
-
There is some solutions for this : http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments , http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime – Mohsen Mirshahreza Mar 13 '15 at 03:48
-
Did you read this post https://mhusseini.wordpress.com/2014/05/23/roslyn-compile-c-expressions-without-using-the-scripting-api/ ? – agua from mars Mar 13 '15 at 08:42
-
I read the post. it is not applicable. I need a method for run the plain c# code at run time. I will answer bellow for a better vision: – Mohsen Mirshahreza Mar 15 '15 at 09:11
2 Answers
0
Please see the following code :
string s = @"
using .....
string a=""this is a test :)""
";
SomeMethodToRunTheCode(s); In asp.net webform I can attach the code to an .ascx (at runtime I create a file with .ascx extension and I append the code to the file ) file then with LoadControl method I will be able to run the codes at runtime. but in the asp.net 5 ( vNext ) I do not know how can I do this.

Mohsen Mirshahreza
- 155
- 1
- 1
- 10
-
Do not aswer to your question, edit your question instead. Thus, if you do that, you application pool will be recycled each time you create a new .ascx file – agua from mars Mar 17 '15 at 10:48
-
Ok. I will edit my question at the next case. And about the application pool you are right, Yes I know !!! but I need a good solution in asp.net 5. Do you have a suggestion. – Mohsen Mirshahreza Mar 17 '15 at 11:00
-
You can create a .cs file, it will be compile at runtime by roslyn but that will going to recycle your application pool too. – agua from mars Mar 17 '15 at 13:09
0
Take a look at how razor does it https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs

davidfowl
- 37,120
- 7
- 93
- 103