2017-08-03 116 views
1

我想使用單獨的項目來存儲我的測試應用程序的控制器。由於ASP.NET Core的工作原理,您需要將services.AddMvc().AddApplicationPart與您要添加的組件聯繫起來。ASP.NET Core MVC主項目無法在獨立程序集中訪問控制器

我用的Visual Studio 2017年(所以沒有更多的project.json有)

問題是我不能達到我想包括大會!

我在我的項目有補充參考:

enter image description here

另外,我決定使用填充工具對AppDomian像這樣(到達組裝我需要):

public class AppDomain 
    { 
     public static AppDomain CurrentDomain { get; private set; } 

     static AppDomain() 
     { 
      CurrentDomain = new AppDomain(); 
     } 

     public Assembly[] GetAssemblies() 
     { 
      var assemblies = new List<Assembly>(); 
      var dependencies = DependencyContext.Default.RuntimeLibraries; 
      foreach (var library in dependencies) 
      { 
       if (IsCandidateCompilationLibrary(library)) 
       { 
        var assembly = Assembly.Load(new AssemblyName(library.Name)); 
        assemblies.Add(assembly); 
       } 
      } 
      return assemblies.ToArray(); 
     } 

     private static bool IsCandidateCompilationLibrary(RuntimeLibrary compilationLibrary) 
     { 
      return compilationLibrary.Name == ("TrainDiary") 
       || compilationLibrary.Dependencies.Any(d => d.Name.StartsWith("TrainDiary")); 
     } 
    } 

但是,當我使用它在列表中只有一個程序集(只鏈接到這個項目本身,而不是一個與控制器)。

這裏是我的Startup.cs

public class Startup 
    { 

     public IConfigurationRoot Configuration { get; } 
     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 

      Configuration = builder.Build(); 
     } 
     // This method gets called by the runtime. Use this method to add services to the container. 
     // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 
     public void ConfigureServices(IServiceCollection services) 
     { 
      var assemblies = GetAssemblies(); // I tought I will find there Assmbly I need but no 

      services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
      services.AddReact(); 
      services.AddMvc(); 
      services.AddLogging(); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
      } 

      app.UseReact(config => 
      { 
      }); 

      app.UseStaticFiles(); 
      app.UseMvcWithDefaultRoute(); 
     } 

     public List<Assembly> GetAssemblies() 
     { 
      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 
      List<Assembly> currentAssemblies = new List<Assembly>(); 

      foreach (Assembly assembly in assemblies) 
      { 
       if (assembly.GetName().Name.Contains("TrainDiary")) 
       { 
        currentAssemblies.Add(assembly); 
       } 
      } 

      return currentAssemblies; 
     } 

    } 

我怎樣才能得到我的項目也查找控制器中的其他項目?爲什麼不能看到它?

UPD

試圖直接讓想在這個exapmle這裏。

所以我的代碼開始看起來像這樣:

var controllersAssembly = Assembly.Load(new AssemblyName("TrainDiary.Controllers")); 
services.AddMvc() 
     .AddApplicationPart(controllersAssembly) 
     .AddControllersAsServices(); 

它成功獲得大會:

enter image description here

但是當我嘗試打電話給控制器的功能 - 它失敗!

控制器代碼:

[Route("Login")] 
    public class LoginController: Controller 
    { 
     [Route("Login/Test")] 
     public void Test(string name, string password) 
     { 
      string username = name; 
      string pass = password; 
     } 
    } 

要求:

enter image description here

UPD2

出於某種原因需要刪除Routes幫助:

public class LoginController: Controller 
    { 
     public void Test(string name, string password) 
     { 
      string username = name; 
      string pass = password; 
     } 
    } 

enter image description here

回答

2

所以,如果和這件事:

1)您需要Add Reference您與 控制器單獨的項目。

2)配置服務(部分):

public void ConfigureServices(IServiceCollection services) 
{ 
    var controllersAssembly = Assembly.Load(newAssemblyName("SomeProject.MeetTheControllers")); 

    services.AddMvc().AddApplicationPart(controllersAssembly).AddControllersAsServices(); 
} 

3)我的配置看起來像(注意UseMvcWithDefaultRoute):

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
      } 

      app.UseReact(config => 
      { 
      }); 

      app.UseStaticFiles(); 
      app.UseMvcWithDefaultRoute(); 
     } 
在我的控制器的情況下

所以這樣你不此處不需要輸入Routes屬性,只需撥打Some/Test url

namespace SomeProject.MeetTheControllers.Test 
{ 
    using Microsoft.AspNetCore.Mvc; 

    public class SomeController: Controller 
    { 
     public void Test(string name, string notname) 
     { 
      string username = name; 
      string nan = notname; 
     } 
    } 
} 
相關問題