DynamicExtensionLoader fails in Compile with using System.ComponentModel.DataAnnotations

Topics: Troubleshooting
Aug 21, 2014 at 1:00 PM
Hi everybody,

Orchard v1.7.2.0.
Dynamic compilation fails in SignUpViewModel.cs from Sipke's samples...

2014-08-21 13:46:09,986 [6] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Start loading dynamic extension "GoldClub.Market"
2014-08-21 13:46:09,986 [5] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Done loading dynamic extension "Workflows": assembly name="App_Web_cuujp1kh, Version=1.7.2.0, Culture=neutral, PublicKeyToken=null"
2014-08-21 13:46:10,001 [5] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Start loading dynamic extension "Roles"
2014-08-21 13:46:10,173 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Generate code for file "/Orchard/Modules/GoldClub.Market/GoldClub.Market.csproj"
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Loading reference 'Ionic.Zip.Reduced'
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Done loading reference 'Ionic.Zip.Reduced'
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Forms'
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Projections'
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Tokens'
2014-08-21 13:46:10,267 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Users'
2014-08-21 13:46:11,220 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Generate code for file "/Orchard/Modules/GoldClub.Market/GoldClub.Market.csproj"
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Loading reference 'Ionic.Zip.Reduced'
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Loaders.DynamicExtensionLoader - Done loading reference 'Ionic.Zip.Reduced'
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Forms'
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Projections'
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Tokens'
2014-08-21 13:46:11,439 [6] Orchard.Environment.Extensions.Compilers.DefaultExtensionCompiler - Loader 'ReferencedExtensionLoader' could not load reference 'Orchard.Users'
2014-08-21 13:46:12,407 [6] Orchard.Environment.DefaultBuildManager - Error when compiling assembly under ~/Modules/GoldClub.Market/GoldClub.Market.csproj.
System.Web.HttpCompileException (0x80004005): d:\www\Orchard\Modules\GoldClub.Market\ViewModels\SignupViewModel.cs(24): error CS0246: The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?)
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetCompiledAssembly(String virtualPath)
at Orchard.Environment.DefaultBuildManager.GetCompiledAssembly(String virtualPath) in C:\Orchard\Orchard.Source.1.7.2\src\Orchard\Environment\IBuildManager.cs:line 53
2014-08-21 13:46:12,439 [6] Orchard.Environment.Extensions.ExtensionManager - No suitable loader found for extension "GoldClub.Market"

Now, I've added the System.ComponentModel.DataAnnotations asembly in the compilation/assemblies section of web.config. No go, same error.
What's more if I open the project in VS and compile it there it successfully does it and then Orchard loads the module by PreCompiled loader.
If I remove DataAnnotations from SignUpViewModel.cs(comment out attributes and Validations) then dynamic compilation is successful and it loads the module.

Any idea why ASP.NET has troubles with System.ComponentModel.DataAnnotations or whatever is causing dynamic compilation to fail?
Developer
Aug 21, 2014 at 11:22 PM
I've never been able to figure that one out. I think it's some sort of versioning issue.
Aug 22, 2014 at 8:08 AM
Hello Sipke,
I've catched the exception in the debugger and there are two errors from the compiler, Orchard logs only the first one(the one above). The second one is this:
CS0104: 'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

If I check System.ComponentModel.DataAnnotations in VS it does not have a CompareAttribute. My guess is dynamic compilation loads a different DataAnnotations when compiling. I'll try to catch what is happening with fuslogvw...
Aug 22, 2014 at 8:38 AM
Edited Aug 22, 2014 at 8:41 AM
Try to use the attribute like following:
[System.ComponentModel.DataAnnotations.Compare("ToCompare")]
This of course is a bit of a work-around till the dynamic compilation issue is solved.
Aug 22, 2014 at 11:31 AM
Ok, I gave up trying to find why it(System.Web.Compilation) fails. I haven't found any clues in fusion logs(it looks like the correct libs get loaded).

This helped:

http://stackoverflow.com/questions/11461911/compareattribute-isnt-found-when-building-in-teamcity-on-windows-server-2012-r

sanderg, if I qualify with System.ComponentModel.DataAnnotations it gives me error in VS, since the Compare attribute does not exist in the referenced assembly. That's the weird stuff when it goes compiling dynamically.

Regards.
Jan 3, 2015 at 10:47 AM
Edited Jan 3, 2015 at 10:56 AM
I have managed to solve this issue.

Instead of doing this:
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace TestNamespace
{
    public class Test
    {
        [StringLength(255), Required, DataType(DataType.Password), Display(Name = "Password")]
        public string Password { get; set; }

        [StringLength(255), Required, DataType(DataType.Password), Compare("Password"), Display(Name = "Repeat password")]
        public string RepeatPassword { get; set; }
    }
}
I removed the System.Web.Mvc using.So I set explitly the namespace for Compare annotation (System.Web.Mvc instead of System.ComponentModel.DataAnnotations):
using System.ComponentModel.DataAnnotations;
//using System.Web.Mvc;

namespace TestNamespace
{
    public class Test
    {
        [StringLength(255), Required, DataType(DataType.Password), Display(Name = "Password")]
        public string Password { get; set; }

        [StringLength(255), Required, DataType(DataType.Password), System.Web.Mvc.Compare("Password"), Display(Name = "Repeat password")]
        public string RepeatPassword { get; set; }
    }
}
Regards :)