Together with Ben (@BuildStarted), we've been building the RazorEngine project, which is designed to allow you compile and parse arbitrary templates outside of ASP.NET MVC. The project has had a couple of releases for a while now, and the feedback has been great (thanks to all!). But, we have encountered an issue: Medium trust.
Because we are using the CSharpCodeProvider to compile the Razor-generated classes (to load into the current AppDomain for execution), we encounter a SecurityException
when trying to invoke it, due to the LinkDemand enforced on it. When a normal ASP.NET page is compiled (regardless of trust level), it does so through a BuildProvider
which is normally GAC'd, and thus is implicity granted the appropriate permissions to compile. Hence ASP.NET WebForms works as standard in Medium trust.
Our code does not, and we need to target scenarios where our built library won't be deployed to the GAC and is running in Medium trust. So we considered building a BuildProvider
specifically for it and defer compilation to the ASP.NET build system, but this seems overly complex and disjointed, and the provider itself requires both a mapping through a file extension, and a virtual path (which may or may not resolve to a physical file - think VirtualPathProvider
). It all seems overkill, just to get the project working in Medium trust; not just that, but purely for ASP.NET projects as our engine also works outside of ASP.NET
So my question is this, does anyone know of any techniques or technologies for compiling C# code in a Medium trust environment?
Thanks in advance.
Perhaps you could consider using MCS (Mono Compiler Service), which has the possibility to emit to an in memory assembly instead of a file (yes, I know CSharpCodeProvider has the ability for in memory only, but it still writes temp files and performs file system operations).
Caveat: You'll probably run into security issues with MCS too, but it's open source (obviously) and I've been able to modify it so as to do expression compilation in Silverlight... Just saying, it's probably not going to be a piece of cake.
Update: Per the Mono roadmap page, my modifications aren't really necessary anymore since they say they support Silverlight out of the box in version 2.8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With