Mvc: Can't find in MVC View @using namespace from different assembly

Created on 20 May 2016  路  14Comments  路  Source: aspnet/Mvc

I'm using RC2 bits and been getting error on @using in View that namespace not found, i have portable library in my solution and reference it in AspNetCore project, everything compiles, but when i run I get:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
/Views/Account/Login.cshtml(4,19): error CS0234: The type or namespace name 'Models' does not exist in the namespace '...' (are you missing an assembly reference?)
1klvb3fe.wbm(98,30): error CS0246: The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)
   at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
   at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String normalizedPath, Func`2 compile)

Any ideas, maybe need more configuration for this case?

external

Most helpful comment

Does your project have preserveCompilationContext set to true: https://github.com/aspnet/Mvc/blob/dev/samples/MvcSandbox/project.json#L5?

All 14 comments

Does your project have preserveCompilationContext set to true: https://github.com/aspnet/Mvc/blob/dev/samples/MvcSandbox/project.json#L5?

I'm having a similar and possibly the same problem. Also using RC2 bits. I've got an ASP.NET Core Web Application using the .Net Framework 4.6. I'm referencing a "standard" Class Library that is also using the .Net Framework 4.6.The Everything compiles fine but when I run I get the following:

The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?)
3.@using ClassLibrary1.Models

If I move the class is inside my Web Application project I don't get an error.

@pakrym - I have preserveCompilationContext set to true in my Web Application project.

/cc @abpiskunov

From my project.json:

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  }

And my application uses full .Net Framework 4.6.1 as in @bhalkett case.

Notice that in RC2 there was a bug that did not copy csproj dlls under xproj output folder after build (for some target frameworks including netcoreapp/netstandard). Since razor files are dynamically compiled, at runtime reference is missed. It does not fail in VS since references are resolved via project dependencies.

See issue https://github.com/dotnet/cli/issues/2961 for reference. It was fixed in post RC2, but is still in progress since it still does not copy pdb files.

@abpiskunov -So is there a current workaround or do we wait for Preview2 bits before we can get this working?

@abpiskunov referenced other issues about this problem and if understand correctly there is a bug in preview1 and it will be fixes in preview2. Current workaround is add this code to Startup.cs:

``` C#
services.AddMvc()
.AddRazorOptions(options =>
{
var previous = options.CompilationCallback;
options.CompilationCallback = context =>
{
previous?.Invoke(context);

                    context.Compilation = context.Compilation.AddReferences(MetadataReference.CreateFromFile(typeof(MyClass).Assembly.Location));
                };
            })

```

@Centurys - That worked! Thank you very much! However, I've reviewed the other referenced issue but could not find that workaround. Where did you find it?

No problem ;) I was ready to do more searching because tools are only preview1.
At first I find needed code from references issues comments text, github and searching, but after awhile somebody did add sample add this issue https://github.com/aspnet/Razor/issues/755.

@abpiskunov, @davidfowl should I close this issue or you will close it yourself when you done with all necessary fixes in preview2?

BTW I set up a quick mtg for Monday to discuss this.

Turns out this is a dup of https://github.com/aspnet/Razor/issues/755. This is being fixed by @pakrym in .NET CLI.

For info, if you see this in an upgraded csproj file, you'll need to add the following manually to your csproj file:

<PreserveCompilationContext>true</PreserveCompilationContext>
<OutputType>Exe</OutputType>

You won't by RTM, those will be automatic

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nefcanto picture nefcanto  路  3Comments

CezaryRynkowski picture CezaryRynkowski  路  4Comments

michaelbowman1024 picture michaelbowman1024  路  3Comments

workmonitored picture workmonitored  路  3Comments

KLuuKer picture KLuuKer  路  3Comments