codeDomProvider code生成失败的Web项目而不是控制台应用程序 [英] CodeDomProvider Code Generation Fails in Web Project but not Console App

查看:222
本文介绍了codeDomProvider code生成失败的Web项目而不是控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是codeDomProvider编译一些C#code内存,然后执行code。一切正常,当我运行code在一个控制台应用程序。它编译,我可以执行它,并得到一个结果。

I'm using the CodeDomProvider to compile some C# code in-memory and then execute the code. Everything works fine when I run the code in a Console application. It compiles and I can execute it and get a result.

然而,当我尝试在Web应用程序中运行它这种完全相同的code失败。我得到一个BadImageFormatException和内部异常是坏的IL格式。

However, this same exact code fails when I try to run it inside a web application. I get a BadImageFormatException and the inner exception is Bad IL Format.

是否与Web应用程序与控制台应用程序,我不知道的一些限制?

Is there some restriction with web applications vs. console application that I'm unaware of?

感谢您的任何建议。

我在运行.NET 4.0。

I'm running .NET 4.0.

编辑:我发现一切正常,直到我添加一个引用到另一个组件从项目。所以,我有一个类库,其中包含我的实体模型的项目引用。我加入这就像一个参考:

I've noticed that everything works until I add a reference to another assembly from the project. So I have a class library with a project reference that contains my entity model. I'm adding it as a reference like:

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters cp = new CompilerParameters();

cp.GenerateInMemory = true;
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Core.dll");
cp.ReferencedAssemblies.Add("System.Data.Linq.dll");
cp.ReferencedAssemblies.Add("System.Data.Entity.dll");
cp.ReferencedAssemblies.Add("MyApp.Data.dll");

var results = provider.CompileAssemblyFromSource(cp, source);
var assm = results.CompiledAssembly;

如果我只是让我的班有返回一个字符串,删除加载项引用MyApp.Data.dll然后一切正常了一个方法。当我再补充一点参考回到失败。

If I just make my class have a single method that returns a string and remove the add reference to the MyApp.Data.dll then everything works. As soon as I add that reference back in it fails.

任何想法?

推荐答案

由于未能在添加引用的程序集我做了一些暴食和碰到<一href="http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-scripting/805/Dynamic-$c$c-Referenced-Assemblies-from-ASP-NET"相对=nofollow>这个讨论。基本上,这是一个问题,需要使用使用Server.Mappath使得code能找到的DLL。

Since it failed on adding the referenced assembly I did some Binging and came across this discussion. Basically it was an issue with need to use Server.MapPath so that the code could find the dll.

cp.ReferencedAssemblies.Add(HttpContext.Current.Server.MapPath("bin\\MyApp.Data.dll"));

现在它的作品!所以这是一个简单的错误。 :)

Now it works! So it was a simple mistake. :)

这篇关于codeDomProvider code生成失败的Web项目而不是控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆