0

I sometimes remote debug my project in Visual Studio, which is enabled and configured in launchSettings.json.

I have an MSBuild task to automatically copy my project output to the remote machine during build.

However, I currently need to manually enable/disable (since copying takes some time that is wasted if I'm not remote debugging) and configure (with the right remote host name) the MSBuild task, even though this information is already in launchSettings.json.

Is there a way to read values (at least remoteDebugEnabled and remoteDebugMachine) from launchSettings.json as part of an MSBuild task?

Solal Pirelli
  • 1,199
  • 9
  • 21
  • The [standard tasks in MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-task-reference?view=vs-2022) do not include tasks for working with JSON (but probably should). However, others have written tasks. You may want to check out [JsonPeek](https://www.nuget.org/packages/JsonPeek) and [JsonPoke](https://www.nuget.org/packages/JsonPoke). Note that I haven't used these tasks so I have no idea of their quality. – Jonathan Dodds Jul 26 '23 at 00:25
  • [Inline task](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks?view=vs-2022) should be the correct way. – Bowman Zhu-MSFT Jul 26 '23 at 06:58
  • You can take a look of my answer, inline task should also be a way, but seems some limits exist on my machine, I shared another method, which should also be able to read launchsettings.json content during build. :) – Bowman Zhu-MSFT Jul 26 '23 at 08:16

1 Answers1

1

Theoretically, Inline Task should be able to realize your idea, it allow us to write code inside the .csproj file. But when I actually tested it, I couldn’t get the data of the third-party library or even some local libraries during the build.

Inline Task, you can test it on your side, because there is a problem with the VS tool and its environment on my side, maybe it can be realized on your side.

Anyway, I found another way.

1, Create a console app named "ReadLaunchSettings":

using System;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace ReadLaunchSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            var launchSettingsFilePath = args[0];
            var json = File.ReadAllText(launchSettingsFilePath);
            var jdoc = JsonDocument.Parse(json);

            var profiles = jdoc.RootElement.GetProperty("profiles");
            var profile = profiles.EnumerateObject()
                                  .FirstOrDefault(p => p.Value.GetProperty("remoteDebugEnabled").GetBoolean());

            if (profile.Value.ValueKind != JsonValueKind.Undefined)
            {
                Console.WriteLine("true");
                Console.WriteLine(profile.Value.GetProperty("remoteDebugMachine").GetString());
            }
            else
            {
                Console.WriteLine("false");
                Console.WriteLine("null");
            }
        }
    }
}

2, build the above console app to get the execute able application(.exe file)

enter image description here

3, change the content of the .csproj file of the project which has the launchSettings.json file(On my side, it is web application.):

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

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

    <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    </ItemGroup>

    <Target Name="TestBowman" BeforeTargets="PreBuildEvent">
        <Exec Command="C:\Users\Administrator\source\repos\projectsref\ReadLaunchSettings\bin\Debug\net6.0\ReadLaunchSettings.exe $(ProjectDir)Properties\launchSettings.json" ConsoleToMSBuild="true">
            <Output TaskParameter="ConsoleOutput" PropertyName="ConsoleOutput" />
        </Exec>

        <PropertyGroup>
            <RemoteDebugEnabled>$(ConsoleOutput.Split(';')[0])</RemoteDebugEnabled>
            <RemoteDebugMachine>$(ConsoleOutput.Split(';')[1])</RemoteDebugMachine>
        </PropertyGroup>

        <Message Text="RemoteDebugEnabled: $(RemoteDebugEnabled)" />
        <Message Text="RemoteDebugMachine: $(RemoteDebugMachine)" />
    </Target>

</Project>

After the above steps, if I build this project, I will get the info you want:

enter image description here

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10