10

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.

Community
  • 1
  • 1
gokhanartuk
  • 101
  • 1
  • 2
  • 4
  • Cross posted to [here](http://forums.asp.net/t/1865767.aspx/1?DotNetOpenAuth+Asp+Couldnt+Load+Assembly+or+one+of+its+dependencies+in+MVC4+App+Unit+Tests) – Andrew Arnott Dec 15 '12 at 14:21

6 Answers6

10

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>
IamStalker
  • 3,813
  • 8
  • 50
  • 88
5

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.

Andy Evans
  • 6,195
  • 17
  • 62
  • 112
1

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.

Sidar Ok
  • 11
  • 2
0

Look for bindingRedirects in your web.config file and make sure they also exist in your unit test's app.config file.

Andrew Arnott
  • 74,820
  • 24
  • 127
  • 163
  • Dear Andrew, I already did what you say. But didnt work out. If you could just check the projects that I gave the link. You will see what I mean. And you will also see bindingredirects configuration in the both of the files. – gokhanartuk Dec 13 '12 at 19:32
  • I also edited by adding detailed log results getting from fusion logs. – gokhanartuk Dec 13 '12 at 22:10
0

Did you add the assemblies to your test project? They should show up in the folder when you compile. Since your test project is the host, it needs the same assemblies for test (depending on the setup but as a general rule)

If so, then you can look a step further in the actual binding details.

See my posting here on enabling fusion logging. How to enable assembly bind failure logging (Fusion) in .NET

Enable it, check the folder and look for bind failures - you'll see what assembly is trying to load it (usually). If you don't - enable successes and failures and in turn look at all the assembly versions. Sometimes you'll see multiple versions loaded which helps track down the issue, or an unexpected version.

Community
  • 1
  • 1
Adam Tuliper - MSFT
  • 29,569
  • 4
  • 49
  • 68
0

I changed the versions to 4.1.0.0 and it worked for me

 <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780CCD10D57B246" culture="neutral" />
    <bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780CCD10D57B246" culture="neutral" />
    <bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
  </dependentAssembly>
6dev6il6
  • 715
  • 1
  • 14
  • 33