NullReferenceException at build time with latest Portable.MvvmLightLibs package

Oct 26, 2013 at 11:41 PM
Edited Oct 26, 2013 at 11:41 PM
Hi gang,

When I updated my Windows Store project (Halfwit) to the latest Portable.MvvmLightLibs, I was suddenly unable to build. Trying to compile the project would yield an error in the "Errors" windows which simply said "Object reference not set to an instance of an object" with no other details.

I turned on diagnostic logging for MSBuild, and found this in the output:

1> Dependency "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes". (TaskId:9)
1> Could not resolve this reference. Could not locate the assembly "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. (TaskId:9)

.... and at the bottom of the error section:

1> Required by "GalaSoft.MvvmLight". (TaskId:9)

I did some Googling and eventually found this:

https://connect.microsoft.com/VisualStudio/feedback/details/779370/vs2012-incorrectly-resolves-mscorlib-version-when-referencing-pcl-assembly

It seems like a known bug with some PCL packages.

I ended up removing the reference to the latest version and using the Package Manager Console to install version 4.1.27.6 manually. Now everything builds just fine.

Let me know if I can be of any extra help in tracking down this issue!

Cheers,
Matt
Oct 27, 2013 at 11:57 AM
Edited Oct 27, 2013 at 4:57 PM
Hi Matt,

Do you have the Microsoft.Bcl and Microsoft.Bcl.Build packages referenced by all of your projects, including the main app? What version of VS and Store (8 or 8.1)?

What platforms does your ViewModel PCL target?

I tried a simple repro with both VS 2012 and VS 2013, starting with a Store 8 and 8.1 app, along with a PCL VM, starting 4.1.27.6 and upgrading to 4.2.30.1. Worked for me in both cases, so we'll need to dig deeper to see what's going on here.
Oct 27, 2013 at 7:37 PM
Visual Studio 2012 Update 3 on Windows 8.1. Halfwit is still a Windows 8 app for now.

Matt

Oct 27, 2013 at 7:37 PM
Damn - I really should read all the questions before reply. 😊

My ViewModel PCL project targets:

“.NET for Windows Store apps, .NET Framework 4.5”

Matt

Oct 27, 2013 at 7:43 PM
Hi Matt,

Do you have a small project that reproduces this on your machine that you'd be able to share? Maybe start with a blank app and vm, add the older pcl mvvmlight and then update?

Do you have another machine you can try to repro it on with your project?

If you start a new project with the newest pcl version, does that have issues as well for you or just on an update?

I tried various scenarios on my VS 2012 Update 3 on Win8.1 but did not run into any issues.

Oren
Oct 27, 2013 at 7:46 PM
One last comment relating to this tweet from Daniel Plaisted:

https://twitter.com/dsplaisted/status/394491110447280128

The MS Connect issue I linked to above does not seem to be the problem. The same "mscorlib" reference error appears in the MSBuild log even when the solution builds without any compiler errors (ie with the earlier version of MVVM Light).

Matt
Oct 27, 2013 at 7:51 PM
Ah! This could be it! From the MSBuild log:

2>C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v11.0\Microsoft.Windows.UI.Xaml.Common.targets(263,9): Xaml Internal Error error WMC9999: Object reference not set to an instance of an object.
2>Done executing task "CompileXaml" -- FAILED. (TaskId:62)
2>Done building target "MarkupCompilePass2" in project "Halfwit.csproj" -- FAILED.: (TargetId:104)

Matt
Oct 27, 2013 at 7:56 PM
One other thought - the DLL name - for the Platforms changed, though that shouldn't be an issue for WinRT-based XAML as the namespaces did not change. It used to be Galasoft.MvvmLight.Platform.NetCore45.dll, but now they're all Galasoft.MvvmLight.Platform.dll (just in different folders in the nuget pkg). That'd be more of an issue for SL/WPF where the assm name is in the XML namespace declaration.

Have you tried a rebuild? How about manually deleting the bin/obj directories when VS is closed? I wonder if there's an old reference somewhere.
Oct 27, 2013 at 9:40 PM
onovotny wrote:
One other thought - the DLL name - for the Platforms changed, though that shouldn't be an issue for WinRT-based XAML as the namespaces did not change. It used to be Galasoft.MvvmLight.Platform.NetCore45.dll, but now they're all Galasoft.MvvmLight.Platform.dll (just in different folders in the nuget pkg). That'd be more of an issue for SL/WPF where the assm name is in the XML namespace declaration.

Have you tried a rebuild? How about manually deleting the bin/obj directories when VS is closed? I wonder if there's an old reference somewhere.
I've definitely tried a clean & rebuild (and of course restarting VS). Haven't tried deleting the obj directories. Will give that a go when I'm home again.

This is the only machine I have that's running Windows 8.1 (unless you count my Surface RT :)) so I can't really test this anywhere else. I'll see if I can reproduce it in a new project, too.

The fact that it seems to come back to the XAML compilation step I pasted in above is really confusing. Why would rolling back to an early MVVM Light version fix it? Bizarre.

Matt
Oct 28, 2013 at 6:39 AM
Ok, an update.

I deleted the obj\ folder under my WinRT app, then updated to the latest Portable.MvvmLightLibs in all projects in my solution.

Rebuilt and still got an "Object reference not set to an instance of an object" error, BUT this time there was a file and line number alongside the error! Here it is:

Error 30 Object reference not set to an instance of an object. C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v11.0\Microsoft.Windows.UI.Xaml.Common.targets 263 9 Halfwit

The line in question:
<CompileXaml
      LanguageSourceExtension="$(DefaultLanguageSourceExtension)"
      Language="$(Language)"
      RootNamespace="$(RootNamespace)"
      XamlPages="@(Page)"
      XamlApplications="@(ApplicationDefinition)"
      PriIndexName="$(PriIndexName)"
      ProjectName="$(MsBuildProjectName)"
      IsPass1="False"
      CodeGenerationControlFlags="$(XamlCodeGenerationControlFlags)"
      ClIncludeFiles="@(ClInclude)"
      LocalAssembly="$(LocalAssembly)"
      ProjectPath="$(MSBuildProjectFullPath)"
      OutputPath="$(XamlGeneratedOutputPath)"
      OutputType="$(OutputType)"
      TargetPlatformWinMDLocation="$(TargetPlatformWinMDLocation)"
      ReferenceAssemblyPaths="@(ReferenceAssemblyPaths)"
      ReferenceAssemblies ="@(XamlReferencesToCompile)"
      ShareAppDomains="$(XamlShareAppDomains)"
      ForceSharedAppDomainShutdown="False"
      CompileMode="RealBuildPass2"
      XAMLFingerprint="$(XAMLFingerprint)"
      FingerprintIgnorePaths="$(XAMLFingerprintIgnorePaths)"
      VCInstallDir="$(VCInstallDir_110)"
      >
  <Output Condition=" '$(ManagedAssembly)'!='false' " ItemName="Compile"   TaskParameter="GeneratedCodeFiles" />
  <Output Condition=" '$(ManagedAssembly)'=='false' " ItemName="XamlGFiles" TaskParameter="GeneratedCodeFiles" />

  <Output ItemName="FileWrites" TaskParameter="GeneratedCodeFiles" />
  <Output ItemName="FileWrites" TaskParameter="GeneratedXamlFiles" />
  <Output ItemName="_GeneratedCodeFiles" TaskParameter="GeneratedCodeFiles" />
  <Output ItemName="_GeneratedXamlFiles" TaskParameter="GeneratedXamlFiles" />
</CompileXaml>

So I then dropped into the Package Manager Console, and typed:

uninstall-package portable.mvvmlightlibs
install-package Portable.MvvmLightLibs -version 4.1.27.6

... and now everything builds again.

It's starting to look like some sort of incompatibility between the latest MVVM Light package and the WIndows.UI.Interactivity package it depends on. Maybe it's something to do with running VS2012 on Windows 8.1.

Any ideas?
Oct 28, 2013 at 1:09 PM
Edited Oct 28, 2013 at 1:10 PM
That's really weird...

The Windows.UI.Interactivity version has not changed since the last release, so that should be the same as well.

As a note, because Behaviors are part of VS 2013/Win 8.1, EventToCommand (and that interactivity reference) aren't in the Win8.1 version. The recommended approach would be to use the built-in one.
Oct 28, 2013 at 6:14 PM
I'd really like to see a repro project for this, if you can create one.

Also, if you can get the stack trace for the NullReferenceException that would be helpful.

Thanks,
Daniel
Oct 28, 2013 at 7:32 PM
There's no stack trace - just this line in the MSBuild log:

2>C:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v11.0\Microsoft.Windows.UI.Xaml.Common.targets(263,9): Xaml Internal Error error WMC9999: Object reference not set to an instance of an object.

I'd be happy to give you guys read access to the Bitbucket repository so you can try it on your machines. If you don't see an error then I'll know it's something local to this PC. Let me know your Bitbucket usernames.

Matt

ps. Oren - I'm not actually using anything in Windows.UI.Interactivity like EventToComment etc. The only parts of MVVM Light that I'm using are ViewModelBase, Messenger and RelayCommand. Just the core stuff.
Oct 29, 2013 at 3:25 PM
Hi Matt,

My Bitbucket username is onovotny. I can take a look to see if it repros for me, but even if it does, this may be the kind of thing that Daniel's team needs to address.
Oct 29, 2013 at 7:43 PM
Added! Let me know if there’s anything else I need to commit - I’m still a bit of a novice at this whole “ignore packages but not the config files” thing with Mercurial and NuGet. 😊


Matt

Oct 29, 2013 at 7:45 PM
My bitbucket username is dplaisted.

Thanks,
Daniel
Oct 29, 2013 at 7:46 PM
Ok, added you too Daniel.

Oct 29, 2013 at 7:53 PM
Hi Matt,

Looking at what's checked in currently, I see the solution using two different Portable.MvvmLightLibs versions. The .Desktop lib is using 4.2.30.1 and the Halfwit and .ViewModels are on 4.1.27.6.

In that config it does build for me, though I'm sure it won't work right due to conflicting dll versions. What config works for you and what's failing?
Oct 29, 2013 at 10:04 PM
onovotny wrote:
Hi Matt,

Looking at what's checked in currently, I see the solution using two different Portable.MvvmLightLibs versions. The .Desktop lib is using 4.2.30.1 and the Halfwit and .ViewModels are on 4.1.27.6.

In that config it does build for me, though I'm sure it won't work right due to conflicting dll versions. What config works for you and what's failing?
Oops - tried replying from my phone but it looks like it didn't get through.

You can ignore the Desktop project, Oren. I was messing about going up and down in MVVM Light versions to diagnose the problem and must've forgotten to roll that particular project back before I pushed to Bitbucket. The Desktop project is a false start that I've left in the solution in case I ever want to revisit it.

Ignoring the desktop app for the moment: At this stage the only combination that works is 4.1.27.6 in the ViewModels project and the same (4.1.27.6) in the WinRT app. Mixed versions obviously lead to a runtime error, and if I update both projects to the latest MVVM Light then I get a compile time NullReferenceException.

Cheers,
Matt
Oct 29, 2013 at 11:55 PM
Okay, I can confirm that I can repro the issue, but I'm not sure why it's happening. Daniel?
Oct 30, 2013 at 12:25 AM
That’s a relief! I was thinking it’d be something local to me and I’d have to pave my PC! Smile

Matt
Feb 17, 2014 at 7:33 AM
Any news for this? I'm seeing this on both Windows 8.0, VS 2012 and in Windows 8.1, VS 2013 when trying to build a Windows 8.0 application and using MVVM light 4.2.30.1.
Feb 17, 2014 at 12:54 PM
Edited Feb 17, 2014 at 2:31 PM
Do you have any sample data (in xaml file) that you are compiling ? Just figure out that was the problem for me.