Bug 40327 - "The type initializer for 'NuGet.EnvironmentUtility' threw an exception" in the Package Manager Console in VS 2013 if the "GettingStarted.Xamarin" file from a Xamarin.Forms portable library project is already open when VS loads the solution
Summary: "The type initializer for 'NuGet.EnvironmentUtility' threw an exception" in t...
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Visual Studio Extensions
Classification: Xamarin
Component: General ()
Version: 4.0.0 (C6)
Hardware: PC Windows
: Normal normal
Target Milestone: Future Cycle
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-04-13 05:45 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2017-07-11 16:01 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case (5.63 MB, application/zip)
2016-04-13 05:45 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details
Log files and version info (21.70 KB, application/zip)
2016-04-13 05:46 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details
Log file for additional errors in Comment 3 (19.76 KB, application/zip)
2016-04-13 06:00 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description Brendan Zagaeski (Xamarin Team, assistant) 2016-04-13 05:45:11 UTC
Created attachment 15688 [details]
Test case

"The type initializer for 'NuGet.EnvironmentUtility' threw an exception" in the Package Manager Console in VS 2013 if the "GettingStarted.Xamarin" file from a Xamarin.Forms portable library project is already open when VS loads the solution

In my tests I was _not_ able to replicate the problem in Visual Studio 2015 Update 1. That suggests to me that the root cause of the problem might in fact be an upstream bug in Visual Studio 2013 itself or the NuGet extension. But if there is a way to avoid the problem by changing something on the Xamarin side, it could still be nice to do that.




## Workaround

Close the "GettingStarted.Xamarin" tab and then quit and reopen Visual Studio.

(If you wanted, you could of course also delete the "GettingStarted.Xamarin" file from the project to prevent the problem from accidentally happening again in the future.)




## Regression status: not a recent regression in either XamarinVS or Xamarin.Forms


### XamarinVS versions tested against Xamarin.Forms 1.4.4.6449

BAD: XamarinVS 4.1.0.111   (a78562a) "Cycle 7 Preview, Alpha 2"
BAD: XamarinVS 4.0.3.214   (0dd817c) "Cycle 6 – Service Release 3"
BAD: XamarinVS 4.0.1.147   (1cc3a99)
BAD: XamarinVS 3.11.1594.0 (a06d6d5) "Cycle 5 – Service Release 5"


### Xamarin.Forms versions tested against XamarinVS 4.0.3.214

BAD: XF 2.2.0.5-pre2
BAD: XF 2.1.0.6529
BAD: XF 1.4.4.6449
BAD: XF 1.4.3.6376




## Replicable on more than one unique Windows system?

Yes, I was able to replicate the problem on a Windows 10 system as well as a separate Windows 8.1 system.




## Steps to replicate

1. Open Visual Studio 2013.

2. If the "Package Manager Console" window is currently open, close it.

3. Quit Visual Studio.

4. Open the attached test case in Visual Studio 2013. [1]

5. The "GettingStarted.Xamarin" tab should be open. If it isn't, double-click the "GettingStarted.Xamarin" file from the Solution Explorer to open it, and then quit Visual Studio and reopen the project.

6. Open "Tools > NuGet Package Manager > Package Manager Console".

7. Enter the following command in the Package Manager Console and then press return:

Install-Package Xamarin.Forms -Version 1.4.3.6376



[1] If you prefer, you could instead create a new template "Visual C# > Cross-Platform > Blank App (Xamarin.Forms Portable)" solution, remove all the projects except for the portable library project, and then quit Visual Studio and reopen that solution.




## "Bad" Results

The "Install-Package" command fails:

> Install-Package : The type initializer for 'NuGet.EnvironmentUtility' threw an exception.
> At line:1 char:1
> + Install-Package Xamarin.Forms -Version 1.4.3.6376
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : NotSpecified: (:) [Install-Package], TypeInitializationException
>     + FullyQualifiedErrorId : System.TypeInitializationException,NuGet.PowerShell.Commands.InstallPackageCommand


### Corresponding exception from the `devenv /log` log file

> The type initializer for 'NuGet.EnvironmentUtility' threw an exception.
>     at NuGet.EnvironmentUtility.get_RunningFromCommandLine()
>     at NuGet.DataServicePackageRepository..ctor(IHttpClient client, PackageDownloader packageDownloader)
>     at NuGet.PackageRepositoryFactory.CreateRepository(String packageSource)
>     at NuGet.VisualStudio.CachedRepositoryFactory.GetPackageRepository(String source)
>     at NuGet.VisualStudio.CachedRepositoryFactory.CreateRepository(String source)
>     at NuGet.VisualStudio.VsPackageSourceRepository.GetActiveRepository()
>     at NuGet.VisualStudio.VsPackageManagerFactory.IsAggregateRepository(IPackageRepository repository)
>     at NuGet.VisualStudio.VsPackageManagerFactory.CreateFallbackRepository(IPackageRepository primaryRepository)
>     at NuGet.VisualStudio.VsPackageManagerFactory.CreatePackageManager(IPackageRepository repository, Boolean useFallbackForDependencies)
>     at NuGet.VisualStudio.VsPackageManagerFactory.CreatePackageManager()
>     at NuGet.VisualStudio.PackageRestoreManager.CheckForMissingPackagesCore()




## "Good" Results (for example when using the workaround)

The "Install-Package" command succeeds without error.




## Additional version info (brief)

Windows 10 (64-bit)

Visual Studio 2013 Update 5
NuGet Package Manager 2.8.60723.765

(I also tested with NuGet Package Manager 2.8.60610.756, and the results were the same.)
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2016-04-13 05:46:19 UTC
Created attachment 15689 [details]
Log files and version info
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2016-04-13 05:50:26 UTC
A few extra details:




## Steps I followed to create the test case

1. Create a new "Visual C# > Class Library (Portable)" named "PortableClassLibrary1" that targets:

- .NET Framework 4.5
- Windows 8
- Windows Phone Silverlight 8
- Xamarin.Android
- Xamarin.iOS
- Xamarin.iOS (Classic)

2. Install the "Xamarin.Forms" NuGet package into the project.

3. Add the "GettingStarted.Xamarin" file from an existing Xamarin.Forms portable project into the new project.

4. Double-click the "GettingStarted.Xamarin" file in the Solution Explorer to open it.

5. Close and save the solution.




## Alternate workaround

Ensure that the NuGet package manager gets initialized before loading any Xamarin projects. In particular, the following steps allowed me to use the Package Manager Console successfully after the initial failure:

1. After you hit the error (at step 7 of the "Steps to replicate"), quit Visual Studio.

2. Open Visual Studio _without_ loading any Xamarin projects.

3. Open the Package Manager Console from "Tools > NuGet Package Manager > Package Manager Console" to allow it to initialize.

4. _Now_ reopen the test case and again try to run the Install-Package command in the Package Manager Console:

Install-Package Xamarin.Forms -Version 1.4.3.6376
Comment 3 Brendan Zagaeski (Xamarin Team, assistant) 2016-04-13 06:00:28 UTC
Created attachment 15690 [details]
Log file for additional errors in Comment 3

To help make this bug more searchable, I will note that I can hit additional error messages for this same problem if I try one of the following:

A. Right-click the project in the Solution Explorer and select "Manage NuGet Packages".

or

B. Open "Tools > Extensions and Updates".




## Results



### Dialog message on the first failed attempt of either (A) or (B)

> Operation failed
> 
> Set property 'System.Windows.ResourceDictionary.DeferrableContent'
> threw an exception.



### Dialog message on the second failed attempt of (A)

> Operation failed
> 
> Object reference not set to an instance of an object.



### Dialog message on the second failed attempt of (B)

> Exception has been thrown by the target of an invocation.



### Top of the call stack for the "Set property" error (from the `devenv /log` log file)

> Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.
>     at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
>     at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
>     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)

### Bottom of the call stack for the "Set property" error

>     at NuGet.Dialog.PackageManagerWindow..ctor(Project project, String dialogParameters)
>     at NuGet.Tools.NuGetPackage.GetVS12PackageManagerWindow(Project project, String parameterString)
>     at NuGet.Tools.NuGetPackage.ShowManageLibraryPackageDialog(Project project, String parameterString)



### Top of the call stack for the "Object reference" error (from the `devenv /log` log file)

> Object reference not set to an instance of an object.
>     at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
>     at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
>     at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

### Bottom of the call stack for the "Object reference" error
 
>     at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
>     at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
>     at Microsoft.VisualStudio.ExtensionsExplorer.UI.VSExtensionsExplorerCtl.InitializeComponent()
>     at Microsoft.VisualStudio.ExtensionsExplorer.UI.VSExtensionsExplorerCtl..ctor()