一个关于动态编译时 Evidence的问题
1 using System.Reflection;
2 using System.CodeDom;
3 using System.CodeDom.Compiler;
4 using System.Collections;
5 using System.Security;
6 using System.Security.Policy;
7 using System.Security.Permissions;
8 using Microsoft.CSharp;
9 using Microsoft.VisualBasic;
10 using System.IO;
11 using System.Windows.Forms;
12
13 using Bee.Core.Util;
14 using Bee.Core.Log;
15
16 namespace Bee.Core.Script
17 {
18 /// <summary>
19 /// DotNetScriptEngine 的摘要说明。
20 /// </summary>
21 public class DotNetScriptEngine
22 {
23 public DotNetScriptEngine()
24 {
25 //
26 // TODO: 在此处添加构造函数逻辑
27 //
28 }
29 /// <summary>
30 /// This method is used internally.
31 /// </summary>
32 public virtual Assembly Compile(ArrayList references, string language, string source,string url)
33 {
34 Assembly generatedAssembly=null;
35 // get a compiler for the appropriate language.
36 CodeDomProvider codeProvider=GetCompiler(language);
37 if(codeProvider == null)
38 {
39 return null;
40 }
41 // setup the compiler defaults. Maybe make this application definable at some point.
42 ICodeCompiler compiler=codeProvider.CreateCompiler();
43 CompilerParameters compilerParams=new CompilerParameters();
44
45 compilerParams.CompilerOptions = "/target:library /optimize";
46 compilerParams.GenerateExecutable=false;
47 compilerParams.GenerateInMemory=true;
48 compilerParams.IncludeDebugInformation=false;
49 compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
50 compilerParams.ReferencedAssemblies.Add("System.dll");
51
52 compilerParams.Evidence=GetEvidence(url);
53
54
55 try
56 {
57 // compile the source code
58 CompilerResults results=compiler.CompileAssemblyFromSource(compilerParams, source);
59 if (results.Errors.Count > 0)
60 {
61 foreach(CompilerError error in results.Errors)
62 {
63 OnLogCompileErr(error);
64 }
65 return null;
66 }
67 else
68 {
69 // get the resultant assembly.
70 generatedAssembly=results.CompiledAssembly;
71 }
72 }
73 catch(Exception ex)
74 {
75 return null;
76 }
77 return generatedAssembly;
78 }
79
80 /// <summary>
81 /// This method is used internally.
82 /// </summary>
83 protected virtual CodeDomProvider GetCompiler(string language)
84 {
85 CodeDomProvider cdp=null;
86 if ( (language=="C#") || (language.ToUpper()=="CS") )
87 {
88 cdp=new CSharpCodeProvider();
89 }
90 else if (language.ToUpper()=="VB")
91 {
92 cdp=new VBCodeProvider();
93 }
94 return cdp;
95 }
96
97 protected virtual Evidence GetEvidence(string url)
98 {
99 try
100 {
101 Evidence evidence=new Evidence();
102 //NamedPermissionSet nps = new NamedPermissionSet(
103 //evidence.AddHost(Zone.CreateFromUrl(url));
104 //evidence.AddHost(Site.CreateFromUrl(url));
105 evidence.AddHost(new Url(url));
106 return evidence;
107 }
108 catch(Exception)
109 {
110 return null;
111 }
112 }
113 }
114 }
用上面这个类来编译一段C#的代码,运行时抛出一个异常PolicyException,message是HRESULT 中的异常:0x80131418。2 using System.CodeDom;
3 using System.CodeDom.Compiler;
4 using System.Collections;
5 using System.Security;
6 using System.Security.Policy;
7 using System.Security.Permissions;
8 using Microsoft.CSharp;
9 using Microsoft.VisualBasic;
10 using System.IO;
11 using System.Windows.Forms;
12
13 using Bee.Core.Util;
14 using Bee.Core.Log;
15
16 namespace Bee.Core.Script
17 {
18 /// <summary>
19 /// DotNetScriptEngine 的摘要说明。
20 /// </summary>
21 public class DotNetScriptEngine
22 {
23 public DotNetScriptEngine()
24 {
25 //
26 // TODO: 在此处添加构造函数逻辑
27 //
28 }
29 /// <summary>
30 /// This method is used internally.
31 /// </summary>
32 public virtual Assembly Compile(ArrayList references, string language, string source,string url)
33 {
34 Assembly generatedAssembly=null;
35 // get a compiler for the appropriate language.
36 CodeDomProvider codeProvider=GetCompiler(language);
37 if(codeProvider == null)
38 {
39 return null;
40 }
41 // setup the compiler defaults. Maybe make this application definable at some point.
42 ICodeCompiler compiler=codeProvider.CreateCompiler();
43 CompilerParameters compilerParams=new CompilerParameters();
44
45 compilerParams.CompilerOptions = "/target:library /optimize";
46 compilerParams.GenerateExecutable=false;
47 compilerParams.GenerateInMemory=true;
48 compilerParams.IncludeDebugInformation=false;
49 compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
50 compilerParams.ReferencedAssemblies.Add("System.dll");
51
52 compilerParams.Evidence=GetEvidence(url);
53
54
55 try
56 {
57 // compile the source code
58 CompilerResults results=compiler.CompileAssemblyFromSource(compilerParams, source);
59 if (results.Errors.Count > 0)
60 {
61 foreach(CompilerError error in results.Errors)
62 {
63 OnLogCompileErr(error);
64 }
65 return null;
66 }
67 else
68 {
69 // get the resultant assembly.
70 generatedAssembly=results.CompiledAssembly;
71 }
72 }
73 catch(Exception ex)
74 {
75 return null;
76 }
77 return generatedAssembly;
78 }
79
80 /// <summary>
81 /// This method is used internally.
82 /// </summary>
83 protected virtual CodeDomProvider GetCompiler(string language)
84 {
85 CodeDomProvider cdp=null;
86 if ( (language=="C#") || (language.ToUpper()=="CS") )
87 {
88 cdp=new CSharpCodeProvider();
89 }
90 else if (language.ToUpper()=="VB")
91 {
92 cdp=new VBCodeProvider();
93 }
94 return cdp;
95 }
96
97 protected virtual Evidence GetEvidence(string url)
98 {
99 try
100 {
101 Evidence evidence=new Evidence();
102 //NamedPermissionSet nps = new NamedPermissionSet(
103 //evidence.AddHost(Zone.CreateFromUrl(url));
104 //evidence.AddHost(Site.CreateFromUrl(url));
105 evidence.AddHost(new Url(url));
106 return evidence;
107 }
108 catch(Exception)
109 {
110 return null;
111 }
112 }
113 }
114 }
stacktracestring是:
" at System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark)\r\n at System.Reflection.Assembly.Load(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence securityEvidence)\r\n at System.CodeDom.Compiler.CodeCompiler.FromFileBatch(CompilerParameters options, String[] fileNames)\r\n at System.CodeDom.Compiler.CodeCompiler.FromSourceBatch(CompilerParameters options, String[] sources)\r\n at System.CodeDom.Compiler.CodeCompiler.FromSource(CompilerParameters options, String source)\r\n at System.CodeDom.Compiler.CodeCompiler.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSource(CompilerParameters options, String source)\r\n at Bee.Core.Script.DotNetScriptEngine.Compile(ArrayList references, String language, String source, String url) in dotnetscriptengine.cs:line 66"
但程序的运行程序的权限是fulltrust的,而若GetEvidence(String Url)函数若是加载evidence.AddHost(Zone.CreateFromUrl(url));
是正常通过的。郁闷中,为何?