Skip to content

Advice Needed: MSBuild task for NSwag breaks when implement Aspire.Seq #5100

@johnkattenhorn

Description

@johnkattenhorn

Describe the bug

If I add Aspire SEQ integration the MSBuild task breaks with the following exception:

NSwag command line tool for .NET Core Net90, toolchain v14.2.0.0 (NJsonSchema v11.1.0.0 (Newtonsoft.Json v13.0.0.0));Visit http://NSwag.org for more information.;NSwag bin directory: C:\Users\JohnKattenhorn.nuget\packages\nswag.msbuild\14.2.0\tools\Net90;Executing file 'config.nswag' with variables 'Configuration=Debug'...;Launcher directory: C:\Users\JohnKattenhorn.nuget\packages\nswag.msbuild\14.2.0\tools\Net90;System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.; ---> System.InvalidOperationException: NSwag requires the assembly Applicita.TestApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null to have either an BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method

This is caused by these lines of code in the Aspire.Seq extension because at BuildTime the settings.ServerUrl is null (its populated during runtime), I'm guessing the NSwag build task is executing this code.

 if (!settings.DisableHealthChecks)
 {
     if (settings.ServerUrl is not null)
     {
         builder.TryAddHealthCheck(new HealthCheckRegistration(
             "Seq",
             _ => new SeqHealthCheck(settings.ServerUrl),
             failureStatus: default,
             tags: default));
     }
     else
     {
         throw new InvalidOperationException(
             "Unable to add a Seq health check because the 'ServerUrl' setting is missing.");
     }
 }

Version used

14.2

To Reproduce

Create a Aspire Starter app from the Visual Studio 2022, Add Seq as per the documentation here. (https://learn.microsoft.com/en-us/dotnet/aspire/logging/seq-integration?tabs=dotnet-cli), try to build the solution.

Expected behavior

The build should be completed successfully.

Additional context

I've workaround this by bringing the source code for Aspire into my solution and commenting out the source code.

Adding this line to the appsetting.config also fixes the issue but is not desired as configuration is being done in code:

  "Aspire": {
    "Seq": {
      "ServerUrl": "http://localhost:5341"
    }
  }

I've also logged an issue for awareness on the Aspire Site, any workarounds or thoughts about what the Aspire Code should look like or modification to the NSwag configuration would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions