4

After upgrading from building our product in Visual Studio 2010 and .NET 4.0 to Visual Studio 2015 and .NET 4.5.2 we have seen issues running the product on a customers machine.

The error we see is a System.MissingMethodException being thrown when starting the application, which from looking at posts on here point to an incorrect version of .NET being installed on the machine. Now the customers machine does have .NET 4.5.2 installed as this is installed during the installation of our product.

The way we have currently fixed the issue is to install .NET 4.6 on the customers machine.

I have a theory that I ideally want to confirm (well preferably rubbish and replace with something more viable):

  • All of our projects are set to compile against .NET 4.5.2
  • The build agent that compiles the code and generates the installer has Visual Studio 2015 and with it .NET 4.6 but it does have the necessary .NET 4.5.2 framework installed

I believe that because .NET 4.6 is installed the machine it is overriding the .NET 4.5.2. I don't know why this would be happening but I have seen something vaguely similar where a .NET 4.0 application running on a machine with .NET 4.5 installed behaves how a .NET 4.5 application would here (if it is I am pretty sure it shouldn't be). I was hoping someone might be able to point out something that I am missing.

I do hope that my theory is wrong.

Additional Information

I have managed to attach a debugger when the application crashes and the error is:

Method not found: '!!0[] System.Array.Empty()'.

There is nothing in my code that uses Array.Empty().

StackTrace:

at MyApp.DisplayExceptionInfo(Exception ex)
at MyApp.Main(String[] args) in E:\Build\MyApp\App.xaml.cs:line 82

DisplayExceptionInfo simply tries to display an Exception which makes me believe that an Exception is being thrown earlier.
Main checks to see if there is another instance running through some Interop (which I also tried removing but it did not resolve the crash) and then creates my App class and runs it.

Update

Sorry I don't believe I gave enough details in my question to help get to the bottom of the issue. It turns out that TeamCity uses MSBuild to build Visual Studio solutions and during the build there is the following warning (annoyingly rather well buried in the log):

[GetReferenceAssemblyPaths] C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1097, 5): warning MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.5.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

I can confirm that I do have the .NET 4.5.2 Multi-Targeting Pack installed but I will continue to investigate

Pradeep
  • 3,258
  • 1
  • 23
  • 36
Bijington
  • 3,661
  • 5
  • 35
  • 52
  • It does not have anything to do with the *installed* .NET version, only the reference assemblies matter. Note how you don't have 4.5.2 installed either. Forever a detail that gets fumbled badly on build machines, referencing the assemblies in c:\windows\microsoft.net instead of c:\program files (x86)\reference assemblies is a very common mistake. Not documenting the name of the missing method and a snippet of the code that uses it makes it pretty hard to help you. – Hans Passant Jan 27 '16 at 13:14
  • @HansPassant thanks for the help. I can confirm that I have all references to Microsoft dlls with no path specified (i.e. I added the references through the Assemblies tab in Add References) and I was under the impression that these would use c:\program files (x86)\reference as you mention. The Event Log entry does not mention a specific method name all it contains is a limited StackTrace with 2 methods from my code. I have added in the information I have from the Event Log entry – Bijington Jan 27 '16 at 14:21
  • Array.Empty() is indeed an added method in 4.6. We can't see how it got there without the stack trace that your debugger provided. – Hans Passant Jan 28 '16 at 11:40
  • @HansPassant sorry I did add it in but removed it again after I didn't think it helped. I found that another library that had been updated was build against 4.6 however I have reverted this and we are still seeing the issue. – Bijington Jan 28 '16 at 11:43
  • You are right, that stack trace is not helpful. It needs to be the one that is captured before your code caught the exception. Like ex.StackTrace or the one you get in the debugger when you tick the Thrown checkbox and turn off Just My Code. If your DisplayExceptionInfo() method does not show it then you want to improve it, ex.ToString() is always helpful. – Hans Passant Jan 28 '16 at 11:48

2 Answers2

10

After a day of investigations it would appear that MSBuild is building using the .NET 4.6 framework because it cannot find the .NET 4.5.2 Framework installed (although the Multi-Targeting pack is installed).

Thanks to other posts on here there are 2 possible solutions both of which have worked:

  1. Provide a FrameworkPathOverride to the MSBuild as an argument.
    -p:FrameworkPathOverride="C:\Program Files (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5.2"
  2. Install the .NET 4.5.2 SDK.

I went with option 2 in the end as although it seems Visual Studio does use option one when building it seems wrong to have to specify the path.

Community
  • 1
  • 1
Bijington
  • 3,661
  • 5
  • 35
  • 52
2

I know this is an old post, but for me, I had to install the MsBuild 2015 update to get it to work :

https://github.com/Microsoft/msbuild/issues/173#issuecomment-192310586

JayR
  • 127
  • 1
  • 4