2014-01-15 94 views
0

我遇到了問題「builder.RegisterInstance(Activator.CreateInstance(repoClass));」。基本上我想反映到一個單獨的程序集來註冊我的WebApiController和類型...的即插即用架構。我是AutoFac的新手。 RegisterInstance無法解析repoClass中的repoInterface,我得到一個編譯錯誤。 「參數類型」對象「不能分配給參數類型repoClass。」我明白錯誤。我還能如何完成這個工作?AutoFac - WebApi不能註冊反射類型

由於

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

     config.EnableSystemDiagnosticsTracing(); 

     ContainerBuilder builder = new ContainerBuilder(); 

     string WebApisPath = ConfigurationManager.AppSettings["AutoFac_WebApiControllerAssembliesPath"]; 
     string titleLoanAsm = ConfigurationManager.AppSettings["WebApi_TitleLoanAssembly"]; 
     Assembly asm = Assembly.LoadFile(AssemblyDirectory.Replace("\\bin", "") + WebApisPath + titleLoanAsm); 

     builder.RegisterApiControllers(asm); 

     List<Type> exportedTypes = new List<Type>(asm.GetExportedTypes()); 

     Type repoClass = exportedTypes.FirstOrDefault(x => x.IsClass && x.Name.Contains("LGF")); 

     if (repoClass == null) 
      throw new ObjectNotFoundException(string.Format("No repositories found for controller")); 

     Type repoInterface = repoClass.GetInterfaces().FirstOrDefault(x => x.IsInterface && x.Name.Contains("LGF")); 

     if (repoInterface == null) 
      throw new ObjectNotFoundException(string.Format("Interface not found for class {0}. Ensure class has and interface implemented with a name that contains LGF", repoClass.Name)); 

     builder.RegisterInstance<repoInterface>(Activator.CreateInstance(repoClass)); 


     IContainer container = builder.Build(); 

     AutofacWebApiDependencyResolver resolver = new AutofacWebApiDependencyResolver(container); 

     config.DependencyResolver = resolver; 
    } 

回答

1

Autofac有辦法寄存器類型和具有由反射創建的實例。

builder.RegisterType(repoClass).As(repoInterface); 

更新,以解決意見

我認爲這裏的混亂可能是在初始問題的專門問你都拿到了RegisterInstance呼籲repoClass錯誤。我的回答是解決這個問題。沒有找到MVC控制器的問題是一個不同的問題,不是一個真正的autofac問題,而是一個MVC路由問題。爲了解決這個問題,您需要告訴路由系統在哪裏可以找到其他控制器。爲了解決這個問題,你需要做這樣的事情:

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = "" }, 
    new[] { "ExternalAssembly.Controllers" } 
); 

要查找該值(一個或多個),以取代「ExternalAssembly.Controllers」你要反映到你的類似於您的代碼是如何已經做外部組件並發現這些命名空間。

+0

沒有骰子......我在我的單元測試中得到了404的WebApi。 – chdev77

+0

404與Autofac註冊有什麼關係? – Nathan

+0

404 Not Found ...查看[link](http://en.wikipedia.org/wiki/HTTP_404)。這意味着它沒有正確註冊。如果我將控制器和類型移動到相同的位置。作爲我的單元測試通過的WebApi,如果我將它們移動到單獨的assm中。並反映類型,它不工作。正如我所說,我是AutoFac的新手。也許你可以詳細闡述一下,好嗎? – chdev77

1

所以在我原來的職位,我得到了一個編譯錯誤在線「builder.RegisterInstance(Activator.CreateInstance(repoClass));」這是因爲repoClass在設計時無法查看repoInterface。所以我創建了一個動態運行時類,它在運行時從repoClass和repoInterface獲取實際類型,然後編譯並執行該類以便從我的Assembly.LoadFile中返回ContainerBuilder。

public class RuntimeContainerBuilder 
{ 
    public static object Evaluate(ContainerBuilder builder, Type controller, Type repoInterface, Type repoClass, Assembly assembly) 
    { 

     CSharpCodeProvider c = new CSharpCodeProvider(); 
     ICodeCompiler icc = c.CreateCompiler(); 
     CompilerParameters cp = new CompilerParameters(); 

     cp.ReferencedAssemblies.Add("system.dll"); 
     cp.ReferencedAssemblies.Add(AssemblyDirectory + "\\System.Web.Http.dll"); 
     cp.ReferencedAssemblies.Add(AssemblyDirectory + "\\Autofac.dll"); 
     cp.ReferencedAssemblies.Add(AssemblyDirectory + "\\Autofac.Integration.WebApi.dll"); 

     cp.ReferencedAssemblies.Add(@"E:\MainTrunk2\LeadGenFramework\trunk\LeadGenFramework.Web.Api\LeadGenFramework.Web.Api.Controller.TitleLoan\bin\LeadGenFramework.Web.Api.TitleLoan.dll"); 

     cp.CompilerOptions = "/t:library"; 
     cp.GenerateInMemory = true; 

     StringBuilder sb = new StringBuilder(""); 
     sb.Append("using System;\n"); 
     sb.Append("using Autofac;\n"); 
     sb.Append("using System.Web.Http;\n"); 
     sb.Append("using System.Web.Http.Controllers;\n"); 
     sb.Append("using Autofac.Integration.WebApi;\n"); 
     sb.Append(string.Format("using {0};\n", assembly.ManifestModule.Name.Replace(".dll",""))); 

     sb.Append("namespace LeadGenFramework.Web.Api{ \n"); 
     sb.Append("public class DynamicBuild{ \n"); 
     sb.Append("public ContainerBuilder Build(ContainerBuilder obj){\n"); 
     sb.Append(string.Format("obj.RegisterApiControllers(typeof({0}).Assembly).PropertiesAutowired();\n",controller.Name)); 
     sb.Append(string.Format("obj.RegisterInstance<{0}>(new {1}());\n", repoInterface.Name, repoClass.Name)); 
     sb.Append("return obj; \n"); 
     sb.Append("} \n"); 
     sb.Append("} \n"); 
     sb.Append("}\n"); 

     string s1 = sb.ToString(); 
     CompilerResults cr = icc.CompileAssemblyFromSource(cp, s1); 
     if (cr.Errors.Count > 0) 
     { 
      //Factory.LogManager.MainProcessLogger.Info(string.Format("DynamicFormatScriptEngine:Evaluate(): Error: {0}, Code: {1}", cr.Errors[0], s1)); 
      return null; 
     } 

     Assembly a = cr.CompiledAssembly; 
     object o = a.CreateInstance("LeadGenFramework.Web.Api.DynamicBuild"); 

     Type t = o.GetType(); 
     MethodInfo mi = t.GetMethod("Build"); 

     object s = mi.Invoke(o,new object[]{builder}); 

     return s; 
    } 

現在我WebApiConfig是這樣的...看行 「RuntimeContainerBuilder.Evaluate(生成器,控制器,repoInterface,repoClass,ASM)」

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

     config.EnableSystemDiagnosticsTracing(); 

     ContainerBuilder builder = new ContainerBuilder(); 

     //string WebApisPath = ConfigurationManager.AppSettings["AutoFac_WebApiControllerAssembliesPath"]; 
     string titleLoanAsm = ConfigurationManager.AppSettings["WebApi_TitleLoanAssembly"]; 
     //Assembly asm = Assembly.LoadFile(AssemblyDirectory.Replace("\\bin", "") + WebApisPath + titleLoanAsm); 

     Assembly asm = Assembly.LoadFile(AssemblyDirectory + "\\" + titleLoanAsm); 

     List<Type> exportedTypes = new List<Type>(asm.GetExportedTypes()); 

     Type controller = exportedTypes.FirstOrDefault(t => !t.IsAbstract && typeof(ApiController).IsAssignableFrom(t)); 

     if (controller == null) 
      throw new ObjectNotFoundException(string.Format("Contoller not found in assembly {0}.", asm.FullName)); 

     Type repoClass = exportedTypes.FirstOrDefault(x => x.IsClass && x.Name.Contains("LGF")); 

     if (repoClass == null) 
      throw new ObjectNotFoundException(string.Format("No repositories found for controller")); 

     Type repoInterface = repoClass.GetInterfaces().FirstOrDefault(x => x.IsInterface && x.Name.Contains("LGF")); 

     if (repoInterface == null) 
      throw new ObjectNotFoundException(string.Format("Interface not found for class {0}. Ensure class has and interface implemented with a name that contains LGF", repoClass.Name)); 

     RuntimeContainerBuilder.Evaluate(builder, controller, repoInterface, repoClass, asm); 

     IContainer container = builder.Build(); 

     AutofacWebApiDependencyResolver resolver = new AutofacWebApiDependencyResolver(container); 
     config.DependencyResolver = resolver; 
    } 

下一步是使它更通用的方法是將程序集加載到數組中,併爲每個程序集設置RuntimeContainerBuilder ...然後builder.Build應該正確地解析和激活所有內容。這個原型的最終目標是拖放程序集並讓WebApi動態地選取它們。