Sandbox Sample in .NET 4.0

282 views
Skip to first unread message

Nate

unread,
Oct 7, 2012, 1:55:21 PM10/7/12
to cs-s...@googlegroups.com
The Sanbox sample on this page: http://www.csscript.net/Samples.html gives some warning when compiled for  ,NET 4.0.

PolicyLevel.CreateAppDomainLevel and AppDomain.CurrentDomain.SetAppDomainPolicy  are obsolete.  This link http://go.microsoft.com/fwlink/?LinkID=155570 tells a way to work around this by editing you App.config file to allow use of the obsolete methods.  That works, but I feel it better to switch to doing Sandboxing the way .NET 4.0 thinks you should do it.

The page also links to http://msdn.microsoft.com/en-us/library/bb763046(v=vs.100).aspx which shows an example of doing sandboxing in .NET 4.0.  I tried but was unable to get this example to work with loading an assembly generated by CSSScript.

Starting from the code shown at the bottom of the sanfboxing in .NET 4.0 example I adding these lines to the start of the main method:
string assemblyName = CSScript.Compile("Danger.cs");
File.Copy(assemblyName, Path.GetFullPath("Danger.dll"), true);

then changing the final line of the main method to be:
newDomainInstance.ExecuteUntrustedCode("Danger", "ClassA", "SayHello", null)

trying to run it the first line of the ExecuteUntrustedCode method throws:
Could not load file or assembly 'Compiled' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. 

And I'm not sure what to do from there.  Can someone with more experience with .NET Assembly loading tell me whats going on here, and show me how I can load an assembly generated with CSScript into a sandboxed environment.

Thanks.

Oleg Shilo

unread,
Oct 9, 2012, 12:20:22 AM10/9/12
to cs-s...@googlegroups.com
Hi Nate,

I had a look at the changes and they are indeed significant. MS changed the game so dramatically that the old syntactical paradigm is not working any more. And it is not the only CS-Script extensions and samples who are affected. It is .NET types as well. 

In the MSDN sample you provided the choice of the instantiation routine is quite important. They are using Activator.CreateInstanceFrom. One would think that AppDomain.CreateInstanceFrom would be a more appropriate choice as the instance is to be created in a specific AppDomain. But it  AppDomain.CreateInstanceFrom is broken by the new Sandboxing model as it cannot operate with the minimal PermissionSet (While Activator can).

Thus CS-Script cannot be used the same way as it was. Reflection is limited, IL emit is in question, lambda serialization is not supported (out of box).

But nevertheless CS-Script still can be used with the new sendboxing model though in a less convenient way. 

I will try to come up with the appropriate syntactic improvements if possible. And for now please follow the technique demonstrated in this sample: "C:\Users\osh\Dropbox\Public\Support\Nate\Host.7z" (or see the attachment). 
  
Cheers,  
Oleg Shilo
Host.7z

Mark

unread,
Feb 26, 2013, 2:08:34 PM2/26/13
to cs-s...@googlegroups.com, osh...@gmail.com
Oleg,

Any news on this? I just recently changed an application I have that was using cs-script for .NET 3.5 perfectly to .NET 4.0. I promptly get an exception that says, "An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous."

I have looked at the example you show but I cannot see how to adapt it to my application. I have a hosting application that creates a c# application in source code dynamically, compiles it into a temporary assembly file, loads it into a new AppDomain using AsmHelper and the creates a specific object within the new AppDomain. I then periodically make calls into the created object from my host.

Any help would be greatly appreciated.

Thanks,
Mark

Mark

unread,
Feb 27, 2013, 8:34:05 AM2/27/13
to cs-s...@googlegroups.com, osh...@gmail.com, mark....@gmail.com
Well, I figured out the problem with my application and it was not moving to .NET 4.0. I downloaded the latest binaries for cs-script on Windows 7 but I failed to "Unblock" the .zip file as Oleg brought to my attention, Thus all of the extracted contents were blocked as well. When I unblocked everything properly it worked just as before. I think I am not using a true sandboxing model.

Thanks Oleg for the help,
Mark
Reply all
Reply to author
Forward
0 new messages