12

I have a WPF application that follows the MVVM pattern. We recently signed the app and now I am getting a lot of first chance exceptions on startup. I have traced the problem to the following:

In any view, if I reference another namespace with in the application when the view is initialized I get the error:

"Could not load file or assembly 'MyApplication, Version=3.0.5917.24348, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"MyApplication, Version=3.0.5917.24348, Culture=neutral, PublicKeyToken=xxxxxxxxxxx"

Its always looking for a version that is 1 behind the version that I'm actually running.

If I remove the references to the other namespaces from the views, the InitializeComponent() does not throw the error

View:

<UserControl x:Class="MyApplication.View.DiagnosticsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:convert="clr-namespace:MyApplication.Converters"  <!--Causes error-->
             xmlns:behave="clr-namespace:MyApplication.Behaviors" <!--Causes error-->
             xmlns:controls="clr-namespace:MyApplication.UserControls"  <!--Causes error-->

If I remove these references, and move my converters and behaviors into another DLL and then reference them through the DLL there is no problem. The errors go away. Also If I don't sign the application I do not get the errors. I don't really want to have to reference these things in a different DLL, it seems like this should work fine. It also spends about 30 seconds throwing all of these errors as all of the views are created, so I am taking a hit on performance. I dont get why the application is trying to load itself, and why its trying to load an older version of itself. No matter how many times I build, the error is always 1 version behind.

Fusion Log:

*** Assembly Binder Log Entry  (3/17/2016 @ 10:30:11 AM) ***

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:\tfs\Development\dev-feature\src\MyApplication\bin\Debug\MyApplication.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = MyApplication, Version=3.0.5920.15594, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx
 (Fully-specified)
LOG: Appbase = file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyApplication.exe
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\tfs\Development\dev-feature\src\MyApplication\bin\Debug\MyApplication.exe.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: MyApplication, Version=3.0.5920.15594, Culture=neutral, PublicKeyToken=7b0591cb18d2a932
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/MyApplication.DLL.
LOG: Attempting download of new URL file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/MyApplication/MyApplication.DLL.
LOG: Attempting download of new URL file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/bin/MyApplication.DLL.
LOG: Attempting download of new URL file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/bin/MyApplication/MyApplication.DLL.
LOG: Attempting download of new URL file:///C:/tfs/Development/dev-feature/src/MyApplication/bin/Debug/MyApplication.EXE.
LOG: Assembly download was successful. Attempting setup of file: C:\tfs\Development\dev-feature\src\MyApplication\bin\Debug\MyApplication.exe
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyApplication, Version=3.0.5920.15596, Culture=neutral, PublicKeyToken=7b0591cb18d2a932
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
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.
soccer7
  • 3,547
  • 3
  • 29
  • 50
user1336827
  • 1,728
  • 2
  • 15
  • 30
  • I found that if i set a hard version in the application properties such as 3.0.0.0 i do not get the error. However when i have the version 3.0.* I do get the errors. – user1336827 Mar 16 '16 at 16:19
  • How are you signing it? – zastrowm Mar 16 '16 at 20:45
  • Im checking "Sign the assembly" under the signing tab in project -> properties, then selecting our key file. – user1336827 Mar 16 '16 at 20:47
  • How are you setting the version? – zastrowm Mar 17 '16 at 15:13
  • the version is set in the project properties assembly information: 3.0.* so the version is generated by VS – user1336827 Mar 17 '16 at 15:25
  • This may be a long shot, but I know the XAML Designer is far from bugfree and from time to time I have to kill it, to get my project built, because it does not release some locks on output files or temporary files. Could you try to 1. close all XAML Designer Tabs 2. kill all instances of XDesProc.exe (the XAML Designer) and then make a clean & rebuild – derpirscher Mar 17 '16 at 21:56
  • Is the behaviour the same when you start the program outside of visual studio? – this.myself Mar 22 '16 at 06:53
  • when the application is signed and i attach the debugger to the debug release, yes I can still see them being thrown. I cant tell in the release because I cannot attach the debugger. We have decided to just not sign the app. – user1336827 Mar 22 '16 at 13:04
  • Do you have one or more projects in solution? If more than one project then I suggest to check for circular references. Consider the following case: in project B you try use type from the project A which already has reference to project B. If you use Resharper, it will suggest to reference the assemby of project A that already have been built. It may cause the problem that you have described in your question. – nicolas2008 Mar 22 '16 at 22:33

2 Answers2

1

Edit:

Did you use ProcessMonitor to see where Visual Studio is loading v 3.0.5917.24348 from? Visual Studio wants v3.0.5920.15596 so you have to put that DLL where its expected.

Edit 2:

Can you put in a binding redirect in your config file like this?

<dependentAssembly>
 <assemblyIdentity name="xxxxxx" publicKeyToken="121fae78165ba3d4"/>
 <bindingRedirect oldVersion="3.0.5920.15596" newVersion="3.0.5917.24348"/>
</dependentAssembly>

Ref: .Net picking wrong referenced assembly version

Original:

One reason you might get the error:

Could not load file or assembly 'MyApplication, Version=3.0.5917.24348, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

Is when your assembly is signed and your reference to it has the Specific Version property set to True, resulting in FileLoadException.

Check you have Specific Version set to False:

enter image description here

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I don't have a reference to the project in the project... MyApplication is the running application. And it is complaining that it cant load itself, because the version doesn't match. – user1336827 Mar 17 '16 at 13:02
  • Oh well was worth a mention – Jeremy Thompson Mar 17 '16 at 13:03
  • Can you upload a bare bones solution to a tempsend.com site for me to reproduce? – Jeremy Thompson Mar 17 '16 at 13:05
  • unfortunately I cannot reproduce this consistently with a test app. I was able to intermittently get it to occur by creating a control, adding an image to the control referenced as MyApplication;component/Images/Image.jpg then signing the app. first build would fail, then seemed to run fine. – user1336827 Mar 17 '16 at 13:09
  • Can you do a ProcessMonitor trace (FileMon log) just verify the DLL IS being loaded from the correct location? – Jeremy Thompson Mar 17 '16 at 13:10
  • its not a DLL, the project is a windows executable, and its complaining that it cant load itself. – user1336827 Mar 17 '16 at 13:40
  • Have you got a fuslog? Your app is obviously an EXE, why its giving you a "load failure" manifest style is key to the problem. Hence fuslog for a clue to the binding error. Bare in mind that it's a generic error message and DLLs are the same as EXE's except for the starting point. Please use the tools I mention to try hunt this down. – Jeremy Thompson Mar 17 '16 at 14:00
  • *its not a DLL, the project is a windows executable, and its complaining that it cant load itself.* why load itself! Somethings obviously wrong and there is no need to load an instance of the same AppDomain to itself to call voids/functions. – Jeremy Thompson Mar 17 '16 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106620/discussion-between-user1336827-and-jeremy-thompson). – user1336827 Mar 17 '16 at 15:32
0

Is it possible that you're trying to use strong-named URI references in your XAML? For example, by setting the AssemblyPublicKeyToken attribute your project file or by modifying the generated code-behind for the XAML?

If your XAML references are using a strong name and you use a constantly-changing version, then your XAML may end up using the previous version of the project, because the references are generated prior to the build being completed (and the new version being set).

To check, find your generated for your XAML under the obj directory and check for Uris (for example ~\obj\Debug\TestControl.g.i.cs):

System.Uri resourceLocater = new System.Uri("/T_Signing;V1.0.0.0;76005ee8ffcf5f2d;component/testcontrol.xaml", System.UriKind.Relative);

Above the URI has the full name and version. Whereas if you don't have strong named references, the uri would be more like:

System.Uri resourceLocater = new System.Uri("/T_Signing;component/testcontrol.xaml", System.UriKind.Relative);
Community
  • 1
  • 1
zastrowm
  • 8,017
  • 3
  • 43
  • 63
  • System.Uri resourceLocater = new System.Uri("/MyApplication;component/view/system/systeminformationview.xaml", System.UriKind.Relative); is what i have so that means they are not strongly named? So how can i fix this? – user1336827 Mar 16 '16 at 21:53
  • @user1336827 in your case, strong named would be bad. The fact that your reference doesn't have the public key means that the above answer may not apply to you. Do you know if the project has any pre-build steps? – zastrowm Mar 16 '16 at 21:55
  • no pre or post build events. The solution does have 4 projects, however the project in question does not reference any of the others, nor do any of the others reference it. I dont think that would have an effect though. – user1336827 Mar 16 '16 at 22:05
  • @user1336827 are you able to reproduce in a standalone project? (by copying some of the files over to an empty project)? It may be something in your project that got in there somehow. – zastrowm Mar 16 '16 at 22:06
  • unfortunately not, I did try to reproduce in a stand alone simple project. I could get it to throw the error some times but then clean and rebuild and it would not throw the error. – user1336827 Mar 16 '16 at 22:07
  • 1
    @user1336827 if it doesn't reproduce on a clean and rebuild, is it possible that the project causing the issue has a reference to itself (e.g. the previous edition of itself)? – zastrowm Mar 16 '16 at 22:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106514/discussion-between-mackiechan-and-user1336827). – zastrowm Mar 16 '16 at 22:10