Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DotNetOpenAuth.Asp Couldnt Load Assembly or one of its dependencies in MVC4 App Unit Tests

I am facing with very strange error in my project. I installed DotnetOpenAuth.Aspnet and Microsoft.AspNet.WebPages.OAuth libraries nuget packages. When I run the project there is no problem. But When I write the test for controllers it is throwing an exception like the following.

Test method MvcApplication2.Tests.ControllerTest.should_return_not_empty_content threw exception:

    System.IO.FileLoadException: Could not load file or assembly 'DotNetOpenAuth.AspNet, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246' 
or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Another strange point is If I setup a project in VS 2010 as MVC3 application and tests are passing. No failure. But When I do the exactly same setup in VS2012 it is firing the same error as above. When I search on stackoverflow I saw this solution but it didnt work either.

You can find all projects and sample tests in the following lines. It is just one app one test project. Very easy to read.

Also I added a sample code in here for controller and failing test.

The pastebin link for code preview is http://pastebin.com/1PCpq3hW

Any help would be appreciated.

Vs2010 and 2012 failing and succeeding projects

A detailed log result like the following


    *** Assembly Binder Log Entry  (13.12.2012 @ 22:27:31) ***

    The operation failed.
    Bind result: hr = 0x80131040. No description available.

    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\JetBrains\ReSharper\v7.0\Bin\JetBrains.ReSharper.TaskRunner.CLR4.exe
    --- A detailed error log follows. 

    === bind state information ===
    LOG: User = DEVELOPER-PC\DEVELOPER
    LOG: DisplayName = DotNetOpenAuth.AspNet, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246
     (Fully-specified)
    LOG: Appbase = file:///D:/Development/Coachius/CoachiusWeb.Tests/bin/Debug
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = NULL
    Calling assembly : CoachiusWeb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: D:\Development\Coachius\CoachiusWeb.Tests\bin\Debug\CoachiusWeb.Tests.dll.config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Post-policy reference: DotNetOpenAuth.AspNet, Version=4.0.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246
    LOG: GAC Lookup was unsuccessful.
    LOG: Attempting download of new URL file:///D:/Development/Coachius/CoachiusWeb.Tests/bin/Debug/DotNetOpenAuth.AspNet.DLL.
    LOG: Assembly download was successful. Attempting setup of file: D:\Development\Coachius\CoachiusWeb.Tests\bin\Debug\DotNetOpenAuth.AspNet.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: DotNetOpenAuth.AspNet, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246
    WRN: Comparing the assembly name resulted in the mismatch: Minor Version
    ERR: The assembly reference did not match the assembly definition found.
    ERR: Run-from-source setup phase failed with hr = 0x80131040.
    ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

like image 581
gokhanartuk Avatar asked Dec 13 '12 15:12

gokhanartuk


3 Answers

I have solved it with

 <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
          </dependentAssembly>
          <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>
like image 185
hackp0int Avatar answered Nov 15 '22 10:11

hackp0int


I think I may have found a possible solution.

See this link

The key pieces of information here are at lines 3 and 7. Basically, Microsoft.Web.WebPages.OAuth needs DotNetOpenAuth.Core 4.0.0.0, but the DotNetOpenAuth.Core I have is version 4.3.0.0.

like image 5
Andy Evans Avatar answered Nov 15 '22 10:11

Andy Evans


This is due to a very nasty bug in VS 2012. In VS 2012 in the test projects, assembly redirection in app.config file doesn't work.

This explains why it works in VS 2010 and not in VS 2012.

It is quite annoying.

A work around is to add a .testsettings file and associate it from the test menu to the project. You have to go to a solution folder for that, if not you will not see it in the menu.

Beware, a lot of resource on the internet says runsettings with forcing older version to work will do the trick, it will not. It will crash your VS / test process. You need a testsettings file.

What you are doing by that is to use VS2010 runner, which doesn't have this bug. On the other hand it is slower.

I hope Microsoft fixes this soon, the problem is not only with OpenAuth but literally with every DLL using a different version from another DLL.

Welcome to DLL Hell version year 2012.

like image 1
Sidar Ok Avatar answered Nov 15 '22 10:11

Sidar Ok