我有两种执行程序(控制台)的方法,作为客户端调用服务或作为服务器调用自己的本地服务。第一种方式是Autofac初始化非常快,因为只注册两个模块,服务器方式较慢,因为Autofac必须初始化所有服务器模块(超过1分钟)。执行方式取决于控制台的参数。
等待更好的解决方案我已经在控制台模块上实现了它,在必要时加载服务器模块,但它有点硬编码(从文件中提取模块和配置/属性) ,因为我已将它们配置为混合文件和模块(下面的示例),我会将配置保留在文件中。
我想动态选择文件:
<autofac configSource="autofac-client.config" />
或
<autofac configSource="autofac-server.config" />
CODE:
autofac-client.config:
<autofac defaultAssembly="Console1">
<files>
<file name=".\BaseSettings\ClientServices.config" section="clientServices" />
</files>
<modules>
<module type="Console1.AutofacModules.ComponentModule, Console1" />
</modules>
</autofac>
autofac-server.config:
<autofac defaultAssembly="Console1">
<files>
<file name=".\BaseSettings\ClientServices.config" section="clientServices" />
<file name=".\BaseSettings\AutofacRegistrationModules.config" section="AutofacRegistrationModules" />
</files>
<modules>
<module type="assembly1.AutofacModules.ComponentModule, assembly1" />
<module type="assembly2.AutofacModules.PrintManagerModule, assembly2" />
<module type="assembly3.AutofacModules.ComponentModule, assembly3" />
<module type="assembly4.AutofacModules.FormEngineServicesComponentModule, assembly4" />
<module type="assembly5.ReportManagerServicesComponentModule, assembly5" />
<module type="assembly6.NHibernateComponentModule, assembly6">
<properties>
<property name="DatabaseConfigurationSectionName" value="databaseSettingsEClient" />
</properties>
</module>
<module type="assembly7.AutofacModules.ComponentModule, assembly7" />
<module type="assembly8.AutofacModules.ComponentModule, assembly8" />
<module type="assembly9.AutofacModules.ComponentModule, assembly9" />
<module type="Console1.AutofacModules.ComponentModule, Console1" />
</modules>
</autofac>
如您所见,使用builder.RegisterModule可以在代码上进行大量复制配置。
答案 0 :(得分:0)
最后,我使用了方法builder.RegisterModule来注册模块:
protected override void Load(ContainerBuilder builder)
{
...
bool executeLocal = false;
var executeLocalObj = Thread.GetData(Thread.GetNamedDataSlot("ExecuteLocal"));
if (executeLocalObj != null)
{
executeLocal = (bool)executeLocalObj;
}
if (executeLocal)
{
builder.RegisterModule(new mpm.seg.Common.Database.AutofacModules.NHibernateComponentModule());
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly1.ComponentModule, assembly1")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly2.AutofacModules.ComponentModule, assembly1")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly3.AutofacModules.ComponentModule, assembly3")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly4.AutofacModules.ComponentModule, assembly4")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly5.AutofacModules.ComponentModule, assembly5")) as IModule);
builder.RegisterModule(new assembly6.AutofacModules.NHibernateComponentModule() { DatabaseConfigurationSectionName = "databaseSettings" });
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly7.AutofacModules.ComponentModule, assembly7")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly8.AutofacModules.ComponentModule, assembly8")) as IModule);
builder.RegisterModule(Activator.CreateInstance(Type.GetType("assembly9.AutofacModules.ComponentModule, assembly9")) as IModule);
}
}
其中executeLocal是通过线程数据发送到Load的标志:
Thread.SetData(Thread.GetNamedDataSlot("ExecuteLocal"), executeLocal);