Skip to content
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

FileNotFoundException: Microsoft.AspNetCore.Razor.Runtime #710

Closed
tiagodenoronha opened this issue Oct 22, 2018 · 8 comments
Closed

FileNotFoundException: Microsoft.AspNetCore.Razor.Runtime #710

tiagodenoronha opened this issue Oct 22, 2018 · 8 comments

Comments

@tiagodenoronha
Copy link

Good afternoon,

I'm having some trouble figuring out why i'm getting this specified error when mocking an aparently normal object.
The exception is
Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

I can provide a concrete test case where this happens.
https://github.com/tiagodenoronha/PlantsIdentifier/blob/feature/authentication/tests/PlantsIdentifierAPI.UnitTests/Controllers/LoginControllerTests.cs#L27

Running this test with the commented line fails.

Is it something i'm doing wrongly on my side?

Thanks!

@stakx
Copy link
Contributor

stakx commented Oct 22, 2018

This will likely be due to an assembly version conflict, or an invalid assembly binding redirect, and nothing directly related to Moq. Check if there are any warnings during the build or in the build log.Check for NuGet version conflicts & update packages where necessary. Check whether your assembly binding redirects are up-to-date & correct (if you have any at all). Possibly delete them all and configure your project to generate them automatically during build. Make sure you're using up-to-date tool versions. That's the usual suggestions I'd hand out to anyone having this kind of problem.

Closing this issue, since it is very likely unrelated to Moq. If you think this is in error, and there's something that needs to be done at Moq's end, please let us know.

@stakx stakx closed this as completed Oct 22, 2018
@tiagodenoronha
Copy link
Author

I don't have that assembly installed on my test project. Could that be it? But why would that assembly be needed for such a simple class with 5 properties?
No warnings, no nuget updates, and no assembly binding redirects :/

It's probably an error on my side, because it doesn't make sense that it has anything to do with Moq. Was just wondering if anyone had already experienced a similar issue.

@stakx
Copy link
Contributor

stakx commented Oct 23, 2018

No warnings, no nuget updates, and no assembly binding redirects :/

TokenModel appears to be defined in your main project, which references Microsoft.AspNetCore.App, which has a dependency on Microsoft.AspNetCore.Razor, which in turn depends on Microsoft.AspNetCore.Razor.Runtime. So it seems to me that the latter assembly should be around.

Perhaps try publishing your application and see what dependencies get copied to the publish/ output directory. Or check your NuGet package cache to see if the package (in the right version) was at least downloaded from NuGet.

Sometimes the "Consolidate NuGet packages" dialog in VS (accessible via the context menu on the solution node in Solution Explorer) gives hints about possible NuGet version conflicts.

Perhaps also increase the build verbosity level to see if you have missed any messages.

But why would that assembly be needed for such a simple class with 5 properties?

I cannot tell, to be honest. The problem doesn't seem to lie with TokenModel itself, since your method already successfully references that type now (new TokenModel()), and that type has only simple properties with primitive types which makes it seem unlikely that JIT-compiling this type would cause the JIT to look for any ASP.NET Core assembly.

(That being said, Mock.Of<T>() does perform a whole lot more work than new T(), in particular it executes a mock.SetupAllProperties(), meaning it'll visit all properties in your type and cause these properties' types to be JIT-compiled if needed) — if you still suspect there's something inside Moq going wrong, you could try to disable VS' "Just My Code" setting and step into the Mock.Of<TokenModel>() call (Moq comes with SourceLink enabled) to see how far execution gets.)

If this turns out to be a problem with Moq after all, I am more than happy to reopen this issue.

@tiagodenoronha
Copy link
Author

Even with the publish the dll doesn't appear in the folder, which makes it even stranger...
It's most likely a problem on my side, just can figure out what :)
Thanks anyways!

@stakx
Copy link
Contributor

stakx commented Oct 26, 2018

@tiagodenoronha - I cloned your repository and ran the unit tests. They all pass. So yes, I'd say this is a problem with your setup or tooling.

@tiagodenoronha
Copy link
Author

Well I installed Microsoft.AspNetCore.Razor.Runtime and aparently it now works... Weird..

@andrewtheart
Copy link

I am still getting an error message

"System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Razor.Parser.ParserBase' from assembly 'Microsoft.AspNetCore.Razor, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'"

I included all of the following packages:

Microsoft.AspNetCore.Razor
Microsoft.AspNetCore.Razor.Runtime
RazorEngine
System.Configuration.ConfigurationManager

@Sword-Smith
Copy link

Sword-Smith commented Aug 12, 2019

I had a controller that was returning an anonymous class and in a xunit test I wanted to check its value. Following this answer, I set the return value to dynamic (wrapped in an OkObjectResult in an IActionResult) and encountered the above error in my unit test project:

FileNotFoundException: Microsoft.AspNetCore.Razor.Runtime

Manually installing Microsoft.AspNetCore.Razor.Runtime v. 2.2.0 fixed the problem. But dotnet should have informed me about a missing package at compile time and not at runtime, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants