How to avoid loading unnessecary assemblies

后端 未结 2 1224
有刺的猬
有刺的猬 2021-01-19 11:58

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 con

相关标签:
2条回答
  • 2021-01-19 12:24

    My workstation at work has references to Oracle all throughout my machine.config. I believe you're going to find your rogue references there.

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

    0 讨论(0)
  • 2021-01-19 12:39

    There are three kind of assemblies on a web application.

    1. assemblies that are come together with the libraries (dlls) that you use
    2. assemblies that are the compiled version of your web application
    3. assemblies that are main to run the site

    Libraries

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

    Reference

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

    web assemblies

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

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

    First you can remove unnecessary Http Modules for faster pipeline in the httpModulesession on 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>
    

    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>
    

    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>
    

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

    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.

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

    <configuration> 
      <configSections>
            <remove name="system.data.oracleclient"></remove>
    ...
    
    0 讨论(0)
提交回复
热议问题