Skip to content

Hosted PS core in ASP.Net core The system cannot find the file specified #5084

Closed

Description

@Qowy

ASP.NET core 2.0 app.

.csproj:

    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.0-beta.8" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0-beta.8" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.WSMan.Management" Version="6.0.0-beta.8" />

controller code:

    using (PowerShell ps = PowerShell.Create())
            {
                var result = ps.AddScript(@"New-Item -Path 'C:\Programming\' -Type Directory -Name TestFolder").Invoke();
            }

Results in an exeption:

System.Management.Automation.RuntimeException: The following errors occurred when updating the assembly list for the runspace:
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands'. The system cannot find the file specified.
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.PowerShell'. The system cannot find the file specified.
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands'. The system cannot find the file specified.
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.PowerShell.Commands'. The system cannot find the file specified.
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.PowerShell'. The system cannot find the file specified.
Could not load file or assembly 'C:\Programming\Intranet\Intranet\Microsoft.WSMan'. The system cannot find the file specified..

at System.Management.Automation.ExecutionContext.UpdateAssemblyCache()

at System.Management.Automation.Runspaces.RunspaceConfigurationEntryCollection`1.Update(Boolean force)

at System.Management.Automation.Runspaces.RunspaceConfiguration.Bind(ExecutionContext executionContext)

at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)

at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper()

at System.Management.Automation.Runspaces.LocalRunspace.OpenHelper(Boolean syncCall)

at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)

at System.Management.Automation.Runspaces.RunspaceBase.Open()

at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)

at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)

at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)

at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)

at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)

at System.Management.Automation.PowerShell.Invoke()

at Intranet.Controllers.PowershellController.Test() in C:\Programming\Intranet\Intranet\Controllers\PowershellController.cs:line 30

Activity

  1. markekraus commented on Oct 11, 2017

    @markekraus
    Contributor

    I can repro this, but I'm not exactly strong on the hosted app side.

    Dongbo Wang (@daxian-dbw) ?

  2. iSazonov commented on Oct 11, 2017

    @iSazonov
    Collaborator
  3. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Oct 11, 2017
  4. markekraus commented on Oct 11, 2017

    @markekraus
    Contributor

    Ilya (@iSazonov) I can repro this with the sample-dotnet2.0-powershell.beta.3 by just changing beta.3 to beta.8 in the MyApp.csproj. Does not repro with beta.7.

  5. iSazonov commented on Oct 11, 2017

    @iSazonov
    Collaborator

    If it's a regression, it's my only thought that we need a CI test or we should manually test this before we release the new Beta version.

  6. iSazonov commented on Oct 11, 2017

    @iSazonov
    Collaborator

    I remember only one related PR in last time #4196 - Mark Kraus (@markekraus) if you have time you can revert locally the change and test.

  7. added this to the milestone on Oct 11, 2017
  8. daxian-dbw commented on Oct 11, 2017

    @daxian-dbw
    Member

    Mark Kraus (@markekraus) Sorry that I cannot reproduce the issue by following the instructions at https://github.com/PowerShell/PowerShell/tree/master/docs/host-powershell#net-core-sample-application

    Can you please point me what I might be missing?

    This is what I did:

    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:12> cat .\MyApp.csproj
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <AssemblyName>MyApp</AssemblyName>
        <OutputType>Exe</OutputType>
        <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0-beta.8" />
        <PackageReference Include="Microsoft.PowerShell.Commands.Diagnostics" Version="6.0.0-beta.8" />
        <PackageReference Include="Microsoft.WSMan.Management" Version="6.0.0-beta.8" />
      </ItemGroup>
    
    </Project>
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:13>
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:13> dotnet restore
      Restoring packages for E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\MyApp.csproj...
      Generating MSBuild file E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\obj\MyApp.csproj.nuget.g.props.
      Generating MSBuild file E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\obj\MyApp.csproj.nuget.g.targets.
      Restore completed in 1.32 sec for E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\MyApp.csproj.
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:14> dotnet publish -c release -r win10-x64
    Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      MyApp -> E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\bin\release\netcoreapp2.0\win10-x64\MyApp.dll
      MyApp -> E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp\bin\release\netcoreapp2.0\win10-x64\publish\
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:15>
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:15> .\bin\release\netcoreapp2.0\win10-x64\publish\MyApp.exe
    
    Evaluating 'Get-Command Write-Output' in PS Core Runspace
    
    Write-Output
    
    Evaluating '([S.M.A.ActionPreference], [S.M.A.AliasAttribute]).FullName' in PS Core Runspace
    
    System.Management.Automation.ActionPreference
    System.Management.Automation.AliasAttribute
    [E:\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3\MyApp]
    PS:16>
  9. markekraus commented on Oct 11, 2017

    @markekraus
    Contributor

    Dongbo Wang (@daxian-dbw) That's exactly what I did. The difference may be that I have never don't the hosted app before so I was downloading the sdk nuget for the first time. did you test this in a fresh environment?

  10. daxian-dbw commented on Oct 11, 2017

    @daxian-dbw
    Member

    I think I can repro it now. MyApp.exe from the publish folder works fine (i.e. .\bin\release\netcoreapp2.0\win10-x64\publish\MyApp.exe), but the one from win10-x64 is having problem (i.e. .\bin\release\netcoreapp2.0\win10-x64\MyApp.exe).
    I'm looking into it.

  11. markekraus commented on Oct 11, 2017

    @markekraus
    Contributor

    Dongbo Wang (@daxian-dbw) fails with dotnet run too.

  12. Qowy commented on Oct 11, 2017

    @Qowy
    Author

    Thank you all for your efforts in fixing this. I just wanted to repeat I did not use a .Net core project (although we see it has been reproduced there), but a ASP.NET core one instead.

    Just in case somebody also wants to try and reproduce it there (or check a potential fix).

  13. daxian-dbw commented on Oct 11, 2017

    @daxian-dbw
    Member

    Yeah, dotnet run is essentially running the exe from win10-x64 folder.

  14. markekraus commented on Oct 11, 2017

    @markekraus
    Contributor

    Qowy I originally reproduced it in an fresh MVC project. It has the same issues as the more basic MyApp. We are using the MyApp as a baseline to rule out project specific issues. In theory, if this is fixed for the MyApp, it should also work for ASP.NET Core apps.

    Dongbo Wang (@daxian-dbw) thanks!!

  15. daxian-dbw commented on Oct 12, 2017

    @daxian-dbw
    Member

    Just confirmed that Ilya (@iSazonov) was right, this is a regression caused by #4196 -- the passed in filename is just a filename without extension, so Assembly.LoadFrom fails.
    This regression could be fixed by #4942 because that PR will change powershell.Invoke() to not depend on RunspaceConfiguration. However, I think we need to revisit the change of #4196 to make it more reliable.

    /cc Steve Lee (@SteveL-MSFT)

  16. iSazonov commented on Oct 12, 2017

    @iSazonov
    Collaborator

    I've seen the same long ago for powershell.exe - from the publish folder it works fine but from win7-x64 it is having problem (no with Beta.8). Also locally I can not get tests with Add-Type passed - ref error - path not found. I guess it can related.

  17. SteveL-MSFT commented on Oct 12, 2017

    @SteveL-MSFT
    Member

    I reopened #3325

  18. daxian-dbw commented on Oct 12, 2017

    @daxian-dbw
    Member

    Ilya (@iSazonov) Prior to adopting .NET Core 2.0, PowerShell needs to spin up a separate AssemblyLoadContext to handle assembly loading by itself, and the assembly probing path it uses is the base folder of S.M.A.dll. So at that time, it's by design that the hosting application needs to be built with dotnet publish and run from the publish folder, where all dependent assemblies are placed.

    After adopting .NET Core 2.0, powershell uses the default CLR loader and there is no need for a separate load context. So it's OK to run the hosting application from outside the publish folder, or with dotnet run.

    As for Add-Type, we can possibly solve the problem if we are able to package the ref folder into the nuget package as content files.

  19. iSazonov commented on Oct 12, 2017

    @iSazonov
    Collaborator
  20. chunqingchen commented on Nov 3, 2017

    @chunqingchen
    Contributor

    I have verified that this bug is not related to #4196 and therefore not fixed by #4942 #4196.
    Although the behavior is very similar, but regardless the change of #4196, it repros anyway. I can take a look.

  21. chunqingchen commented on Nov 4, 2017

    @chunqingchen
    Contributor

    After talked with dongbo and verified the issue with further steps, the fix actually works.

    The keypoint is building nuget package with the code change as local package and reference to the local package instead of going online (of course, the code change won't affect the online package, beta.8 in this case).

    Here is a repro step:

    1. Generate the nuget package locally with the code change:
      $VersionSuffix = ("v6.0.0-beta.9" -split '-')[-1]
      Publish-NuGetFeed -VersionSuffix $VersionSuffix

    2. edit the nuget config file include the local package:
      go to ..\PowerShell\docs\host-powershell\sample-dotnet2.0-powershell.beta.3
      add following config to NuGet.config

      where C:\Github\powershellfull\p2\PowerShell\nuget-artifacts is folder where previous neget package generates.

    3. go to ..\sample-dotnet2.0-powershell.beta.3\MyApp
      dotnet restore
      dotnet run

    The exception is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions