所以在我原來的職位,我得到了一個編譯錯誤在線「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動態地選取它們。
沒有骰子......我在我的單元測試中得到了404的WebApi。 – chdev77
404與Autofac註冊有什麼關係? – Nathan
404 Not Found ...查看[link](http://en.wikipedia.org/wiki/HTTP_404)。這意味着它沒有正確註冊。如果我將控制器和類型移動到相同的位置。作爲我的單元測試通過的WebApi,如果我將它們移動到單獨的assm中。並反映類型,它不工作。正如我所說,我是AutoFac的新手。也許你可以詳細闡述一下,好嗎? – chdev77