Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Module-based application: ViewModels and Views in separate project

Open t0xicDen opened this issue 9 years ago • 7 comments

Introduction

I am trying to develope module-based architecture. I have WebHost application and Modules folder inside. I can put any module inside this folder and it will be dynamically added on startup using mvcBuilder.AddApplicationPart(module.Assembly)

If my module consists from one project (.dll) like SomeModule.dll and this module contains ViewModels, Views etc inside themselve it works fine.

But in situation when one my module consists from multiple assemblies, like SomeModule.dll, SomeModule.ViewModel.dll, SomeModule.Domain.dll I got error

FileNotFoundException: Could not load file or assembly 'EnergySuite.OrganizationWeb.Module.Organization.ViewModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Не вдалося знайти вказаний файл. System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)

PS: File not found.

Configuration

All projects targets

  "frameworks": {
    "net46": {
      "dependencies": {}
    }

For ASP NET Core projects i tried to use 1.1 versions and latest builds, like

"Microsoft.AspNetCore.Mvc": "1.2.0-preview1-23142",
"Microsoft.AspNetCore.Mvc.Razor": "1.2.0-preview1-23142",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.2.0-preview1-23142",

ASP.NET Core 1.1

dotnet --version 1.0.0-preview2-1-003177

Additional references adding

I tried to register additional assemblies in three ways:

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new ModuleViewLocationExpander());
            options.CompilationCallback = (context) =>
            {
                var modulesToAdd = _modules.Select(m => MetadataReference.CreateFromFile(m.Assembly.Location));
                context.Compilation = context.Compilation.AddReferences(modulesToAdd);
            };
        });

and

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new ModuleViewLocationExpander());
            var metadataReferences = _modules.Select(m => MetadataReference.CreateFromFile(m.Assembly.Location));
            foreach (var reference in metadataReferences)
            {
                options.AdditionalCompilationReferences.Add(reference);
            }
        });

and both

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new ModuleViewLocationExpander());

            var metadataReferences = _modules.Select(m => MetadataReference.CreateFromFile(m.Assembly.Location));
            foreach (var reference in metadataReferences)
            {
                options.AdditionalCompilationReferences.Add(reference);
            }

            options.CompilationCallback = (context) =>
            {
                var modulesToAdd = _modules.Select(m => MetadataReference.CreateFromFile(m.Assembly.Location));
                context.Compilation = context.Compilation.AddReferences(modulesToAdd);
            };
        });

But nothing helps. I will try to prepare some repository to reproduce. Coming soon. Any suggestions?

t0xicDen avatar Jan 17 '17 16:01 t0xicDen

The 1.2.0 packages (which is what you're using) require using a newer CLI than the one you're currently using. I'd recommend either switching to our publicly released bits (version 1.1.0) if you'd prefer working with stable bits.

Could you include more of the stack trace? It's not entirely clear if it's view compilation that's throwing the exception.

pranavkm avatar Jan 17 '17 18:01 pranavkm

@pranavkm, Repository with issue https://github.com/t0xicDen/ModuleBased

t0xicDen avatar Jan 18 '17 09:01 t0xicDen

@pranavkm assigning to you to investigate.

Eilon avatar Jan 19 '17 18:01 Eilon

It works when I switched to netstandard16 and using this

                try
                {
#if NET451
                    assembly = Assembly.LoadFrom(file.FullName);
#else
                    assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
#endif
                }
                catch (FileLoadException)
                {
                    // Get loaded assembly
                    assembly = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(file.Name)));
                } 

That means that all works fine when we using System.Runtime.Loader

@pranavkm, please, investigate as fast as you can.

t0xicDen avatar Jan 24 '17 11:01 t0xicDen

After 8,5 days of researching I found something.

Based on my previous comment and this https://github.com/aspnet/Announcements/issues/149 I made conclusion that .NET Core and .NET Framework have different behavior in assembly loading context.

.NET Framework (Assembly class) searching referenced assemblies of runtime loaded assemblies in root bin directory. .NET Core (AssemblyLoadContext class) - searhing in some registered path, eg bin/Modules/SomeModule/

As a temporary solution:

public List<Assembly> ModulesAssemblies => _modules.Select(m => m.Assembly).ToList();

public void ConfigureServices(IServiceCollection services)
{
   ...
   AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
}

private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
   return ModulesAssemblies.FirstOrDefault(a => a.FullName == args.Name);
}

But I think that some bug exists.

t0xicDen avatar Jan 26 '17 13:01 t0xicDen

@t0xicDen, yes - they changed the behavior of assembly loading in CoreCLR as part of the 1.1.0 release. In essence, for Assembly.Load to succeed, the assembly must be listed in the application's deps file. Arbitrary assemblies in the application's bin directory will fail to load. We hit this issue earlier in our precompilation code and worked around it by using AssemblyLoadContext.LoadFromAssemblyPath to load the assembly from disk (https://github.com/aspnet/Mvc/pull/5371/files).

I think the CLR folks were considering expanding the set of loadable assemblies to include ones not listed in the deps file, although I can't find a work item for it. @gkhanna79 might know more about this.

pranavkm avatar Jan 26 '17 18:01 pranavkm

I think the CLR folks were considering expanding the set of loadable assemblies to include ones not listed in the deps file, although I can't find a work item for it.

There is no workitem for this since the issue in question was bug we fixed in the host where it was accidentally picking up assemblies, treating them as app closure, even if they were not in deps.json (which defines the app closure).

If the application wishes to load such assemblies, it should do so explicitly.

gkhanna79 avatar Jan 26 '17 18:01 gkhanna79