ASP.NET MVC: Views using a model type that is loaded by MEF can't be found by the view engine

前端 未结 2 901
天命终不由人
天命终不由人 2021-02-13 21:06

I\'m attempting to create a framework for allowing controllers and views to be dynamically imported into an MVC application. Here\'s how it works so far:

  • I\'m usin
相关标签:
2条回答
  • 2021-02-13 21:45

    Based on Update 2, I'm guessing what you've got is an explicitly loaded copy of your assembly (that is, it was loaded through some other method than Load, like LoadFrom). Explicitly loaded assemblies are set off aside into a special place, because they are not allowed to satisfy implicit type requirements. The rules for Fusion (the assembly loader) can be pretty arcane and hard to understand.

    I agree with Matthew's assessment that, to get this to work, your DLL is going to have to be in /bin or else it will never be able to satisfy the implicit type requirement.

    0 讨论(0)
  • 2021-02-13 21:57

    The imported libaries aren't in the /bin directory so aren't probed when trying to resolve references. I discovered a work around which I published in my MVC + MEF article (Part 2). Essentially you need to add your directories where your extensions sit to the probing path of the AppDomain.

    Essentially where I am building my container:

    /// <summary>
    /// Creates the composition container.
    /// </summary>
    /// <returns></returns>
    protected virtual CompositionContainer CreateCompositionContainer()
    {
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog(MapPath("~/bin")));
    
        var config = CompositionConfigurationSection.GetInstance();
        if (config != null && config.Catalogs != null) {
            config.Catalogs
                .Cast<CatalogConfigurationElement>()
                .ForEach(c =>
                {
                    if (!string.IsNullOrEmpty(c.Path)) {
                        string path = c.Path;
                        if (path.StartsWith("~"))
                            path = MapPath(path);
    
                        foreach (var directoryCatalog in GetDirectoryCatalogs(path)) {
    
                            // Register our path for probing.
                            RegisterPath(directoryCatalog.FullPath);
    
                            // Add the catalog.
                            catalog.Catalogs.Add(directoryCatalog);
                        }
                    }
                });
        }
    
        var provider = new DynamicInstantiationExportProvider();
        var container = new CompositionContainer(catalog, provider);
        provider.SourceProvider = container;
    
        return container;
    }
    

    I register all the directories of catalogs in the current domain:

    /// <summary>
    /// Registers the specified path for probing.
    /// </summary>
    /// <param name="path">The probable path.</param>
    private void RegisterPath(string path)
    {
        AppDomain.CurrentDomain.AppendPrivatePath(path);
    }
    

    I believe the same should work for MVC3.

    UPDATE: Correct me if I am wrong, but I don't believe that ViewEngines are instantiated once per request, you create a single instance that you register with MVC. Because of this only one IModule instance is ever used with your ViewEngine, so if a path doesn't match that first IModule.Name it won't be found? Does that make sense?

    0 讨论(0)
提交回复
热议问题