Powershell: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

Created on 1 Oct 2018  路  29Comments  路  Source: PowerShell/PowerShell

Hi,
I am using Powershell SDK to execute powershell commands from an ASP.net core 2.1 application. It works in in dev (when i run it from visual studio). But when i deploy to IIS in production i keep getting:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)
at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelperTInput,TOutput
at System.Management.Automation.PowerShell.CoreInvokeTInput,TOutput
at System.Management.Automation.PowerShell.CoreInvokeTOutput
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)

It works if I call Invoke() from a Console application. Any Idea why?

Area-Maintainers-Build Issue-Question Resolution-Answered Resolution-Duplicate

Most helpful comment

I have the same Problem, if start the Program in the Visual Studio it works fine, but if i host the Program as Windows Service it fires the same Exception. PLease help me

The Problem is the same after installing the Packege manualy

All 29 comments

That sounds like a bug to me in the definition of the SDK NuGet package, the SDK Nuget package seems to no declare the (apparently) required dependency to the Microsoft.Management.Infrastructure NuGet package as it can be seen below in the dependencies of the SDK Nuget package. You can manually add the Microsoft.Management.Infrastructure NuGet package, which should resolve your problem.

image

It seems that the problem i is Hangfire. When I start the code from the main tread it works as intended.

I have the same Problem, if start the Program in the Visual Studio it works fine, but if i host the Program as Windows Service it fires the same Exception. PLease help me

The Problem is the same after installing the Packege manualy

Perhaps #7886 helps.

Thank you #7886 worked. I changed my Publish Profile to win10-x64 and it worked fine.

Thank you #7886 worked!

@c01danjo can you explain what you did exactly, if you can still recall?

@c01danjo can you explain what you did exactly, if you can still recall?

I changed the property "RuntimeIdentifier" to "win10-x64" in the publishing configuration. /Properties/{your publishing configuration}.pubxml.

Thanks. I don't seem to have any .pubxml files in my project. Perhaps it needs to be created?

Hello Guys,

I am having issues when I try this on a Windows Server 2019.
I also tried it using Windows 10 and a Windows Server 2012R2 and it worked fine.
I tried all the methods i found in this issue and in the issue #7886.
What else can I try to make it work on Windows Server 2019?

Thanks in advance.

I'm having this issue after publishing on the client side using IIS6 to host the web application (asp.net core), I tried everything mentioned above and in other threads but no luck, does anyone have any suggestions? please... (also my publish profile only works with win-x86)

What is SDK version you use?

I use the newest ASP.Net Core SDK 3.1.201.

@iSazonov the powershell sdk 6.2 its the one that have microsoft.managemnet.infrastructue 1.0.0, if I upgrade to the latest it switches microsoft.management to 2.0.0.
@Toubster I'm using the latest core 3.1.

@ZedTR Are you using the Powershell SDK on a Server 2019? If yes, which version have you Installed from the components on the Server and from the Nuget packets?

My components versions:
grafik

My Nuget packets:
grafik

I'm using it on server 2016, I have PowerShell.sdk, Automation and infrastructure downloaded not the diagnostics or the run time installed. not sure about the components on the sever though! can you please tell me how can I get to see what is installed on my server?

Get-Package -ProviderName NuGet

Please use latest PowerShell SDK version.
I guess you have to add the MMI dll manually into your app publish folder.

@iSazonov I'm using the latest sdk 7.0.0. I also made sure that I have the management package in the application folder but still no luck. I started to suspect that I might need something called Microsoft Management Framework updated or installed on the server.

What is targeting in your project?

.NET core 3.1

The version has a dll loading tracing. You could use the feature for troubleshooting. I don't know whether this works in IIS.

I also made sure that I have the management package in the application folder but still no luck.

Do you mean that you see mmi dll in your application folder?

Can you share the exception with stack trace?

I to am running into this issue on Windows Server 2019 with .NET Core 3.1. Except I am running it from a stand alone service. Here is the exception I get:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' at System.Management.Automation.Runspaces.AsyncResult.EndInvoke() at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown

I find it interesting that is looking for the 1.0.0.0 version while I have the 2.0.0.0 mmi in my project. The build will not add the mmi.dll into the application folder and manually adding it will not fix the issue. I assume its a versioning problem. How does it determine which version of mmi to load?

As a side note this worked as recently as a week ago. I think the problem started after either a recent Visual Studio 2019 or a powershell SDK (????) update.

I am invoking PoweShell script from .Net Core Console application when I debug the code the PoweShell command is working but when I run the published EXE I am getting below error:
There is exceptionSystem.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at System.Reflection.Assembly.get_ExportedTypes()
at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)
at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelperTInput,TOutput
at System.Management.Automation.PowerShell.CoreInvokeTInput,TOutput
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke()
at EncryptionExe.Program.Main(String[] args).

Is there any solution for this?

@DivyashriVR Please use Search to find workarounds in the repo. If you use SDK you should "manually" copy the dll to publish directory.

@DivyashriVR Please use Search to find workarounds in the repo. If you use SDK you should "manually" copy the dll to publish directory.

Hi @iSazonov , I am getting the mentioned error when I run the the trimmed self-contained executable and here I have only 2 files getting generated as in below screenshot:
trimmed self-contained single executable.

I have created this self-contained executable with the purpose that I don't want any DLL's to be included in it.

So Is there a way to resolve this issue w.r.t self-contained executable.

Thanks

@DivyashriVR I think you need to put MMI.dll near the exe and look a result.

Hi @iSazonov ,I don't want any dll to be included in my trimmed self-executable.So Is there any way if we can resolve this without adding any dll's to the executable?

If you put MMI.dll near the exe and this resolve your issue then you can try to modify your csproj so that include the dll in exe. I hope it is possible.

Solution for me taken from #7886 was to run publish command manually with win10-x64. Visual Studio 2019 in the latest update did not included proper libraries into the publish folder next to the exe file while using Publish command on project.

dotnet publish -o .\publish -r win10-x64
Was this page helpful?
0 / 5 - 0 ratings