Blazer serverside multi-targeting netcoreapp3.1 and net472 will not build

iqworks Information Quality Works 331 Reputation points
2022-11-11T18:43:15.36+00:00

Hi, I just created a blazer serverside 3.1 project and added net472. In my csproj I have this:
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
</PropertyGroup>

I also have these specified in my program.cs
using AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

These are the errors
Error CS0234 The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Program.cs 2 Active

Error CS0234 The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Program.cs 3 Active

Error CS0246 The type or namespace name 'IHostBuilder' could not be found (are you missing a using directive or an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Program.cs 21 Active

Error CS0103 The name 'Host' does not exist in the current context MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Program.cs 22 Active

Error CS0246 The type or namespace name 'IApplicationBuilder' could not be found (are you missing a using directive or an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Startup.cs 35 Active
Warning RAZORSDK1005 One or more Razor component files (.razor) were found, but the project is not configured to compile Razor Components. Configure the project by targeting RazorLangVersion 3.0 or newer. For more information, see https://go.microsoft.com/fwlink/?linkid=868374. MbsaBlazorServerMultiTarget C:\Program Files\dotnet\sdk\6.0.203\Sdks\Microsoft.NET.Sdk.Razor\targets\Sdk.Razor.CurrentVersion.targets 720

Error CS0246 The type or namespace name 'IConfiguration' could not be found (are you missing a using directive or an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Startup.cs 18 Active

Error CS0246 The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) MbsaBlazorServerMultiTarget (net472) D:\MBSSys\MbsaBlazorServerMultiTarget\Startup.cs 35 Active

I am sure its something I need to do or not do.

thanks for any advice or suggestions

Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-11-12T15:59:49.453+00:00

    The chart says the EF core library was built multi-target. And which targets each version supports.

    You could build a muti-target blazor app (probably requiring conditional assembly) but only for .net core 3.1 and later.

    Your error is caused by the fact 4.7 runtime does not the AspNetCore libraries. Most core libraries depend on runtime features that do not exist in the 4.* runtime. 4.7 only support c# 7.3 and aspnet core and blazor are written in later versions of c# which required changes in the .net runtime.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-11-11T23:20:03.477+00:00

    blazor projects only run under core 3.1+ and can not multi-target 4.*

    you can share .netstandad 2.0 class libraries projects between blazor and a 4.7 projects. extract the shared code to a library, though none of this shared code can have blazor or aspnet core dependencies.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.