Reputation: 3120
I have an ASP.NET Core project that was recently updated to .NET 8.0, and now I am attempting to add a Blazor WASM project. I followed these instructions to get the project added to my existing ASP.NET Core project (copied the WASM project from a donor project).
The project is an ASP.NET Core project (MyApp
) which is hosting a Blazor WASM project (MyApp.Client
). The Client project has a Page with breakpoints in it. Everything is working except debugging the Client app in Visual Studio 2022.
I also created a brand new project using the "Blazor Web App" template (setup the same as the donor project I used) and it does successfully break on breakpoints, so I know the browser/environment is correct and its some sort of project configuration issue or something I did wrong when copying the donor project.
Things I've tried/confirmed:
launchSettings.json
has the correct inspectUri
Microsoft.AspNetCore.Components.WebAssembly.Server
is referenced by the host projectOnInitialized()
to give the time for the debugger to attach (and also tested breakpoints inside of a button click with no success)Program.cs
MyApp
project)System.Diagnostics.Debugger.Break();
does not work eitherI don't know what else to try, any suggestions on what to look for next would be appreciated.
Upvotes: 6
Views: 3179
Reputation: 11
I came up with this minimal solution based loosely on Microsoft's documentation and other sources/experimentation/head banging. These are the essentials.
ASP.NET Core Web App (MVC)
project template.Blazor Web App
template, as described by Microsoft's documentation. Choose Interactive render mode as "WebAssembly", and Interactivity location as "Per page/component".[ProjectName].Client
project folder from the donor solution to the MVC solution. The rest of the donor app can be discarded.[ProjectName].Client
project to the solution containing the MVC project using Solution > Add > Existing Project.[ProjectName].Client
project.Components
folder in the client project.Components/App.razor
with the text @* no-op component *@
.Pages
folder in the Components
folder.Components/Pages/Counter.razor
with:@page "/counter"
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
inspectUri
property to your debugging profile in Properties/launchSettings.json
:"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7069;http://localhost:5083",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
IMPORTANT: The
inspectUri
parameter does not work with IIS Express
Add the Microsoft.AspNetCore.Components.WebAssembly.Server
Nuget package to the MVC project.
Add app.UseWebAssemblyDebugging();
to Program.cs
this way:
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
else
{
app.UseWebAssemblyDebugging();
}
builder.Build();
:builder.Services.AddRazorComponents()
.AddInteractiveWebAssemblyComponents();
app.Run();
(with the appropriate using):using [ProjectName].Client.Components;
app.MapRazorComponents<App>()
.AddInteractiveWebAssemblyRenderMode();
<script src="_framework/blazor.webassembly.js"></script>
at the end of the _Layout.cshtml
page body before the line @await RenderSectionAsync("Scripts", required: false).
@using [ProjectName].Client.Components.Pages
<component type="typeof(Counter)" render-mode="WebAssembly"><component>
TIP: Use this in the head of your
_Layout.cshtml
if your app URL is different on the server:
<environment include="Production">
<base href="/PathToYourApp" />
</environment>
NOTE: I've had a few circumstances when the debugger still did not attach until after I hit Ctrl + Shift + R. According to another answer on SO, this might be due to certain processes being too slow during startup.
Upvotes: 0