Having been having lots of fun playing with Blazor, and even using it on a couple of new projects, I started wrapping common bits of code into components, so I could reuse them elsewhere. This started off as a folder in the current project folder, but I quickly realised that I really needed them in a separate project.
No problem, just create a Blazor Library. Except I don’t have a template for one of those! For some reason, it seems you can’t add such a project through VS, you have to use the CLI. Now I’m old enough to remember when all we had was a CLI, and am really puzzled why Microsoft think it’s such a great idea to go back to one. However, I’m sure someone thought this was a good idea. All the blogs I saw on the subject said all you needed to do was this…
dotnet new blazorlib -n MyBlazorLibrary
Problem with that was that I got a lovely red error saying it couldn’t find such a template.
OK, a bit more digging led to advice on how to update your templates…
dotnet new -i Microsoft.AspNetCore.Blazor.Templates
Easy peasy… except it didn’t work either. Digging through the dense output of the command, I could see that it was trying to pick up the package from Telerik’s Nuget package source. No, I have no idea why it was trying to do this either.
I right-clicked on the solution node in an open instance of VS (any would have done) and opened the Nuget package manager for the solution. I clicked the little gear wheel to open the options and deselected the Telerik Nuget package source, clicked OK to close the dialog and an the command again.
Now is it me, or is this pathetic? I have four package sources set up in there. Why didn’t it try them all if it couldn’t find the package in the first one? For that matter, why did it try that source at all, when the nuget.org source was above it on the list?
These and other questions will probably never be answered.
Update about two frustrating hours later… Turns out that I shouldn’t have been using that template at all! I couldn’t persuade VS to see the Blazor Library at all, it claimed that the namespace just wasn’t there.
To add a library for Blazor components, you need to add a Razor Pages Library, not a Blazor Library. Makes sense eh? No, I didn’t think so either. Once I added a Razor Pages Library, I was able to use it without problem.
OK, so that’s a lie, I had plenty of problems, but of a different sort. Maybe that will be the subject of a different post. Maybe.