Visual Studio 2022 Hot Reload

I have a few issues with the tutorial and I think I have them due to lack of experience with Visual Studio 2022 (I usually use VS Code). I hope you might find the time to help me in clarifying these issues.

1: Adding a reference to the Console project: It is only possible to add one of there: .exe, .dll, .tlb, .olb, .ocx or .winmd. It seems you reference a csproj file. What am I doing wrong here?

2: Browsing for the dll in the debug or release folders brings nothing before the ClassLib project has been build. This is kind of related to 1. If I’m supposed to reference the csproj file, that makes perfect sense, but after building and referencing the dll, I get a different reference in my csproj file the the one in the guide.

3: Building the ClassLib project and referencing the dll, makes the following line from the console application code point to a file that does not exist: powerShell.AddCommand(“Import-Module”).AddParameter(“Name”, Path.Combine(Environment.CurrentDirectory, “ClassLibrary1.dll”)); I suppose the csproj file should do some kind of magic when building the project and place the dll files in the same directory, but that does not seem to happen. Adding the include reference for the ClassLibrary1cs.proj file in the Console project does not seem to be enough to build a folder with both dlls. What am I missing here?

I have two projects. First is a ConsoleApp1.csproj.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
  </ItemGroup>

</Project>

The second is the ClassLibrary1.csproj.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
  </ItemGroup>

</Project>

My solution looks like this.

image

The Class1.cs has the cmdlet in it.

using System.Management.Automation;

namespace ClassLibrary1
{
    [Cmdlet("Write", "Hello")]
    public class WriteHelloCommand : PSCmdlet
    {
        [Parameter(Position=0)]
        public string Name { get; set; }

        protected override void ProcessRecord()
        {
            WriteObject(Name);
        }
    }
}

The Program.cs calls the cmdlet. using System.Management.Automation;

do
{
    var commandLine = Console.ReadLine();
    if (commandLine == "exit")
    {
        break;
    }

    using var powerShell = PowerShell.Create();
    powerShell.AddCommand("Import-Module").AddParameter("Name", Path.Combine(Environment.CurrentDirectory, "ClassLibrary1.dll"));
    powerShell.AddStatement().AddScript(commandLine);
    powerShell.AddCommand("Out-String");

    try
    {
        var item = powerShell.Invoke<string>().FirstOrDefault();
        Console.WriteLine(item);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

} while (true);

Thanks, Adam, and sorry for the late reply. Originally I had no solution file, since I started the tutorial from command line.

Starting over again from within Visual Studio 2022 2 times and still not able to build:

It seems to be an issue with loading a DLL:

The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program '[26872] ConsoleApp1.exe' has exited with code 3221225794 (0xc0000142) 'DLL Initialization Failed'.
The program '[26872] ConsoleApp1.exe: Program Trace' has exited with code 0 (0x0).

This seems like something could be missing on your machine. I found some posts on SO about a similar issue: c# - The target process exited without raising CoreCLR started event error with .NET Core 2.2 - Stack Overflow

1 Like

Thanks Adam! That did it.

I added ASP.NET as described in your link - I had everything else.

You rock!

2 Likes