Could not load file or assembly 'WebGrease, Ve

2019-03-08 17:23发布

问题:

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

please advice what i am missing.

回答1:

Have you tried through NuGet ?

Install-Package WebGrease -Version 1.5.1

or

Install-Package WebGrease -Version 1.5.2


回答2:

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.



回答3:

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>


回答4:

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!



回答5:

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.



回答6:

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


回答7:

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



回答8:

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



回答9:

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



回答10:

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



回答11:

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.



回答12:

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.



回答13:

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>


回答14:

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 Sami, Oweis 4 Computer



回答15:

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