2017-08-01 10 views
0

我試圖在編譯和導入的MEF模塊中以我自己創建的模塊目錄中的.dll文件的形式訪問視圖。ASP MVC無法從導入的MEF模塊中查看視圖

在編寫MEF dll之後,我嘗試導航到已配置的模塊URL,以檢查模塊及其控件是否已正確導入,並且控件正試圖獲取正確的ActionView但問題是.cshtml無法找到文件是我在想什麼。

我也有一個customViewEngine,根據指定的文件夾處理模塊視圖的映射。

爲customViewEngine

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 
using Dependency_Injection_MEF_MVC.Components; 

public class CustomViewEngine : RazorViewEngine 
{ 
    private List<string> _plugins = new List<string>(); 

    public CustomViewEngine(List<string> pluginFolders) 
    { 
     //_plugins = pluginFolders; 

     DirectoryInfo d = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules/temp/"));//Assuming Test is your Folder 
     FileInfo[] Files = d.GetFiles("*.dll"); //Getting Text files 
     foreach (FileInfo file in Files) 
     { 
      _plugins.Add(file.Name.Substring(0, file.Name.IndexOf('.'))); 
     } 


     ViewLocationFormats = GetViewLocations(); 
     MasterLocationFormats = GetMasterLocations(); 
     PartialViewLocationFormats = GetViewLocations(); 
    } 

    public string[] GetViewLocations() 
    { 
     var views = new List<string>(); 
     views.Add("~/Views/{0}"); 

     _plugins.ForEach(plugin => 
      views.Add("~/Modules/temp/" + plugin + "/Views/Home/{0}") 
     ); 

     return views.ToArray(); 
    } 

    public string[] GetMasterLocations() 
    { 
     var masterPages = new List<string>(); 

     masterPages.Add("~/Views/Shared/{0}.cshtml"); 

     _plugins.ForEach(plugin => 
      masterPages.Add("~/Modules/temp/" + plugin + "/Views/Shared/{0}.cshtml") 
     ); 

     return masterPages.ToArray(); 
    } 
} 

我得到這個錯誤,分析它,我可以看到,其檢查的最後兩個地方是它應該駐留,但視圖引擎無法找到它,即使它是存在的代碼。

enter image description here

所以TL:博士

創建sperate項目,它們組成爲一個主要項目MEF部分,作曲之後,我試圖通過導航到模塊的控制器的網頁來測試它在哪裏我迎接上面的錯誤。

回答

0

這與DLL文件鎖定有關,本質上我只需創建模塊dll的影子副本,它應該允許您訪問所有內容。

這是我使用的一段代碼,我在網上找到並完美地工作。可能必須更改項目中的路徑定義。

private static void genShadowCopy(List<string> pluginFolders){ 



    DirectoryInfo PluginFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/Modules/"); 


    DirectoryInfo ShadowCopyFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/Modules/temp")); 

    String shadowFullPath = ShadowCopyFolder.FullName; 

    Directory.CreateDirectory(ShadowCopyFolder.FullName); 
    //clear out old plugins in previous shadowcopyfolder) 
    foreach (var f in ShadowCopyFolder.GetFiles("*.dll", SearchOption.AllDirectories)) 
    { 
     f.Delete(); 
    } 
    //shadow copy files 
    foreach (var plug in PluginFolder.GetFiles("*.dll", SearchOption.AllDirectories)) 
    { 
     var di = Directory.CreateDirectory(ShadowCopyFolder.FullName); 
     // NOTE: You cannot rename the plugin DLL to a different name, it will fail because the assembly name is part if it's manifest 
     // (a reference to how assemblies are loaded: http://msdn.microsoft.com/en-us/library/yx7xezcf) 

     String dif = di.FullName + plug.Name; 

     File.Copy(plug.FullName, Path.Combine(di.FullName, plug.Name), true); 

    }