Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing the ASP.NET Application to load the assembly from bin not from GAC

Is there any way to force my asp.net application to load the assembly from local bin directory since there is another older version of the assembly with the same name in the gac?

I can't delete the gac version since other applications are using it and I am facing some difficulties when adding the newer version to the gac.

like image 478
Khaled Musaied Avatar asked Jun 13 '09 19:06

Khaled Musaied


People also ask

Why would I avoid the GAC?

The best answer was that "The GAC is only useful if you register libraries which you're going to reuse." In other words, don't use it if you are not going to share libraries between different applications.

How do I find out if assembly is registered in GAC?

If you dont care that the assembly is actually in the GAC, but just loadable on the machine (from the appdomain) you can just use LoadAssembly with the assemblies name (strong, common, full, etc). If the assembly can be loaded by Fusion it will be and then you will know it exists.

Can weakly named assemblies be put into GAC?

Again, you cannot install an assembly into GAC unless the assembly is strongly named.

How do I install an assembly in the GAC using Visual Studio?

The syntax for using gacutil.exe to install an assembly in the GAC is as follows: In this command, <assembly name> is the name of the assembly to install in the global assembly cache. If gacutil.exe isn't in your system path, use Visual Studio Developer Command Prompt or Visual Studio Developer PowerShell.

How do I share assemblies between applications?

You should share assemblies by installing them into the Global Assembly Cache only when you need to. As a general guideline, keep assembly dependencies private, and locate assemblies in the application directory unless sharing an assembly is explicitly required.

Should assemblies be stored in the application directory or global cache?

As a general guideline, keep assembly dependencies private, and locate assemblies in the application directory unless sharing an assembly is explicitly required. In addition, it is not necessary to install assemblies into the Global Assembly Cache to make them accessible to COM interop or unmanaged code.

Is there a way to change the default GAC of an assembly?

No there is no way to do this. When loading an assembly the CLR will check to see if a DLL with an equivalent strong name is present in the GAC. If there is a matching assembly in the GAC it will pick the GAC assembly every time. There is unfortunately no way to override this behavior.


3 Answers

I found it

To force your application to read from local bin directory you have to remove signing from your assembly and then the application will load the assembly from bin.

Thanks Wyatt Barnett and murad.

like image 195
Khaled Musaied Avatar answered Oct 16 '22 23:10

Khaled Musaied


Change the version number, strong name the assembly and reference the strongly named higher version you deploy with your solution.

like image 28
Wyatt Barnett Avatar answered Oct 16 '22 21:10

Wyatt Barnett


The oldVersion configuration that is suggested by Muse VSExtensions works! You can use the strong name for the local assembly: Please look this page: http://msdn.microsoft.com/en-us/library/7wd6ex19.aspx

Basically in web.config add something like:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DllName"
          publicKeyToken="0123456789abc"
          culture="neutral" />        
        <!-- Assembly versions can be redirected in app, 
          publisher policy, or machine configuration files. -->
        <bindingRedirect oldVersion="2.0.0.0-2.5.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>      
    </assemblyBinding>  
  </runtime>

That way if i have an assembly in the gac that can be from version 2.0.0.0 to version 2.5.0.0 all the calls would be redirected to the newVersion (3.0.0.0)

In the assemblies section I added the assembly:

<add assembly="DllName, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0123456789abc" />

And that's it.

like image 29
Paco Zarate Avatar answered Oct 16 '22 21:10

Paco Zarate