Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

C#.Netasp.net Mvc-4DllWebgrease

C# Problem Overview


I have an MVC4 Web API project. While running the service project I am getting an error

> Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

I have added the WebGrease.dll file and still its coming error

And tried with changing web.config

<bindingRedirect oldVersion="0.0.0.0-1.3.0.0"/>"

And

<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />

C# Solutions


Solution 1 - C#

I know it's kind of late for the OP but I ran into the same problem while trying out the Bootstrap 3 for MVC 4 NuGet package, in my case it had something to do with the Microsoft.AspNet.Web.Optimization package, and managed to find a simple solution.

Try executing the following commands in the package manager console:

Install-Package Microsoft.AspNet.Web.Optimization
Update-Package WebGrease
Uninstall-Package Microsoft.AspNet.Web.Optimization
Uninstall-Package WebGrease
Install-Package Microsoft.AspNet.Web.Optimization
Update-Package WebGrease

The first two lines had no effect for me since those packages were already installed and updated by the Bootstrap 3 for MVC 4 package, but I ran them anyway and then it all compiled and ran great.

Solution 2 - C#

This error is because Microsoft.AspNet.Web.Optimization 1.1.3 internally references WebGrease 1.5.1.25624 even though the Nuget package, itself, has a dependency on WebGrease 1.5.2.14234. Someone clearly messed up while creating the Nuget package.

To solve this, add this assembly binding in your Web.Config.

  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.1.25624" newVersion="1.5.2.14234" />
  </dependentAssembly>

Solution 3 - C#

Have you tried through NuGet ?

Install-Package WebGrease -Version 1.5.1

or

Install-Package WebGrease -Version 1.5.2

Solution 4 - C#

For me, none of above scenarios worked.

After trying for two days, finally i found the solution.

What i did was, i uninstalled Microsoft.AspNet.Web.Optimization and WebGrease both.

I knew that this optimization assembly internally refers to WebGrease 1.5.1. So i chose a version of optimization which does not refer to WebGrease 1.5.1.

I ran following commands in order to make everything work.

Uninstall-Package Microsoft.AspNet.Web.Optimization
Uninstall-Package WebGrease
Install-Package Microsoft.AspNet.Web.Optimization -Version 1.1.0

I hope i will be able to be a good help for someone for whom above solution does not work.

Cheers!

Solution 5 - C#

The issue I observed matched closely what Jidheesh Rajan mentioned. However, updating the package from Nuget package manager (without specifying version) did not fix the issue. Here is what I had to do to fix the issue. (Essentially, I explicity updated WebGrease to version 1.6)

From within the package manager console in Visual Studio, explicitly update WebGrease to version 1.6 . here is the command.

Install-Package WebGrease -Version 1.6

Output should be

Removing 'WebGrease 1.5.2' from {project name}

Adding 'WebGrease 1.6.0' to {project name} Successfully added 'WebGrease 1.6.0' to {project name} Uninstalling 'WebGrease 1.5.2'. Successfully uninstalled 'WebGrease 1.5.2'.

This ended up updating web.config with

    assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /
    bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" 

This also resulted in the removal of a line from the csproj file.

<WebGreaseLibPath>..\packages\WebGrease.1.5.2\lib</WebGreaseLibPath>

and an update in this node in the csproj file

<Reference Include="WebGrease, Version=1.5.2.14234, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
  <Private>True</Private>
</Reference>

to this

<Reference Include="WebGrease, Version=1.6.5135.21930, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>..\packages\WebGrease.1.6.0\lib\WebGrease.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
  <Private>True</Private>
</Reference>

Keep in mind that I didnt have an issue on my local machine, just on a stage web server. I wanted to try my best to resolve the issue without updating the package, but this was the only fix I had.

Solution 6 - C#

Tried a lot of things but this worked for me sometimes webgrease assembly issues sometimes Web.Optimization assembly issues. This worked for me with Microsoft.AspNet.Web.Optimization 1.1.3

Update-Package WebGrease -Version 1.6

Solution 7 - C#

Oldie but goldie...

I was working on a MVC 5 project in Visual Studio 2013, and I had the same problem. We were using Visual SVN for the versioning and I was the only one in my team that had this problem.

The only thing that worked for me was this:

> 1. Uninstall Microsoft.AspNet.Web.Optimization > 2. In Nuget command prompt run: install-package Microsoft.AspNet.Web.Optimization -Version 1.0.0

This will install an older version of Web.Optimization. The project was initially referencing version 1.1.0. The problem is not with WebGrease, but with the System.Web.Optimization.dll that is referencing an old, inexisting version of WebGrease.

When I ran the project, it worked, but I wanted to use the same package version as the rest of team. So, I tried to update but... the error returned...

After this, I deleted the project (again) and took it back from the SVN. To my surprize, it started to work... The thing is that I had deleted the project completely and took it from the SVN several times before this. I even took the dlls from a colleague because I thought that maybe I'm getting corrupted files from NuGet, but to no avail.

I hope this will help someone, someday.. with Visual Studio 2016 and MVC 8 :)

Solution 8 - C#

When you redirect webgrease make sure you don't have an appliesTo attribute on your assemblyBinding element. For me I removed it completely.

enter image description here

Solution 9 - C#

Since I wasn't able to uninstall using package manger console, I manually went to packages.config and comment out the older version.

Solution 10 - C#

I used "Install-Package Microsoft.AspNet.Web.Optimization" (current version is 1.1.3) for starup web-project, which was not selected in NuGet console by default.

> But NuGet has created a new app.config file even though web.config is > expected to be updated.

For me it worked to move WebGrease dependentAssembly element from app.config to web.config's <runtime><assemblyBinding>...</assemblyBinding></runtime>.

Solution 11 - C#

The other answers did not resolve this for me. We are adding a newer MVC 5 API project and it wasn't playing nice with the older MVC 2 API's. After running package updates I was getting the mentioned error. The resolution for me was to remove the WebGrease references added by NuGet in the web.config's of the offending projects.

To Resolve: Open the web.config of the project(s) throwing the exception and delete or comment out the dependency added for WebGrease.

Solution 12 - C#

Alreasy resolved this error. Situation was simpler than i thought. When you install from nuget new version of System.Web.Optimization it has reference to

// References: WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35

At same time i have WebGrease version 1.5.2.14234. Running project, we have this error: Could not load file or assembly 'WebGrease, Version=1.5.1.25624

Soo, i try to check this version on nuget and was surprized, that it wasn't...but when i create new project with VS - i got it. To hack this error i try to install it in GAC..but gac says me, that this lib is not subscribed. Downgrading to System.Web.Optimization Version=1.1.0.0 , where reference is:

// References: WebGrease, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

and 1.3.0.0 is strongly subscribed - helped me. It works. After upgrading webgrease, i have it Version=1.6.5135.21930 and it worked perfectly.

At conclusion, i have an opinion, than this error (Could not load file or assembly 'WebGrease, Version=1.5.1.25624) was because of that System.Web.Optimization lib is referenced to unsubscribed webgrease dll version.

Solution 13 - C#

I had this issue during a deployment.

I copied WebGrease.dll to production, but I forgot to update the Web.Config file as well.

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Solution 14 - C#

Here is what I did to get around it,

  1. I saved a copy of the Web.config file of my current (corrupted project) to "Corrupted-Web.Config". and close the project solution.
  2. Created a new application with the same type of my courrpted project. called it ProjTemp.
  3. Save and Run the "PorjTemp", then Save a copy of the Web.Config to "OrigTemp_Web.config"
  4. Close the Web.config files.
  5. On the "ProjTemp" open Tools --> "NuGet Package Manager" --> "Manage NuGet Packages for Solutions" ---> Select to Update all related installed packages. 6 Now Open the newly updated Web.config file. Copy the get the info from the Updated "" Starting from "

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
             --- Copy this section to replace in the corrupted file.---
       </assemblyBinding>
</runtime>


  1. Save your project. Rebuild and Run. Hope this will help! ....

Here is the corrected one for example, VS2015.


<runtime>
           <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                 <dependentAssembly>
                        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
                        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
                 </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
              </dependentAssembly>
              <dependentAssembly>
                        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
                        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /
              </dependentAssembly>
              <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /
                </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                </dependentAssembly>
                <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                 </dependentAssembly>
                 <dependentAssembly>
                        <assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
                         <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
                  </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>
          </assemblyBinding>
  </runtime>


that's what I have done to fix it, [Solution on Github ][1] [1]: https://github.com/NuGet/Home/issues/1379#issuecomment-222285622

Sami, Oweis 4 Computer

Solution 15 - C#

This problem may also arise if you do not deploy Web.config file to the web server.

Solution 16 - C#

VS 2017 seems to have the problem of not deploying the web.config, even though the properties for the file are set to 'copy always'. In my project, I have to swap out different web.config files for various deployment scenarios. And the 'webgrease' error will appear if it did not deploy the file.

Just verify that the web.config file gets deployed.

Solution 17 - C#

I've faced the excat same issue and managed to fix it by doing the following:

  1. Uninstalling the Microsoft.AspNet.Web.Optimization nuget package.
  2. Uninstalling the Antlr nuget package because its a dependecy for WebGrease package.
  3. Uninstalling the WebGrease nuget package.
  4. Install Microsoft.AspNet.Web.Optimization nuget package again, this action will take care of installing Antlr and WebGrease because they are its dependencies.

Solution 18 - C#

In my case this was not solved by any of the solutions above. The error relating to webgrease was actually a red herring ... it was a problem with an invalid (and completely unrelated) assembly binding redirect. I guess this problem with my web.config file was causing all the assembly binding redirects to fail, and it just so happened this was causing a runtime issue with WebGrease.

Basically, during a merge one of the binding redirects had got corrupted and it had ended up with two assembly redirects within a single <dependentAssembly> tag.

So, if you are getting this issue and you already have the binding redirect set up correctly for WebGrease, it is worth scanning through all your other assembly binding redirects to check that non of them have been corrupted.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJidheesh RajanView Question on Stackoverflow
Solution 1 - C#c0y0teXView Answer on Stackoverflow
Solution 2 - C#Andre PenaView Answer on Stackoverflow
Solution 3 - C#Fernando VezzaliView Answer on Stackoverflow
Solution 4 - C#jparthjView Answer on Stackoverflow
Solution 5 - C#jbookerView Answer on Stackoverflow
Solution 6 - C#Ali UmairView Answer on Stackoverflow
Solution 7 - C#Alex AlbuView Answer on Stackoverflow
Solution 8 - C#kenjaminView Answer on Stackoverflow
Solution 9 - C#user3557878View Answer on Stackoverflow
Solution 10 - C#Anton LyhinView Answer on Stackoverflow
Solution 11 - C#rjchicagoView Answer on Stackoverflow
Solution 12 - C#faradzenView Answer on Stackoverflow
Solution 13 - C#tno2007View Answer on Stackoverflow
Solution 14 - C#SamiView Answer on Stackoverflow
Solution 15 - C#David SanchezView Answer on Stackoverflow
Solution 16 - C#Brian BlackView Answer on Stackoverflow
Solution 17 - C#Voice Of The RainView Answer on Stackoverflow
Solution 18 - C#soupy1976View Answer on Stackoverflow