Search code examples
c#asp.net-coreblazorblazor-webassembly

Hosted blazor assembly conflict


My solution structure is hosted blazor webassembly template :

//Clinet Part
BlazorApp
ClientModel

//Server Part
ServerCoreApp
ServerModel

//template of this project is "shared project" and cannot compile
SharedProject

I have razor pages on server side and blazor client at the same time

Shared project has class 'ClassA' and I added it to ClientModel and ServerModel as linked items. with debugging symbol, I changed some behavior of ClassA on client or server for example

#if AspCore
    Console.Writeline("Server Project");
#else
    Console.Writeline("Client Project");
#endif

I dont have any problem on debugging. but after publish solution I got error for linked class conflict

The type 'ClassA' exists in both 'ClientModel,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServerModel, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null'

this happened on razor pages because we cannot define extern alias on razor pages


Solution

  • Finally I found solution on debug, visual studio can find assemblies correctly and when we publish project, assembly resolver cannot find correct assembly and raises conflict error.

    so, we must add two reference of Blazor app to Server app 1- a reference for debug purpose as

    <ProjectReference Include="..\Client\BlazorApp.csproj" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" />
    

    2- a reference for publish

     <ProjectReference Include="..\Client\BlazorApp.csproj" ReferenceOutputAssembly="False" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'" />
    

    then on publishing, visual studio does not copy client app dlls on root of publish folder and all of client dlls is exists on wwwroot_content and wwwroot_framework folder and no conflict raised.

    another solution is delete all client-only dlls from root of published folder. but we must do it every time after publishing