如何避免加载不必要的程序集 [英] How to avoid loading unnessecary assemblies

查看:78
本文介绍了如何避免加载不必要的程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET .NET 4.6 MVC4应用程序加载不必要的程序集。 System.Data.OracleClient。不使用Oracle。

ASP.NET .NET 4.6 MVC4 application loads unnessecary assemblie, eq. System.Data.OracleClient . Oracle is not used.

使用控制器中的代码创建组装依赖项

Assembly dependencies are created using code in controller

        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

Output is below. According to this System.Web references to System.Design and System.Design references to System.Data.OracleClient

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

MVC Application is running in VPS server with limited memory. How to fix this so that unnessecary assemblies are not loaded? This will hopefully free some memory in VPS server.

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绑定部分也不包含它:

web.config binding section does not also contain it:

  <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,但出现错误

I tried according to answer to place remove command to web.config but got error

还尝试根据答案使用

<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>
  ...

但出现错误:

如何修复?

推荐答案

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

There are three kind of assemblies on a web application.


  1. 组合在一起的程序集您使用的库(dll)

  2. 程序集是您的Web应用程序的编译版本

  3. 主要用于运行站点的程序集





在第一种情况下,您可以优化您的库,删除直接在dll项目上的所有不必要的引用。

Libraries

In the first one case, you can optimize yours libraries, removing any unnecessary reference direct on the project of your dll.

在第二种情况下,为了优化网页和编译内容,您需要删除所有不必要的内容参考后面的代码...例如页面通常使用系统第一行

In the second case to optimize your web page and what to compile you need to remove any unnecessary reference on the code behind... eg page usually have this first lines

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

您不使用的部分是灰色的,可以将其删除...

and what you do not use there are gray, you can remove it...

现在困难的部分-困难,因为您必须玩尝试/失败游戏。现在,您可以删除哪些内容以及从何处删除。

And now the difficult part - difficult because you must play the "try/fail" game. Now what you can remove and from where.

首先在哪里搜索以查找要删除的内容...答案是在 asp.net 上的全局web.config上。就我而言,我转到当前框架版本的 windows 目录中的 config 目录中,并找到 web.config

First where to I search to find what to remove... and the answer is on global web.config on asp.net. In my case I go to windows directory, on my curent framework version, on config directory and find the web.config

我在其中找到了一些键...。

There I locate some keys....

首先,您可以删除不必要的Http模块在 web.config

这是我在全局web.config上看到的内容

This is what I see on global 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,这就是删除不使用的内容的方法:

and I go to web.config on my application and this is how I remove what I am not use:

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

现在,在您继续进行程序集会话的情况下,这是我在全局web.config上看到的内容:

Now for you case I continue on assemblies session, this is what I see on global 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目录添加您的库。

this as it is I copy/paste to my web.config and place <clear /> on first line then I start remove the lines that I KNOW that my program is not use... and be careful to keep this line <add assembly="*" /> and at the end, this says to add your library from your dll directory.

            <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>

进行此操作并删除不使用的对象,方法是删除并查看您的应用程序是否仍在运行,您可以限制使用的程序集。

Making this and removing the one that you not use, by remove and see if your application is still working, you can limit a lot the assemblies that you use.

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

I make a simple test and by removing this line

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

这次我从83个减少到79个,仅加载了程序集。为什么要减去一个以上?因为 WorkflowServices 会自行加载更多内容。

I count from 83 down to 79 only assemblies loaded this time. Why more than one minus ? because WorkflowServices loads some more by them self.

要从配置部分删除某些内容,请再次在web.config上您可以这样做:

To remove something from configuration section, again on your web.config you do it as:

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

这篇关于如何避免加载不必要的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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