Why is Microsoft.CodeAnalysis published with ASP.NET Core website?
Asked Answered
B

5

25

I'm publishing an ASP.NET Core MVC 3.0 website and the output folder contains lots of reference in many language to Microsoft.CodeAnalysis librairies, someone knows why?

Of course the FxCopAnalyzers Nuget package is installed on the project, but it was not published in an earlier version of the project, so I don't understand why it is now since it should be useful only at dev time not in a production environment.

Byandby answered 8/10, 2019 at 17:18 Comment(1)
It seems to be somehow related to .net core 3 compiling views on publication but i'm not sureByandby
S
8

contains lots of reference in many language to Microsoft.CodeAnalysis librairies

I did encounter the same issue when I used the 3.0 version. But I don't think it's caused by .net core 3 compiling views on publication because there's also View ViewCompilation in the release/2.1 branch .


it should be useful only at dev time not in a production environnement.

  1. I believe you're correct. These Analysis should be used at devtime only.

  2. But when I uninstall the SDK(3.0) manually and install the latest SDK again, I can't not reproduce any more. I don't why it happens, maybe it has been fixed now. It is more likely caused by another reason: I added an extra reference on other packages that depends on Microsoft.CodeAnalysis by accident). Anyway, please upgrade your SDK to the latest version firstly.

  3. Another important thing is, when using Visual Studio to add controller, it will add a reference on Microsoft.VisualStudio.Web.CodeGeneration.Design automatically. Note this package has a dependency on Microsoft.CodeAnalysis.Common package indirectly. Here the Microsoft.CodeAnalysis.Common is a shared package used by the Microsoft .NET Compiler Platform ("Roslyn"). If you download this package and unzip this lib manually, you'll find that there's a Microsoft.CodeAnalysis.dll :

    microsoft.codeanalysis.common.3.3.1/
    ├───lib/
    │   └───netstandard2.0/
    │       ├─── ...
    │       ├─── Microsoft.CodeAnalysis.dll
    │       ├─── Microsoft.CodeAnalysis.pdb
    │       ├─── Microsoft.CodeAnalysis.xml
    │       └─── ...
    ├───package/
    │   └───...
    └───_rels/
    

    This package is only needed at Dev-Time. If you don't remove this dependency, you'll get quite a lot of dlls related to Microsoft.CodeAnalysis in your publish folder.

    <ItemGroup>
        <!-- this is not necessary when publishing -->
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
    </ItemGroup>
    

    Remove those packages that depends on Microsoft.CodeAnalysis, and then you should get no Microsoft.CodeAnalysis related dlls:

    enter image description here

Sweaty answered 9/10, 2019 at 6:57 Comment(7)
how can I see which package has direct or indirect dependency to Microsoft.CodeAnalysis ?Byandby
@Byandby https://mcmap.net/q/153285/-net-core-dependency-treeSweaty
ok it is like you said the Microsoft.VisualStudio.Web.CodeGeneration.Design package that has a dependency on it. By setting PrivateAssets property to all, Microsoft.CodeAnalysis files are not in the published project anymore. Just not sure if the code generation will still works fine because now there is a yellow triangle over the package in the project's dependency list.Byandby
@Byandby You only need this package during development time. Actually, if you don't need the scaffolding feature, e.g. using VSCode, you won't add such a dependency at all.Sweaty
@Byandby If you need scaffolding, when using VS, the package will be installed again. If you're using VSCode/CLI, you have to add such a package before invoking dotnet aspnet-codegenerator controlller ...Sweaty
I don't understand... i don't use VSCode I use Visual Studio EnterpriseByandby
@Byandby In short: there's no need to worry about it won't work: When you add a controller/scaffold in VS, VS will add this package automatically if there's no such a dependency ( this package is not necessary when publishing).Sweaty
P
24

For me, this line inside *.csproj file solved the issue somehow. It still deploys the Microsoft.CodeAnalysis, but only for en:

<PropertyGroup>
  <!-- ... -->
  <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

See the comment (by Jonathon Marolf) on the Github issue.

Paff answered 12/1, 2020 at 10:55 Comment(3)
This worked for me on ASP.Net Core 3.0, and prevented the country folders. Is it possible to prevent the subfolders of the "runtimes" folder that I don't need (unix, etc.) ?Reality
@Reality Please see this question regarding .NET Core 2.1: #53507729Paff
Thanks. So it's possible when publishing, but I guess you can't prevent the wasted folders when simply building/compiling.Reality
S
19

Here's my take at trying to make the solution easier to see.

The problem, more than likely is use of AddRazorRuntimeCompilation(). More specifically, in the startup.cs you likely to add razor runtime compilation like so:

IMvcBuilder builder = services.AddControllersWithViews()
                     .AddRazorRuntimeCompilation(); 

and to support that, your web project probably has a reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

That NuGet package has a dependency on Microsoft.CodeAnalysis that is producing all that unwanted output in the publish folder.

The fix is to edit the project file and make the dependency conditional on Debug mode like so:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" 
Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />
</ItemGroup>

and then in the startup.cs file, conditionally call AddRazorRuntimeCompilation() like so:

IMvcBuilder builder = services.AddControllersWithViews();

#if DEBUG
if (Env.IsDevelopment()) {
    builder.AddRazorRuntimeCompilation();
}
#endif

This will cause all those Microsoft.CodeAnalysis librairies to only be out when compiling in Debug mode. So now, when you publish using Release mode, they will not be part of the output.

Strudel answered 6/1, 2020 at 20:11 Comment(4)
IWebHostEnvironment (Env in the above example) is not readily available in ConfigureServices(), you can reveal it quite easily though. #37660543Respire
@AntonioNicolaasTeyken Excellent addition, that’s an important detail I didn’t realize I glossed over.Strudel
@Strudel Thanks! This is a great tweak for Blazor sitesHarvester
For me, as mentioned in this answer, the files were included by the presence of the package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. But I solved it adding ExcludeAssets="All" to the reference in the csproj file as Eduardo Teixeira suggested. Thank you both.Belonging
A
16

In my case, the problem was "Microsoft.VisualStudio.Web.CodeGeneration.Design". I needed to change the package reference in ".csproj" file to include ExcludeAssets="all":

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" ExcludeAssets="All" />
Arrowroot answered 11/3, 2020 at 18:51 Comment(2)
as usual - best answer a last oneShizukoshizuoka
In an ASP.NET Core 3.1 project, I don't see any <PackageReference> to Microsoft.VisualStudio.Web.CodeGeneration.Design, unfortunately.Dolomites
S
8

contains lots of reference in many language to Microsoft.CodeAnalysis librairies

I did encounter the same issue when I used the 3.0 version. But I don't think it's caused by .net core 3 compiling views on publication because there's also View ViewCompilation in the release/2.1 branch .


it should be useful only at dev time not in a production environnement.

  1. I believe you're correct. These Analysis should be used at devtime only.

  2. But when I uninstall the SDK(3.0) manually and install the latest SDK again, I can't not reproduce any more. I don't why it happens, maybe it has been fixed now. It is more likely caused by another reason: I added an extra reference on other packages that depends on Microsoft.CodeAnalysis by accident). Anyway, please upgrade your SDK to the latest version firstly.

  3. Another important thing is, when using Visual Studio to add controller, it will add a reference on Microsoft.VisualStudio.Web.CodeGeneration.Design automatically. Note this package has a dependency on Microsoft.CodeAnalysis.Common package indirectly. Here the Microsoft.CodeAnalysis.Common is a shared package used by the Microsoft .NET Compiler Platform ("Roslyn"). If you download this package and unzip this lib manually, you'll find that there's a Microsoft.CodeAnalysis.dll :

    microsoft.codeanalysis.common.3.3.1/
    ├───lib/
    │   └───netstandard2.0/
    │       ├─── ...
    │       ├─── Microsoft.CodeAnalysis.dll
    │       ├─── Microsoft.CodeAnalysis.pdb
    │       ├─── Microsoft.CodeAnalysis.xml
    │       └─── ...
    ├───package/
    │   └───...
    └───_rels/
    

    This package is only needed at Dev-Time. If you don't remove this dependency, you'll get quite a lot of dlls related to Microsoft.CodeAnalysis in your publish folder.

    <ItemGroup>
        <!-- this is not necessary when publishing -->
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
    </ItemGroup>
    

    Remove those packages that depends on Microsoft.CodeAnalysis, and then you should get no Microsoft.CodeAnalysis related dlls:

    enter image description here

Sweaty answered 9/10, 2019 at 6:57 Comment(7)
how can I see which package has direct or indirect dependency to Microsoft.CodeAnalysis ?Byandby
@Byandby https://mcmap.net/q/153285/-net-core-dependency-treeSweaty
ok it is like you said the Microsoft.VisualStudio.Web.CodeGeneration.Design package that has a dependency on it. By setting PrivateAssets property to all, Microsoft.CodeAnalysis files are not in the published project anymore. Just not sure if the code generation will still works fine because now there is a yellow triangle over the package in the project's dependency list.Byandby
@Byandby You only need this package during development time. Actually, if you don't need the scaffolding feature, e.g. using VSCode, you won't add such a dependency at all.Sweaty
@Byandby If you need scaffolding, when using VS, the package will be installed again. If you're using VSCode/CLI, you have to add such a package before invoking dotnet aspnet-codegenerator controlller ...Sweaty
I don't understand... i don't use VSCode I use Visual Studio EnterpriseByandby
@Byandby In short: there's no need to worry about it won't work: When you add a controller/scaffold in VS, VS will add this package automatically if there's no such a dependency ( this package is not necessary when publishing).Sweaty
H
4

I do not use Razor Compilation but still had the same problem. I combined answers from here to solve it. To the csproj, I first added this:

<PropertyGroup>
    <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

And changed the actual item to:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" 
    Condition="'$(Configuration)' == 'Debug'" />
Hart answered 7/8, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.