Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Embedded views problem when hosting in console app #6178

Closed
oguzhaneren opened this issue Apr 21, 2017 · 2 comments
Closed

Embedded views problem when hosting in console app #6178

oguzhaneren opened this issue Apr 21, 2017 · 2 comments

Comments

@oguzhaneren
Copy link

oguzhaneren commented Apr 21, 2017

In that repo https://github.com/oguzhaneren/birsey, I created 3 projects:

App1 - Aspnet core class library
ClassLibrary1 - dotnet core class library (includes views)
ConsoleApp1 - dotnet core console application (hosting App1)

When I start project from ConsoleApp1, embedded views not working, throws exception:

 fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: One or more compilation failures occurred:
      jm5mnrtm.ru3(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
      jm5mnrtm.ru3(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
      jm5mnrtm.ru3(12,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
      jm5mnrtm.ru3(15,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
      jm5mnrtm.ru3(16,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
      jm5mnrtm.ru3(18,86): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?

When I start from App1 (after converted to Console App) its working.

@Eilon
Copy link
Member

Eilon commented May 13, 2017

I'm guessing this is the same/similar issue as #6021.

I think the problem is that because view pre-compilation is turned on by default for web projects, the reference assemblies that would be needed by Razor to compile the embedded views are not included anymore, so all runtime compilation will fail.

I think in https://github.com/oguzhaneren/birsey/blob/master/App1/App1.csproj#L12-L15 you need to add <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>, which will make the reference assemblies available to the app for compilation.

Can you let us know if that works for you?

@pranavkm
Copy link
Contributor

@oguzhaneren Mvc looks for the deps file that matches the application name - in your case App1.deps.json to locate references to compile views with. However, nothing places it in the output directory of the executing application viz. ConsoleApp1. Explicitly copying the file fixes the compilation. Here's one way to go about it:

 <!-- ConsoleApp1.csproj -->

<Target Name="CopyDepsFile" AfterTargets="CopyFilesToOutputDirectory">
  <ItemGroup>
    <DepsFile Include="..\App1\bin\$(Configuration)\netcoreapp1.1\App1.deps.json" />
  </ItemGroup>

  <Copy SourceFiles="@(DepsFile)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
</Target>

Feel free to reopen if you still continue seeing this issue after this change.

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

No branches or pull requests

3 participants