我使用如下代码进行源码动态编译:
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace);
//生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
// ICodeCompiler icc = csc.CreateCompiler();
//设定编译参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
//cplist.GenerateInMemory = true;
cplist.GenerateInMemory = false;
cplist.OutputAssembly = assemblyname;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
//编译代理类
CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
assembly = cr.CompiledAssembly;
---
编译完成后,在D:/CacheTemp/下能找到动态生成的DLL。但是,在
assembly = cr.CompiledAssembly; 这一步却发生异常,提示说未能找到程序集,没有授予运行权限。
这个该怎么处理呢?
web应用程序没有执行D:/CacheTemp/目录下代码的权限,把dll移动到bin目录下试试。
将 ASPNET 账户添加到 D:/CacheTemp/ ,并赋予运行权限
学习了...