MVC View get's compilation error (CS0246) when referenced assembly built as console application. When it built as Class Library - it works fine

StackOverflow https://stackoverflow.com/questions/21616799

Question

I have a solution. First project contains entity framework models, output type is Console application. Other project is frontend, asp.net mvc 5 application that have CRUD views for entities from the first project. First project referenced from the second project, also I added dependent assembly in web.config:

<dependentAssembly>
 <assemblyIdentity name="VolLoader" publicKeyToken="null" />
</dependentAssembly>

But when I access view that uses entity from this assembly, I get view compilation error:

Compiler Error Message: CS0246: The type or namespace name 'VolLoader' could not be found (are you missing a using directive or an assembly reference?)

Source Error:



Line 29:     
Line 30:     
Line 31:     public class _Page_Views_JobMon_Index_cshtml : System.Web.Mvc.WebViewPage<IEnumerable<VolLoader.Data.JobToWatch>> {
Line 32:         
Line 33: #line hidden

Adding namespace () to the web.config in views folder causes compilation errors on every view.

Assembly is copied to output.

But when I change Output type of the first project to the Class Library, it works, no compilation errors. I can't understand why? Does anyone has ideas?

Was it helpful?

Solution

I think I found why this is happening. I'm not 100% sure, but I can't recompile system.web.dll to prove it. I found this code in system.web.dll:

namespace System.Web.Compilation
{
    /// <summary>Provides a container for building an assembly from one or more virtual paths within an ASP.NET project.</summary>
    public class AssemblyBuilder
......
}

Line 381:

internal CompilerParameters GetCompilerParameters()
{
    CompilerParameters compilerParameters = this._compilerType.CompilerParameters;
    string text = this._tempFiles.TempDir;
    if (this.CultureName != null)
    {
        text = Path.Combine(text, this.CultureName);
        Directory.CreateDirectory(text);
        compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".resources.dll");
    }
    else
    {
        compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".dll");
    }

As you can see, it takes only dlls:

compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".dll");

OTHER TIPS

Most likely that your first project can not be built as a console application (Do you have static Main method?. And because first project can not be built, second project fails to build because missing references - i.e. missing assembly for the first project.

Why do you want your EF models be contained within console application?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top