假设:
app.config;和那么内部加载了哪个版本的 .NET 以在客户端环境中执行应用程序?
下面的控制台应用程序将在控制台中简单地显示其 CLR 版本为 v4.0.30319,但@Reed Copsey 的堆栈答案 ( CLR 2.0 vs 4.0 performance? ) 显示 .NET 2.0 已加载这个案例。此外,在 MSDN它表示当 useLegacyV2RuntimeActivationPolicy 设置为 false false 时:
Use the default activation policy for the .NET Framework 4 and later, which is to allow legacy runtime activation techniques to load CLR version 1.1 or 2.0 into the process.
尽管 app.config 具有 .NET 4.0 配置,但听起来似乎加载了 .NET 2.0。我有没有误解什么?
C#源代码
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string version = Environment.Version.ToString();
Console.WriteLine(version);
}
}
}
app.config
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="false">
<supportedRuntime version="v4.0.30319"/>
</startup>
</configuration>
最佳答案
最重要的是,在您的场景下,您将 .Net 4 指定为唯一受支持的运行时,因此您的应用程序将加载 CLR 4。
您的程序的 CLR 行为完全符合设计:
当我使用 supportedRuntime 作为 v4.0 运行您的测试应用程序时,Process Explorer 显示它加载 mscorlib v4.0.30319。
当我使用 supportedRuntime 作为 v2.0.50727 运行时,Process Explorer 显示它加载 mscorlilb v2.0.50727。
当我在没有 supportedRuntime 元素的情况下运行时,Process Explorer 显示它加载了 mscorlilb v2.0.50727。
This blurb from Microsoft说明 supportedRuntime 元素定义了程序运行的特定版本:
By default, an application runs on the version of the .NET Framework that it was built for. If that version is not present and the application configuration file does not define supported versions, a .NET Framework initialization error may occur. In this case, the attempt to run the application will fail.
To define the specific versions on which your application runs, add one or more elements to your application's configuration file. Each element lists a supported version of the runtime, with the first specifying the most preferred version and the last specifying the least preferred version.
这里有两个独立的元素在起作用。只有 supportedRuntime 元素适用于您的方案。
supportedRuntime 元素定义您的应用程序将按首选顺序运行的 CLR 版本。如果您列出支持的运行时,那么将使用这些 CLR 版本,从上到下在列表中向下查找,直到找到已安装的 CLR 版本。如果您没有列出支持的运行时,那么您的程序将使用编译它所针对的 CLR 版本运行。
useLegacyV2RuntimeActivationPolicy 元素仅适用于混合模式程序集 --- 包含托管 (.Net) 和非托管( native )代码的程序或 DLL。您的示例程序不是混合模式程序集。对于混合模式程序集,将值设置为 false(默认值)或不全部设置,使用新的 .Net 4 进程内并行加载混合模式程序集,因此您的应用程序可以使用 CLR 4 运行,并使用 CLR 1.0-2.0 在同一进程中加载混合模式程序集。将其设置为 true 基本上会恢复到 .Net 4 之前的先前功能,其中进程内并排功能被禁用,并且选择运行应用程序的任何 CLR 版本都将尝试加载您的混合模式程序集。用于加载混合模式程序集的 CLR 版本将是选择运行应用程序的版本,具体取决于用于编译应用程序的版本,以及您列出的支持的运行时(如果有)。
有一个 MSDN Magazine article和一个 MSDN article关于 COM 组件的 .Net 4 加载和进程内并排 (In-Proc SxS) 执行,这也会对没有 COM 组件的情况产生影响。在 .Net 4 之前,如果您使用某个版本的 CLR 编译您的应用程序,并且该版本在运行时在系统上不可用,则该应用程序将自动在已安装的更新版本的 CLR 上运行。从 .Net 4 开始,应用程序现在不会使用较新版本的 CLR 运行,除非您在 supportedRuntimes 元素中指定较新版本。
这是 MSDN 文章中的引述:
Application developers. Side-by-side hosting has almost no effect on application developers. By default, applications always run against the version of the .NET Framework they were built on; this has not changed. However, developers can override this behavior and direct the application to run under a newer version of the .NET Framework (see scenario 2).
Library developers and consumers. Side-by-side hosting does not solve the compatibility problems that library developers face. A library that is directly loaded by an application -- either through a direct reference or through an Assembly.Load call -- continues to use the runtime of the AppDomain it is loaded into. You should test your libraries against all versions of the .NET Framework that you want to support. If an application is compiled using the .NET Framework 4 runtime but includes a library that was built using an earlier runtime, that library will use the .NET Framework 4 runtime as well. However, if you have an application that was built using an earlier runtime and a library that was built using the .NET Framework 4, you must force your application to also use the .NET Framework 4 (see scenario 3).
最后,如果您使用的是 Vista、Win7、Server 2008、Server 2008 R2,you automatically have CLR 2.0 installed .因此,如果您要删除 supportedRuntimes 元素,或将其更改为 v2.0.50727,您可能仍然可以使用 CLR 2.0 作为运行时。
关于c# - 在 .net 4.0 上执行应用程序时,在 .net 2.0 下编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6921976/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou