.NET Framework 4 与 .NET Framework 4 Client Profile

      今天碰到的一个问题和Client Profile相关的。问题是这样的:一个WPF工程,需要引用另外几个.NET的assembly, 在WPF工程中添加了对这几个assembly的引用,并在程序中可以添加了在这几个assembly中定义的类型(例如:MyCompany.Foo,并在程序头部添加了using MyCompany),到此没有任何问题。可是一编译,报了一大堆错误出来,都是说引用类型或者namespace找不到,其中一条错误如下:

Error 6836 The type or namespace name 'MyCompany' could not be found (are you missing a using directive or an assembly reference?) C:/SourceCode/Project/.../MyCode.cs

      可是明明在项目的Reference中已经加入了对相关assembly的引用,并且也能够在Object Viewer查看到应用的assembly中定义的类型。这到底是咋回事呢?让人挠头啊......

      无意中在项目的Property页面中看到,该工程的Target framework是 .NET Framework 4 Client Profile。在把它改成了.NET Framework 4以后就可以后,整个工程就可以编译通过了。为啥会这样?

      原来默认创建的WPF工程都是瞄准.NET Frameworks 4 Client Profile,除了WPF之外还有其他很多工程也都是如此,参见.NET Framework Client Profile,并且该页面还注明使用Client Profile应用是不能够应用非Client Profile的assembly的:

If you are targeting the .NET Framework 4 Client Profile, you cannot reference an assembly that is not in the .NET Framework 4 Client Profile. Instead you must target the .NET Framework 4. For more information, see Troubleshooting .NET Framework Targeting Errors.

同时在给出的链接中也列出了如果没有遵守这条规则,可能会出现的错误:

When your application targets a particular profile, you might encounter errors if you try to reference an assembly that is not part of that profile. Common errors include the following:

  • The type or namespace name "name" does not exist in the namespace "namespace". (Are you missing an assembly reference?)
  • Type "typename" is not defined.
  • Could not resolve assembly "assembly". The assembly is not listed as part of the "profile" Profile.

These errors can result from different actions. This topic includes descriptions of what might have caused the error and how to resolve the issue.

      呵呵,是不是Yong给出的错误信息很相像啊,所以他原因的是因为那些引用的assembly中包含有非Client Profile中的assembly。那么到底哪些是Client Profile中的assembly,哪些又不属于呢? Assemblies in .NET Framework Client Profile列出了所有的属于Client Profile的assembly,不在其列的就属于Client Profile,如果要引用这样的非Client Profile的assembly,你的应用就必须要选择.NET Framework。

      对于这个问题,编译器或者Visual Studio可以做得更好一些,提供更有指向性的错误信息,来帮助准确定位错误来源。


      最近使用Visual Studio 2010 创建来创建Workflow的Activity,遇到了一个很奇怪的问题。在我创建了一个Workflow ActiveLibrary工程时明明选择的Target Framework = .Net Framework 4 (以下简称为.NET 4 Full),然而创建的工程却显示Target framework = .NET Framework 4 Client Profile (以下简称为.NET 4 Client Profile)。一头雾水啊,啥是".NET Framework 4 Client Profile啊?仔细再看看,发现不只是这个,还有.NET Framework 3.5 Client Profile和.NET Framework 3.5 Server Core Profile。

      其实,一开始我也没有意识到这些东东,当我想为工程添加一个对Microsoft.TeamFoundation.Build.Client.dll的Reference时侯,发现怎么也找不到这个Assembly,明明就是放在GAC中的,咋个就找不到呢?然后左查又看,才发现这个Target framework设置有些异样,再把它从新设为.NET Framework 4后,就能够找到了。

      先不说这个Client Profile是干啥用的(我想应该是.NET Framework的一个子版本),VS这样的设计还是有些问题的。创建时所选择的Target Framework应该和创建后工程的设置是一致,虽然两者可能仅是小有不同,但如果有问题的话很难查找,而且花费这样的时间是很不值得的。

       那么到底加了Client Profile和不加的有啥区别呢? Bing一下就会有发现了。(Bing的照片很是Nice,所以偶尔回去用看看照片顺带着也搜索一下,呵呵!)Jossef的博客What’s new in .NET Framework 4 Client Profile Beta 2 给出了详细的解释。简而言之,加了Client Profile的.NET Framework是不加的子集或者说是精简版(Compact Version)。

为什么要引入这个简化版本呢?

  1. 减少.NET Framework以及包还.NET Framework的应用程序的部署时间;
  2. 减少.NET Framework部署的失败;
  3. 减少.NET Framework在ISV软件的所占大小,从而给其软件更多的空间;
  4. 减少安装了.NET Framework Client Profile的系统受攻击的“界面”,因为Client Profille中部包括ASP.NET和一些服务器所要的组件;

总之,就是Client Profile的Framework更小更精简了,到底小了多少呢?看看下面的表就是知道了:

减少了7 ~ 8MB左右,也不是太明显!不过第4点还是很吸引人的,减少了被攻击的可能。

 

原文地址:https://www.cnblogs.com/ShaYeBlog/p/5552844.html