.netblazordotnet-aspire

Adding .Net Aspire to Existing Blazor Server App


I have a Blazor Server .Net 8 web application. I right-click on the Blazor sever project and select "Add" then ".Net Aspire Orchestrator Support".

This added the two Projects (AppHost, and Service Defaults) to my solution. The AppHost project is selected as the start-up project. When I run the app in Debug mode it fails and I am getting the below-attached error.

Any idea what is wrong? It seems it's trying to convert a string to a boolean.

When I debug my app normally without using Aspire, I use IIS Express in Visual Studio

If it helps this is the full error msg

System.Text.Json.JsonException
  HResult=0x80131500
  Message=The JSON value could not be converted to System.Nullable`1[System.Boolean]. Path: $.profiles.DptBlazor.dotnetRunMessages | LineNumber: 27 | BytePositionInLine: 33.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.ContinueDeserialize(ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Stream utf8Json)
   at Aspire.Hosting.LaunchProfileExtensions.GetLaunchSettings(IProjectMetadata projectMetadata)
   at Aspire.Hosting.LaunchProfileExtensions.GetLaunchSettings(ProjectResource projectResource)
   at Aspire.Hosting.LaunchProfileExtensions.TrySelectLaunchProfileFromEnvironment(ProjectResource projectResource, String& launchProfileName)
   at Aspire.Hosting.LaunchProfileExtensions.SelectLaunchProfileName(ProjectResource projectResource)
   at Aspire.Hosting.LaunchProfileExtensions.GetEffectiveLaunchProfile(ProjectResource projectResource, Boolean throwIfNotFound)
   at Aspire.Hosting.ProjectResourceBuilderExtensions.WithProjectDefaults(IResourceBuilder`1 builder, Boolean excludeLaunchProfile, String launchProfileName)
   at Aspire.Hosting.ProjectResourceBuilderExtensions.AddProject[TProject](IDistributedApplicationBuilder builder, String name)
   at Program.<Main>$(String[] args) in C:\inetpub\wwwroot\MyApp\Dpt.AppHost\Program.cs:line 3

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: Cannot get the value of a token type 'String' as a boolean.

enter image description here

enter image description here

Im using IIS Express when I run it without Aspire and it works fine. But when I try and use Aspire i get the error

My launchSettings.json is below, im using Windows Auth:

  {
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iis": {
      "applicationUrl": "http://localhost/DptBlazor",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IIS",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

After cleaning up launchSettings.son I got it to run a bit more, but now I get an odd error.

enter image description here enter image description here enter image description here enter image description here

enter image description here enter image description here

My launching settings file is below:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:61105",
      "sslPort": 44392
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IIS",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost/DptBlazor",
      "dotnetRunMessages": true
    },
    "DptBlazor": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "dotnetRunMessages": true
    }
  }
}

Below is the code im using to get the Windows Authentication User Name, which seems to come back blank:

private readonly AuthenticationStateProvider authProvider;

       var authenticationStateTask = await authProvider.GetAuthenticationStateAsync();

   var user = authenticationStateTask.User;

   if (user.Identity.IsAuthenticated)
   {
       return user.Identity.Name.Split('\\')[1]; ;
   }

Solution

  • This solution works for a Blazor Server application running with "IIS Express" profile and .Net Aspire Orchestrator Support added later on. This will generate an error message something like below when you first run your Aspire.host project with "https" profile.

    enter image description here

    To resolve this error, you need to update launchSettings.json file of your original Blazor server project. Go to BlazorServerApp.UI project > Properties folder > expand it and open launchSettings.json file. It will look like something below.

    {
      "iisSettings": {
        "windowsAuthentication": true,
        "anonymousAuthentication": false,
        "iisExpress": {
          "applicationUrl": "http://localhost:61105",
          "sslPort": 44392
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "BlazorServerApp.UI": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          "dotnetRunMessages": "true" 
        }
      }
    }
    

    Change the line from "dotnetRunMessages" : "true" to "dotnetRunMessages": true. This is the bug from Asire framework. This will start your project but will show you an empty or "None" endpoints on Aspire Dashboard. See user's question screenshot above.

    Add below two lines into your "IIS Express" profile.

     "applicationUrl": "https://localhost/blazorserverapp",
     "dotnetRunMessages": true
    

    This will fixed the problem and start showing you your newly entered endpoints into Aspire Dashboard. The final launchSettings.json will look like below.

    {
      "iisSettings": {
        "windowsAuthentication": true,
        "anonymousAuthentication": false,
        "iisExpress": {
          "applicationUrl": "http://localhost:61105",
          "sslPort": 44392
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          //below lines added manually
        //"applicationUrl": "https://localhost:44392",
          "applicationUrl": "https://localhost/blazorserverapp",
          "dotnetRunMessages": true
        },
        "BlazorServerApp.UI": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "https://localhost:5001;http://localhost:5000",
          //Changed below line manually from "true" to true
          "dotnetRunMessages": true
        }
      }
    }
    

    In Aspire.Host, I have to changed back to builder.AddProject<Projects.BlazorServerApp_UI>("blazorserverapp-ui"); without mentioning the "IIS Express".

    If you are getting "failed to start" error on Aspire Dashboard then follow below two steps.

    1. Close Solution and Close Visual Studio
    2. Delete ".vs" folder from solution path
    3. Reopen VS and your solution.
    4. Run the Aspire.Host

    It should start and Aspire Dashboard will start showing below Endpoint registered.

    enter image description here

    Let me know this works for you or not. I have to update my packages to the latest version for everything to workout well. Make sure you are not using Visual Studio Preview version.