Search code examples
c#mauiblazor-hybrid

How to import some classes/structs/record to "Console App"-type projects from ".NET MAUI Blazor Hybrid App"-type projects?


I need to import some classes/structs/records from .NET MAUI Blazor Hybrid App to Console App:

enter image description here

Please note that:

  • No Blazor or .NET MAUI functionality will be accessed in the console app.
  • The console application will be run independently on .NET MAUI application and required only for development purposes, not on production.

The example of class which I want to import (it will required to make it public):

internal record PeopleManagementPageEnglishLocalization : PeopleManagementPageLocalization
{

  internal override string personManagerActivationGuidance { get; } = 
      "Click or tap the person card to view details or edit the dedicated person data.";
  
  internal override SuccessMessages successMessages { get; init; } = new()
  {
    personAddingSucceeded = "Person has been added.",
    personUpdatingSucceeded = "Person has been updated",
    personDeletingSucceeded = "Person has been deleted."
  };
  
  internal override ErrorMessages errorMessages { get; init; } = new()
  {
    personAddingFailed = SharedStaticEnglishStrings.SingleInstance.
        buildDataRetrievingOrSubmittingFailedPoliteMessage("The malfunction has occurred during adding of new person."),
    personUpdatingFailed = SharedStaticEnglishStrings.SingleInstance.
        buildDataRetrievingOrSubmittingFailedPoliteMessage("The malfunction has occurred during updating of person."),
    personDeletingFailed = SharedStaticEnglishStrings.SingleInstance.
      buildDataRetrievingOrSubmittingFailedPoliteMessage("The malfunction has occurred during deleting of person.")
  };
  
}

If add the project reference to .NET MAUI Blazor Hybrid App in Console App, previously worked Console App will brake because of plenty of errors:

enter image description here

For example, Cannot resolve symbol 'System' in

using System.Text.Json.Nodes;

If try to execute the console application, it will fail with:

Error (active)  NU1201  Project Client is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project Client supports:
  - net8.0-android34.0 (.NETCoreApp,Version=v8.0)
  - net8.0-ios17.5 (.NETCoreApp,Version=v8.0)
  - net8.0-maccatalyst17.5 (.NETCoreApp,Version=v8.0)
  - net8.0-windows10.0.19041 (.NETCoreApp,Version=v8.0) GeneratorOfAssetsForStaticPreview   D:\***\Automation\GeneratorOfAssetsForStaticPreview\GeneratorOfAssetsForStaticPreview.csproj

The Visual Studio will highlight the Console App with triangled exclamation mark icon, however, without explanations:

enter image description here

Everything will restore once remove the link to .NET MAUI Blazor Hybrid App from Console App.

"Why are you doing this? Maybe it is the X|Y problem.

Well, I hope so. If the C# applications could have multiple entry points, it was the solution. All that required in this hypothetical case is create one more entry point inside .NET MAUI Blazor Hybrid App project, and all desired classes will be accessible. Additionally, no need to make all desired classes/structs/records public.

I need to generate some JSON files based on classes like PeopleManagementPageEnglishLocalization but:

  • Although this JSON will NOT be used by .NET MAUI application, this JSON is required inside the .NET MAUI Blazor Hybrid App project (currently I am planning to output the generated JSON files to .NET MAUI Blazor Hybrid App project).
  • The classes like PeopleManagementPageEnglishLocalization could NOT be extracted to another projects because it will brake the components organizing.

If these conditions will be satisfied, I can consider the alternative solutions.

Update

Tried to change TargetFramework from net8.0 to net8.0-windows.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <!-- ↓↓↓ -->

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <!-- ... -->

</Project>

The Error will change to:

Error (active)  NU1201  Project Client is not compatible with net8.0-windows7.0 (.NETCoreApp,Version=v8.0). Project Client supports:
  - net8.0-android34.0 (.NETCoreApp,Version=v8.0)
  - net8.0-ios17.5 (.NETCoreApp,Version=v8.0)
  - net8.0-maccatalyst17.5 (.NETCoreApp,Version=v8.0)
  - net8.0-windows10.0.19041 (.NETCoreApp,Version=v8.0) GeneratorOfAssetsForStaticPreview   D:\IntelliJ IDEA\****\Automation\GeneratorOfAssetsForStaticPreview\GeneratorOfAssetsForStaticPreview.csproj 1       

Error (active)      Project '..\..\Implementation\Elements\Client\Client.csproj' targets 'net8.0-maccatalyst;net8.0-ios;net8.0-android;net8.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v8.0'.   GeneratorOfAssetsForStaticPreview   C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets   1890        

The .csproj file of .NET MAUI Blazor Hybrid Application begins from:

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFrameworks>net8.0-maccatalyst;net8.0-ios;net8.0-android</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
    <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
    <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
    <OutputType>Exe</OutputType>
    <RootNamespace>Client</RootNamespace>
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnableDefaultCssItems>false</EnableDefaultCssItems>
    <Nullable>enable</Nullable>

   <!-- ... ->

Solution

  • Your MAUI app targets the following frameworks:

    net8.0-maccatalyst
    net8.0-ios
    net8.0-android
    net8.0-windows10.0.19041.0
    

    If you want the console app to reference the MAUI app, the console app needs to target a framework that is compatible with the above framworks. I assume your console app will run on Windows, so it needs to target a framework that is compatible with net8.0-windows10.0.19041.0

    Then accoriding to this table, net8.0 is not compatible with net8.0-windows10.0.19041.0.

    According to this table net8.0-windows is shorthand for net8.0-windows7.0. The documentation of NU1201 error mentions that the framework verion of the referenced project needs to be equal or lower than the consuming project. So net8.0-windows is not compatible with net8.0-windows10.0.19041.0 too.

    Therefore the console app needs to target net8.0-windows10.0.19041.0 at least.