Skip to content

Razor compilation APIs exposing Roslyn types being obsoleted in 2.2.0 and removed in 3.0.0 #4902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danroth27 opened this issue Jul 30, 2018 · 12 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Milestone

Comments

@danroth27
Copy link
Member

As part of the 2.1.0 release, ASP.NET Core added support for build time compilation of cshtml files (Razor Views & Razor Pages). This feature is on by default and allows build time verification of the correctness of Razor files, and also faster startup performance. Additionally, MVC watches for changes to cshtml files and seamlessly updates build-time compiled views with runtime compiled views. This allows editing cshtml files and then refreshing the browser to see the changes.

MVC uses the app's compilation context, details about the references and settings that were used to compile the source code (.cs) in the app, to compile views at runtime. In addition to the compilation context, MVC exposes options such as RazorViewEngineOptions.CompilationCallback and AdditionalCompilationReferences that allow configuring compilation specifically for runtime.

Runtime specific customizations are unavailable at build time and consequently the two outputs could be different.

To address this inconsistency, we plan on removing APIs in MVC that change or modify runtime compilation. For 2.2.0, we're marking these APIs obsolete with the intent to remove them in 3.0.0. This is the list of affected APIs:

  • Properties on Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions

    • AdditionalCompilationReferences
    • CompilationCallback
  • Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeature

  • Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvider

  • Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorReferenceManager

No changes are required to your application if it does not use these APIs.

Apps using these APIs to add assembly references to the compilation context for runtime compilation should instead use ApplicationPartManager.AddApplicationPart to add application parts for each assembly reference, or switch to a built-time compilation model (see Create reusable UI using the Razor Class Library project).

Other scenarios blocked by the removal of these APIs and not covered by alternative MVC features should raised here.

@aspnet-hello aspnet-hello transferred this issue from aspnet/Mvc Dec 14, 2018
@aspnet-hello aspnet-hello added this to the Discussions milestone Dec 14, 2018
@aspnet-hello aspnet-hello added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 14, 2018
@ngohungphuc
Copy link

ngohungphuc commented Dec 14, 2018

Hi @danroth27 I have below code so how can I change it accordingly to AddApplicationPart because AddApplicationPart require assembly to be pass in
devenv_mqw62b3mzz

Full code

            var mvcBuilder = services
                .AddMvc()
                .AddJsonOptions(
                    options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                )
		        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
                .AddRazorOptions(o =>
                {
                    foreach (var module in modules)
                    {
                        o.AdditionalCompilationReferences.Add(
                            MetadataReference.CreateFromFile(module.Assembly.Location));
                    }
                });

@pranavkm
Copy link
Contributor

What do you use to create the passed in instance of MetadataReference?

@ngohungphuc
Copy link

@pranavkm I just added my code please check it.
Thank you

@pranavkm
Copy link
Contributor

Looks like you're already adding the assembly as an application part on line 115. Removing the foreach loop that adds compilation references should suffice.

@MathiasKowoll
Copy link

I have the same issue, just adding the aplicación part assembly does not register views and models from those compilations.

@thiennn
Copy link

thiennn commented Dec 28, 2018

In https://github.com/simplcommerce/SimplCommerce, with it's modular architecture, each module is a Razor Class Library. We manually load modules and then add them to the main web app using ApplicationPart.

We also support theming, users can switch between the themes at run time. This feature required run time compilation of cshtml file. Adding ApplicationPart seems not work this scenario

https://github.com/simplcommerce/SimplCommerce/blob/894813fe6f52e5a28307d424deb198686f78110c/src/SimplCommerce.WebHost/Extensions/ServiceCollectionExtensions.cs#L79

@pranavkm
Copy link
Contributor

@MathiasKowoll here's the bit of code that's required to register assembly parts including the views assembly: https://github.com/pranavkm/PluginApp/blob/master/MainApp/Startup.cs#L74-L90

This feature required run time compilation of cshtml file. Adding ApplicationPart seems not work this scenario

@thiennn can you be more precise about what does not work? I'd recommend filing a separate issue with a simple repro to show what's expected and what does not work.

@camelofcode
Copy link

camelofcode commented Jan 9, 2019

Using Core 2.2

I have an issue the same as #2126 which I solved this using @pranavkm 's solution on that issue (Aug 14, 2017).

However, since MetadataReferenceFeature is being obsoleted, I am trying to rewrite so it doesn't use it. But it just brings back my original issue.

I'm basically doing what @pranavkm 's other link says (https://github.com/pranavkm/PluginApp/blob/master/MainApp/Startup.cs#L74-L90) now, but it makes no difference, always get this error, unless using the workaround in issue 2126.

[14:38:42 ERR] An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Cannot find compilation library location for p
ackage 'Modular.Modules.Application1'
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePa
ths(ICompilationAssemblyResolver resolver, List1 assemblies) at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePa ths() at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.<>c.<GetReferencePa ths>b__8_0(CompilationLibrary library) at System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext()
at Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvide
r.PopulateFeature(IEnumerable1 parts, MetadataReferenceFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateF eature[TFeature](TFeature feature) at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.GetCo mpilationReferences() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boole an& initialized, Object& syncLock, Func1 valueFactory)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.get_C
ompilationReferences()
at Microsoft.AspNetCore.Mvc.Razor.Internal.LazyMetadataReferenceFeature.get_R
eferences()
at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.Execu
teCore(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCode
Document codeDocument)
at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDo
cument document)
at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(
RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjec
tItem projectItem)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(S
tring relativePath)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.OnCacheMiss(Stri
ng normalizedPath)

A screenshot of Modular.Modules.Application1 sitting in mvcBuilder.PartManager.ApplicationParts, and having the path that it really exists at on disk.
image

@MathiasKowoll
Copy link

@camelofcode look at this bug #6215

@camelofcode
Copy link

@MathiasKowoll Thanks, that worked great - deleting the deps.json file for the dll fixes it. Not a surprise as my code is heavily inspired by @thiennn 's article here https://www.codeproject.com/Articles/1109475/Modular-Web-Application-with-ASP-NET-Core - so anything that fixes his problems likely helps me!

@MathiasKowoll
Copy link

@camelofcode You are welcome, my code is also heavily bases on @thiennn work.

@dougbu dougbu self-assigned this Feb 12, 2019
@dougbu dougbu added 1 - Ready enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Feb 12, 2019
@dougbu dougbu removed their assignment Feb 12, 2019
@dougbu dougbu removed 1 - Ready enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Feb 12, 2019
@aspnet-hello
Copy link

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.

@dotnet dotnet locked and limited conversation to collaborators Apr 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

8 participants