question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting PreserveCompilationContext=false trims Microsoft.AspNetCore.App shared runtime in deps files

See original GitHub issue

From @pranavkm on March 26, 2018 17:18

Steps to reproduce

  1. Create an webapi \ mvc \ emptyweb application using preview1 (or preview2) Sdk
  2. Build the project

The generated deps file will say that the application references both Microsoft.NETCore.App and Microsoft.AspNetCore.App:

"blah/1.0.0": {
    "dependencies": {
      "Microsoft.AspNetCore.App": "2.1.0-preview1-final",
      "Microsoft.NETCore.App": "2.1.0-preview1-26216-03"
    },
    "runtime": {
      "blah.dll": {}
    },
    "compile": {
      "blah.dll": {}
    }
},
  1. Delete the bin directory. Edit the csproj <PreserveCompilationContext>false</PreserveCompilationContext>. Rebuild the project
  2. The generated deps file will say that the app no longer references Microsoft.AspNetCore.App:
 "blah/1.0.0": {
   "dependencies": {
     "Microsoft.NETCore.App": "2.1.0-preview1-26216-03"
   },
   "runtime": {
     "blah.dll": {}
   }
 },

Environment data

.NET Command Line Tools (2.1.300-preview1-008174)

Product Information:
 Version:            2.1.300-preview1-008174
 Commit SHA-1 hash:  b8df89a54f

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.16299
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Users\Pranav\Downloads\dotnet-sdk-2.1.300-preview1-008174-win-x64\sdk\2.1.300-preview1-008174\

Microsoft .NET Core Shared Framework Host

  Version  : 2.1.0-preview1-26216-03
  Build    : f2c3216183d20416568a4bbf5bb7d153e826f153

More information:

The bulk of MVC’s controller discovery model is based on finding libraries that directly or transitively depend on any one of Mvc’s libraries. In 2.1.0-preview2, the Razor.Sdk only sets PreserveCompilationContext=true if the app has any files that require runtime compilation. For projects without views, such as webapi or empty web template, the produced deps file claims that the app does not reference Microsoft.AspNetCore.App -> Microsoft.AspNetCore.Mvc. Consequently no controllers are discovered.

Copied from original issue: dotnet/core-setup#3902

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Apr 9, 2018

Stepping back, the primary driver for turning off PreserveCompilationContext is to avoid publishing the refs directory when an application does not require it (e.g. when you’re writing an application not using runtime compiled views). Right now, there’s a single switch that determines the structure of the deps file and the published refs directory. Could we possibly introduce a new switch that controls publishing the refs directory?

// Microsoft.NET.Sdk.Razor.targets
<!-- Use PreserveCompilationContext's value if it was explicitly set in the user's project -->
<PreserveCompilationReferences Condition="'$(PreserveCompilationContext)' != ''">$(PreserveCompilationContext)</PreserveCompilationReferences>

<!-- Initialize PreserveCompilationContext -->
<PreserveCompilationReferences Condition="'$(PreserveCompilationReferences )' == ''">true</PreserveCompilationReferences>

Targets in Microsoft.NET.Sdk that control publishing would be modified to rely on this new switch: https://github.com/dotnet/sdk/blob/6945e3694c918eea4c8c4fb6217e1485b179994b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.PreserveCompilationContext.targets#L45-L46 https://github.com/dotnet/sdk/blob/6945e3694c918eea4c8c4fb6217e1485b179994b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.PreserveCompilationContext.targets#L84-L85

It’s a breaking change for users relying on the refs directory in non-web scenarios, but I’m not sure if that’s a common scenario.

0reactions
nguerreracommented, Jan 8, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

PreserveCompilationContext not configured by default - .NET
Learn about the breaking change in ASP.NET Core 6.0 where the PreserveCompilationContext property is no longer configured by default.
Read more >
Exploring the Microsoft.AspNetCore.App shared framework ...
When you publish your app, the Microsoft.AspNetCore.All metapackage trims out all the dependencies that it knows will be in the runtime store, ...
Read more >
What is deps.json, and how do I make it use relative paths?
The answer to your first question, per the Runtime Configuration File Documentation: And the answer to your second question, is to remove ...
Read more >
Deep-dive into .NET Core primitives, part 2: the shared ...
To put it simply, a .NET Core shared framework is a folder of assemblies (*.dll files) that are not in the application folder....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found