I created a solution from the Blazor WebAssembly template where I checked off the ASP.NET Core hosted checkbox, so I have Client
, Server
, Shared
projects in the solution.
To this solution I added a Razor class library (without checking "Support pages and views") called BlogsRcl in which I have a Blazor component called Blogs.razor
.
I want to be able to inject an HttpClient into my Blogs.razor
component just like the FetchData.razor
component in the 'Client` project has, i.e. '@inject HttpClient Http'.
In the '.csproj' file for my BlogsRcl project I added either/or/both references to
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0.0" />
<PackageReference Include="System.Net.Http" Version="5.0.0" />
which, everything's kosher with Microsoft.Extensions.Http
, but when I add System.Net.Http
, every project in the solution complains that it is Unable to find package System.Net.Http with version (>= 5.0.0)
.
Yet when I do a Go To Definition on the HttpClient
in the FetchData.razor
component in the Client
project, the metadata says Assembly System.Net.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
.
Edit: I tried adding <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
too, to no avail.
What am I missing?
An RCL (<Project Sdk="Microsoft.NET.Sdk.Razor">
) should not need any of those package references as the required ones is part of the framework. In Visual Studio you can see what's included with the framework by expanding Project/Depedencies/Frameworks/Microsoft.NETCore.App/
.
But you do however have to make sure you also have added the required using directives.
Either add them to the top of your .razor
-file, or even better for such generic usings, add them to the _Imports.razor
-file.
@using System.Net.Http
@using System.Net.Http.Json