The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception
Asked Answered
S

3

12

I was working on a simple record management web app in ASP.NET MVC. The build was successful. However, when I go to Package Manager Console, I always get this error.

The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception.

Can somebody please explain this error to me? I get it all the time and it bothers me in development. Thanks in advance.

Syncytium answered 21/12, 2015 at 3:5 Comment(3)
You could try and check what is suggested here, which might be related. Otherwise make Visual Studio create an ActivityLog, which usually contains more technical information about the error, and post the essence here.Overarch
I had the same problem, I restarted Visual Studio and it was gone. Am using Visual Studio Community 2013.Carotid
Same problem on VS 2022 also. Restarting IDE worked for me.Conchaconchie
A
6

The following information was gathered from this Visual Studio NuGet Console bug report ticket thread: https://github.com/NuGet/Home/issues/1638

Cause

Per this comment from Yishai Galatzer (Microsoft), the problem is caused by a stack overflow bug in a PowerShell DLL that's part of System.Management.Automation 3.0. The updated DLL with the bug evidently was distributed as part of Windows Updates sometime in the past few weeks.

Also per another comment by Yishai Galatzer, the PowerShell team will be releasing a fix for this, hopefully later this month (January 2016).

Workaround / Temporary Solution

"pete1208" posted the following workaround earlier in that same thread:

In your Visual Studio folder, make a backup copy of file devenv.exe.config.

Then, in the original devenv.exe.config file, insert the following just after the opening assemblyBinding element:

<!-- WORKAROUND START for NuGet Console error: 
  "The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception" 
  Author: pete1208 from https://github.com/NuGet/Home/issues/1638 -->
<dependentAssembly>
        <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
        <publisherPolicy apply="no" />
      </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Utility" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.ConsoleHost" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Management" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Security" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.PowerShell.Commands.Diagnostics" publicKeyToken="31bf3856ad364e35" />
      <publisherPolicy apply="no" />
    </dependentAssembly>
<!-- WORKAROUND END -->

(You'll probably need an admin-elevated version of your favorite text editor to save the file to your Visual Studio folder.)

Then, restart Visual Studio.

Again per Yishai Galatzer, the effect of this workaround is to use binding redirects to force loading PowerShell 1.0.

I tried the workaround, and it did work for me. My Package Manager Console in my Visual Studio 2013 is once again functional.

I'm planning to keep an eye on that NuGet bug thread, and once the announcement is made that the Windows Update with the System.Management.Automation fix is live, I'm going to back out the workaround in my devenv.exe.config file.

Update

The fix was released in the January 12, 2016 Windows Update, per Yishai Galatzer (Microsoft).

Adelladella answered 11/1, 2016 at 21:36 Comment(0)
P
0

I also faced same issue when I opened my project Project.csproj in Visual Studio.

Later I opened my project solution Project.sln in visual studio and then open Tools->NuGet Package Manager-> Package Manager Console.

This solved the issue for me.

Pantelleria answered 29/2, 2024 at 18:25 Comment(0)
P
0

I was facing the same issue in 2024 "The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception." I solved it as follows:

  • I checked my whole code but nothing found.
  • Then I rebuilt my project and closed it.
  • Again with PrjectName.Sln file
  • Now Open the Tools-> nuget Package Console.
  • Then it works for me.
Pimp answered 28/7, 2024 at 9:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.