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.

Attempting to run xunit integration test results in "Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.Reference" exception ( on 3.0.100-preview8-013596)

See original GitHub issue

From @ossentoo on Tuesday, July 30, 2019 2:04:21 PM

HI,

I’ve upgraded a project from v2.2 sdk to v3.0 sdk. This is because we want to use some of the new functionality in Microsoft.Data.SqlClient. and other web app features.

The web app runs fine (bar one or two problems that we are still investigating).

However, most of our web app integration tests are failing with the following exception:

System.InvalidOperationException : Cannot find reference assembly ‘Microsoft.AspNetCore.Antiforgery.dll’ file for package Microsoft.AspNetCore.Antiforgery.Reference at Microsoft.Extensions.DependencyModel.Resolution.ReferenceAssemblyPathResolver.TryResolveAssemblyPaths(CompilationLibrary library, List1 assemblies) at Microsoft.Extensions.DependencyModel.Resolution.CompositeCompilationAssemblyResolver.TryResolveAssemblyPaths(CompilationLibrary library, List1 assemblies) at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List1 assemblies) at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths() at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions.<>c.<GetReferencePaths>b__0_0(CompilationLibrary library) at System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext() at System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 collection) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths() at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.get_CompilationReferences() at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.LazyMetadataReferenceFeature.get_References() at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors() at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument) at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument) at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document) at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument) at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)

Here’s dotnet info

$ dotnet --info .NET Core SDK (reflecting any global.json): Version: 3.0.100-preview8-013596 Commit: 425adefb59 Runtime Environment: OS Name: Windows OS Version: 10.0.14393 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview8-013596\ Host (useful for support): Version: 3.0.0-preview8-28379-02 Commit: 3a87ac0079 .NET Core SDKs installed: 1.0.4 [C:\Program Files\dotnet\sdk] 2.1.601 [C:\Program Files\dotnet\sdk] 2.2.100 [C:\Program Files\dotnet\sdk] 3.0.100-preview7-012821 [C:\Program Files\dotnet\sdk] 3.0.100-preview8-013596 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview7.19365.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview8.19377.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview8-28379-02 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.0.0-preview8-28379-02 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download

image

We are using a WebServerBase test startup class like this:

` public class WebServerBase : IDisposable { public HttpClient Client;

    public WebServerBase()
    {

        var integrationTestsPath = PlatformServices.Default.Application.ApplicationBasePath;
        var applicationPath = Path.GetFullPath(Path.Combine(integrationTestsPath, "../../../../../application.web"));

        var server = new TestServer(new WebHostBuilder()

// .UseApplicationInsights() .UseStartup<TestStartup>() .UseConfiguration(new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(“appsettings.json”) .Build() ) .UseContentRoot(applicationPath)) { BaseAddress = new Uri(“https://localhost”) };

        Client = server.CreateClient();

        // ... initialize data in the test database ...
    }

    public void Dispose()
    {
        Client.Dispose();
        Client = null;
    }
}`

TestStartup has an override method to be used by tests only that looks like this:

` protected override void ConfigureMvc(IServiceCollection services) { var assembly = Assembly.Load(new AssemblyName(“application.web”));

        services.AddMvc(opts =>
        {
            opts.Filters.Add(new AllowAnonymousFilter());
        })
        .AddRazorRuntimeCompilation()
        .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver()) 
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
        .AddApplicationPart(assembly); 
    }`

I noticed that the app.UseMvc call is no longer being made for v3.0 apps. However, for tests, I can’t seem to remove AddMvc because if I do, I get a 404 response when a call is made to any controller action.

As you can see, I added AddRazorRuntimeCompilation to AddMvc because otherwise, another error was raised: The view ‘Index’ was not found.

As documented here: https://github.com/aspnet/AspNetCore/issues/8783

It seems Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation does have Microsoft.AspNetCore.Antiforgery as a dependency, but can’t find out for some reason. In checking the bin/Debug/netcoreapp3.0 directory for the tests project, no assembly called Microsoft.AspNetCore.Antiforgery.dll is found. At first I thought this was because dotnet was pulling this from the nuget packages directory (where it is present). However, even if I try and force the assembly to the output directory by adding this statement to the csproj file:

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

it still doesn’t appear.

I can provide access to the source if required via one of our private github repos if review is required.

Copied from original issue: dotnet/core-sdk#3603

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
livarcocccommented, Jul 30, 2019

From @livarcocc on Tuesday, July 30, 2019 2:38:59 PM

Moving this to asp.net for them to have a first look. If this is indeed on the SDK, just let us know.

0reactions
ossentoocommented, Aug 1, 2019

thanks a lot @pranavkm . I removed the razor compilation package and added the changes you suggested. The test now works.

Please close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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