Runtime: .Net Core 3 : InvalidOperationException when calling ResolveReferencePaths on a referenceassembly

Created on 7 Oct 2019  路  12Comments  路  Source: dotnet/runtime

Describe the bug

On Asp .Net Core 3 getting referencePaths for all reference assembly throw a InvalidOperationException, it used to work on Asp .Net Core 2.

To Reproduce

Steps to reproduce the behavior:

Use Asp .Net Core 3
Run this code at the end of Startup.Configure
```c#
var context = DependencyContext.Load(typeof(Startup).Assembly);

var references = context.CompileLibraries.SelectMany(library => library.ResolveReferencePaths()).ToList();
```
See error : InvalidOperationException

Expected behavior

I expect to have all the reference paths like I used to have in Asp .net Core 2

Additional context

DepencyModel is used to load assembly to create Roslyn compilation. It is used by RazorLight and this library is often used for email generation.

area-DependencyModel

Most helpful comment

My bad, I was refering to the PreserveCompilationContext parameters. Thanks a lot, it works !

All 12 comments

@kYann Is this the exception you get?

System.InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery

(Just want to make sure I'm trying to debug the same problem)

Yes exactly ! The exception happens for all referenceassembly type because the DependencyModel resolver does not find the SDK assemblies.

Thanks, I can repro the problem. Unfortunately this is probably a bigger problem introduced by changes to how shared frameworks are packaged in 3.0. I'm trying to put together the whole story of what we changed and why, to come up with a solution.

It was known that there would be cases where more reference assemblies would need to be copied to the output. I think what's happening here is ASP.NET Core is avoiding copying those refs on the basis that it is pre-compiling views. If you were to use the runtime compilation package, or set PreserveCompilationReferences=true, I believe it would work as in 2.2. Looking for relevant SDK issue to link.

PreserveCompilationReferences=true does not work

I will take a look at that.

See https://github.com/dotnet/cli/issues/10502 for some discussion on this.

cc @pranavkm

I just tried with adding

    <PreserveCompilationReferences>true</PreserveCompilationReferences>

Into the project and now it seems to work.
Note that once you build it you should now see a refs folder in the output.

On an Asp .Net core project it does not work. But adding :
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
Make it works

Not sure what you mean by ASP.NET core project - I tried it on a the project I get by running dotnet new web.

Possibly an incremental build issue when PreserveCompilationReferences is changed? I believe that is what that package does (sets that same bool), possibly the new package ref forced the build to be less incremental?

My bad, I was refering to the PreserveCompilationContext parameters. Thanks a lot, it works !

Was this page helpful?
0 / 5 - 0 ratings