This is a condensed version. You'll have to sign the assembly or you'll get
an error at the domain creation step, other than that this should run just
by starting a new console application, adding the IPY and DLR DLLs, and then
copying and pasting this file over the default generated code.

Even after starting a new project to make sure my sample builds I still
observe the same behavior.

---
LC

On Tue, Sep 2, 2008 at 6:36 PM, Curt Hagenlocher <[EMAIL PROTECTED]>wrote:

> Do you have a simple reproduction that doesn't include any of your
> domain-specific code?
>
> On Tue, Sep 2, 2008 at 4:30 PM, Leo Carbajal <[EMAIL PROTECTED]> wrote:
>
>> So here's a strange wrinkle,
>>
>> when I run this with the debugger (unmodified except for adding the
>> IronPython assemblies as full-trust on the domain) it works fine and as
>> expected. If I run it without the debugger attached it gives me the same
>> exception as before, when I catch the exception myself I also get this
>> tidbit:
>>
>> The assembly or AppDomain that failed was:
>> Microsoft.Scripting, Version=1.0.0.4000, Culture=neutral,
>> PublicKeyToken=31bf3856ad364e35
>> The Zone of the assembly that failed was:
>> MyComputer
>> The Url of the assembly that failed was:
>>
>> file:///B:/Code/IronPythonShell/IronPythonShell/bin/Debug/Microsoft.Scripting.DLL
>>
>> If I build and compile the code as Release instead of Debug I get:
>>
>> System.Runtime.Serialization.SerializationException: Type
>> 'System.Scripting.SourceUnit' in assembly 'Microsoft.Scripting.Core,
>> Version=1.0.0.4000, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not
>> marked as serializable.
>>    at Microsoft.Scripting.Hosting.ScriptRuntime.ExecuteFile(String path)
>>    at IronPythonShell.Program.Main(String[] args) in
>> B:\Code\IronPythonShell\IronPythonShell\Program.cs:line 54
>>
>> It's a little beyond odd to me, but like I said before I fear I don't
>> fully understand what's going on behind the goo. The only consolation is
>> that I can at least built out my scripting system in working form and later
>> run it under a more trusted domain for production by simply removing the
>> domain from the Runtime.Create() constructor and then adding it later. (At
>> least, I hope this is the case)
>>
>> ---
>> Leo C.
>>
>> On Tue, Sep 2, 2008 at 4:50 PM, Shri Borde <[EMAIL PROTECTED]>wrote:
>>
>>>  The CLR doesn't dump out full exception information on
>>> SecurityExceptions in partial trust – you can get a lot more information if
>>> you look at the exception in a debugger, or if you Assert for FullTrust
>>> before doing a ToString on the permission.  Once you do that, you should be
>>> able to get more data including the demanded permission and the assembly
>>> which caused the demand to fail, instead of the message saying "The
>>> granted set of the failing assembly was:" which does not say which
>>> assembly is causing the problem.
>>>
>>>
>>>
>>> Also, can you try adding IronPython.dll and IronPython.Modules.dll to the
>>> fullTrustAssemblies argument to 
>>> AppDomain.CreateDomain<http://msdn.microsoft.com/en-us/library/ms130766.aspx>
>>> ?
>>>
>>>
>>>
>>> Thanks,
>>>
>>> Shri
>>>
>>>
>>>
>>
>>
>> _______________________________________________
>> Users mailing list
>> [email protected]
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>
> _______________________________________________
> Users mailing list
> [email protected]
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Diagnostics;


namespace IronPythonShell
{
    class Program
    {
        static void Main(string[] args)
        {            
            PermissionSet pset = new PermissionSet(PermissionState.None);
            pset.AddPermission(new 
SecurityPermission(PermissionState.Unrestricted));
            pset.AddPermission(new 
ReflectionPermission(PermissionState.Unrestricted));
            pset.AddPermission(new 
FileIOPermission(FileIOPermissionAccess.PathDiscovery | 
FileIOPermissionAccess.Read, AppDomain.CurrentDomain.BaseDirectory));
            AppDomainSetup domainSetup = new AppDomainSetup();
            domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

            // create the sandboxed domain
            AppDomain sandbox = AppDomain.CreateDomain(
                "Sandboxed Domain",
                AppDomain.CurrentDomain.Evidence,
                domainSetup,
                pset,
                CreateStrongName(Assembly.GetExecutingAssembly()),
                CreateStrongName(Assembly.GetAssembly(typeof(ScriptRuntime))),
                
CreateStrongName(Assembly.GetAssembly(typeof(IronPython.Modules.PythonGC))),
                CreateStrongName(Assembly.GetAssembly(typeof(PythonEngine))),
                
CreateStrongName(Assembly.GetAssembly(typeof(System.Scripting.ScriptCode))));


            StringBuilder script = new StringBuilder();            
            script.AppendLine("import Artifact");
            script.AppendLine("i = 200");
            script.AppendLine("while i > 0:     i = i - 1");
            script.AppendLine("print \"Done in the second verse. Yes?\"");
            script.AppendLine("print Artifact.Name");
            script.AppendLine("Artifact.SetName(\"Tool\")");
            script.AppendLine("print Artifact.Name");

            try
            {
                ScriptRuntime runtime = ScriptRuntime.Create(sandbox);          
      
                runtime.Globals.SetVariable("Artifact", new 
ScriptableItem("Dude"));
                ScriptEngine engine = runtime.GetEngineByFileExtension("py");
                ScriptScope scope = engine.CreateScope();
                
                ScriptSource source = 
engine.CreateScriptSourceFromString(script.ToString(), 
System.Scripting.SourceCodeKind.Statements);
                source.Execute(scope);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadKey();
        }

        /// <summary>
        /// Create a StrongName that matches a specific assembly
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="assembly"/> is null
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// if <paramref name="assembly"/> does not represent a strongly named 
assembly
        /// </exception>
        /// <param name="assembly">Assembly to create a StrongName for</param>
        /// <returns>A StrongName that matches the given assembly</returns>
        public static StrongName CreateStrongName(Assembly assembly)
        {
            if (assembly == null)
                throw new ArgumentNullException("assembly");

            AssemblyName assemblyName = assembly.GetName();
            Debug.Assert(assemblyName != null, "Could not get assembly name");

            // get the public key blob
            byte[] publicKey = assemblyName.GetPublicKey();
            if (publicKey == null || publicKey.Length == 0)
                throw new InvalidOperationException("Assembly is not strongly 
named");

            StrongNamePublicKeyBlob keyBlob = new 
StrongNamePublicKeyBlob(publicKey);

            // and create the StrongName
            return new StrongName(keyBlob, assemblyName.Name, 
assemblyName.Version);
        }
    }

    [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = 
true)]
    public class ScriptableItem : MarshalByRefObject
    {

        public string Name { get; set; }

        public void SetName(string name)
        {
            Name = name;
        }

        public ScriptableItem(string name)
        {
            Name = name;
        }

    }
}
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to