如何避免加载不合规的程序集

时间:2016-10-17 09:22:20

标签: c# asp.net .net asp.net-mvc .net-assembly

ASP.NET .NET 4.6 MVC4应用程序加载unnessecary assemblie,eq。 System.Data.OracleClient。不使用Oracle。

使用控制器

中的代码创建程序集依赖项
        var sb = new StringBuilder();
        foreach (Assembly b in AppDomain.CurrentDomain.GetAssemblies())
        {
            sb.AppendLine(b.FullName);
            foreach (AssemblyName an in b.GetReferencedAssemblies())
                sb.AppendLine("   " + an.Name);
        }

输出如下。根据这个System.Web引用System.Design和System.Design对System.Data.OracleClient的引用

MVC应用程序在内存有限的VPS服务器中运行。如何解决这个问题,以便不加载unnessecary程序集? 这有望在VPS服务器中释放一些内存。

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   mscorlib
   System.Drawing
   System
   System.Configuration
   System.Core
   System.Data
   System.Xml
   System.DirectoryServices
   System.EnterpriseServices
   System.Web.RegularExpressions
   System.Design
   System.Web.ApplicationServices
   System.ComponentModel.DataAnnotations
   System.DirectoryServices.Protocols
   System.Security
   System.Runtime.Caching
   System.ServiceProcess
   System.Web.Services
   Microsoft.Build.Utilities.v4.0
   Microsoft.Build.Framework
   Microsoft.Build.Tasks.v4.0
   System.Windows.Forms
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
   mscorlib
   System.Configuration
   System.Xml
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
   mscorlib
   System
   System.Xml
   System.Numerics
   System.Security
System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
   mscorlib
   System
   System.Configuration
System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   mscorlib
   System
   System.Xml
   System.Security
   System.Core
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
   mscorlib
   System
   System.Data.SqlXml
   System.Configuration
System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   mscorlib
   System
   System.Data
   System.Configuration
Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
   mscorlib
   Microsoft.Build.Framework
   System
   System.Xml
   System.Core
...
System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
   mscorlib
   System.Configuration
   System.Data
   System.Transactions
   System
   System.EnterpriseServices

web.config绑定部分也不包含它:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

更新

我试着根据回答将删除命令放到web.config但是出错了

enter image description here

还尝试使用

根据答案删除oracle部分
<configuration>
  <configSections>
    <section name="system.data.oracleclient" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <remove name="system.data.oracleclient"></remove>
    </section>
  </configSections>
  ...

但得到了错误:

enter image description here

如何解决?

2 个答案:

答案 0 :(得分:1)

Web应用程序上有三种程序集。

  1. 与您使用的库(dll)一起组合的程序集
  2. 程序集,它们是Web应用程序的编译版本
  3. 主要运行网站的程序集
  4. 在前一种情况下,您可以优化您的库,直接在您的dll项目中删除任何不必要的引用。

    参考

    在第二种情况下,要优化您的网页以及编译什么,您需要删除任何不必要的代码背后的代码...例如,页面通常有第一行

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    

    你不使用的是灰色,你可以删除它......

    网络集会

    现在困难的部分 - 很难,因为你必须玩“尝试/失败”游戏。现在您可以删除哪些内容。

    首先我在哪里搜索以找到要删除的内容...答案是在 asp.net 上的全局web.config上。在我的情况下,我会在配置目录上的配置目录中的 windows 目录中找到web.config

    我找到了一些钥匙......

    首先,您可以在httpModule上的web.config会话中移除不必要的Http模块以加快管道

    这是我在全球web.config

    上看到的
    <httpModules>
        <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
        <add name="Session" type="System.Web.SessionState.SessionStateModule" />
        <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
        <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
        <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
        <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
        <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
        <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
        <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
        <add name="Profile" type="System.Web.Profile.ProfileModule" />
        <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
        <add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>
    

    我在我的应用程序上转到web.config,这就是我删除我不使用的内容:

    <httpModules>
      <remove name="Session" />
      <remove name="PassportAuthentication" />
      <remove name="AnonymousIdentification" />
    </httpModules>
    

    现在为你的情况我继续汇编会话,这是我在全球web.config上看到的:

    <compilation>
            <assemblies>
                <remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="mscorlib" />
                <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                <add assembly="System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.ServiceModel.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.WorkflowServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.Web.DynamicData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
                <add assembly="*" />
                <add assembly="System.Web.WebPages.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            </assemblies>
    

    这是因为它是我复制/粘贴到我的web.config并在第一行放置<clear />然后我开始删除我知道我的程序不使用的行...并小心保持这个第<add assembly="*" />行,最后说这是从你的dll目录添加你的库。

                <assemblies>
                    <clear />
                    <add assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                    <add assembly="mscorlib" />
                    <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    ....
                    <add assembly="*" />
                </assemblies>
    

    通过删除并查看您的应用程序是否仍然有效,删除并删除不使用的那个,可以限制您使用的程序集。

    我做了一个简单的测试并删除了这一行

    <add assembly="System.WorkflowServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    

    这次我只计算从83下降到79只装配的程序集。为什么不止一个减去?因为WorkflowServices自己加载了更多。

    要从配置部分删除某些内容,请再次在web.config上执行以下操作:

    <configuration> 
      <configSections>
            <remove name="system.data.oracleclient"></remove>
    ...
    

答案 1 :(得分:0)

我的工作站在整个machine.config中都引用了Oracle。我相信你会在那里找到你的流氓参考资料。

c:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config