private static void Main(string[] args) { var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(asmCatalog, isThreadSafe: true); var sl = new ExportProviderServiceLocator(container); foreach (var t in sl.GetAllInstances <ITest>()) { } foreach (var t in sl.GetAllInstances <ITest>()) { } Console.ReadLine(); }
private static void Main(string[] args) { var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(asmCatalog, isThreadSafe: true); var sl = new ExportProviderServiceLocator(container); foreach (var t in sl.GetAllInstances<ITest>()) { } foreach (var t in sl.GetAllInstances<ITest>()) { } Console.ReadLine(); }
protected void Application_Start(object sender, EventArgs e) { var rootDir = new DirectoryInfo(this.Server.MapPath("~/bin")); var config = new IniFileConfigRepository(filePath: Path.Combine(rootDir.FullName, "config.ini"), isReadOnly: false); // ServiceLocator { this.GlobalCompositionCatalog = new AggregateCatalog(); // assemblies { var asms = new HashSet <Assembly>(); asms.Add(typeof(global::MarcelJoachimKloubert.ServerAdmin.IServerAdminObject).Assembly); asms.Add(this.GetType().Assembly); foreach (var a in asms) { this.GlobalCompositionCatalog.Catalogs.Add(new AssemblyCatalog(a)); } } this.GlobalCompositionContainer = new CompositionContainer(this.GlobalCompositionCatalog, true); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.Configuration.IConfigRepository>(config.MakeReadOnly()); this.GlobalCompositionContainer.ComposeExportedValue <global::System.Web.HttpApplication>(this); var innerLocator = new ExportProviderServiceLocator(this.GlobalCompositionContainer); this.GlobalServiceLocator = new DelegateServiceLocator(innerLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.IServiceLocator>(this.GlobalServiceLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.Impl.DelegateServiceLocator>(this.GlobalServiceLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.Impl.ExportProviderServiceLocator>(innerLocator); ServiceLocator.SetLocator(this.GlobalServiceLocator); } }
private static void Test_ServiceLocator() { var catalogs = new AggregateCatalog(); catalogs.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); var container = new CompositionContainer(catalogs, true); var innerServiceLocator = new ExportProviderServiceLocator(container); var serviceLocator = new DelegateServiceLocatorWrapper(innerServiceLocator); serviceLocator.RegisterSingle <IA>(Test_ServiceLocator3); serviceLocator.RegisterMulti <IB>(Test_ServiceLocator2); var a1 = serviceLocator.GetAllInstances <IA>().ToArray(); var a2 = serviceLocator.GetAllInstances <IA>().ToArray(); // var a3 = serviceLocator.GetInstance<IA>(); var b1 = serviceLocator.GetAllInstances <IB>().ToArray(); var b2 = serviceLocator.GetAllInstances <IB>().ToArray(); var b3 = serviceLocator.GetInstance <IB>(); }
// Public Methods (2) /// <summary> /// Reloads the list of plugins. /// </summary> public void ReloadPlugIns() { try { var loadedPlugIns = new List <IPlugIn>(); var plugInDir = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "PlugIns")); if (plugInDir.Exists) { foreach (var file in plugInDir.GetFiles("*.dll")) { try { var asmBlob = File.ReadAllBytes(file.FullName); var asm = Assembly.Load(asmBlob); var catalog = new AssemblyCatalog(asm); var ctx = new PlugInContext(); ctx.Assembly = asm; ctx.AssemblyFile = file.FullName; var container = new CompositionContainer(catalog, isThreadSafe: true); container.ComposeExportedValue <global::MarcelJoachimKloubert.DragNBatch.PlugIns.IPlugInContext>(ctx); var instances = new MultiInstanceComposer <IPlugIn>(container); instances.RefeshIfNeeded(); // service locator { var mefLocator = new ExportProviderServiceLocator(container); var sl = new DelegateServiceLocator(mefLocator); ctx.ServiceLocator = sl; } var initializedPlugIns = new List <IPlugIn>(); foreach (var i in instances.Instances) { try { i.Initialize(ctx); initializedPlugIns.Add(i); } catch (Exception ex) { this.OnError(ex); } } ctx.PlugIns = initializedPlugIns.ToArray(); loadedPlugIns.AddRange(initializedPlugIns); } catch (Exception ex) { this.OnError(ex); } } } this.PlugIns.Clear(); this.PlugIns.AddRange(loadedPlugIns); } catch (Exception ex) { this.OnError(ex); } }
protected void Application_Start(object sender, EventArgs e) { var rootDir = new DirectoryInfo(this.Server.MapPath("~/bin")); // app context var app = new CloudApp(); { IConfigRepository config; var configIni = new FileInfo(Path.Combine(rootDir.FullName, "config.ini")); if (configIni.Exists) { config = new IniFileConfigRepository(configIni); } else { config = new KeyValuePairConfigRepository(); } app.Config = config.MakeReadOnly(); } // principal repository var pricRepo = new PrincipalRepository(); { pricRepo.LocalDataDirectory = rootDir.FullName; { string temp; if (app.Config.TryGetValue <string>("Data", out temp, "Directories") && string.IsNullOrWhiteSpace(temp) == false) { pricRepo.LocalDataDirectory = temp.Trim(); } } var iniFile = new FileInfo(Path.Combine(rootDir.FullName, "users.ini")); if (iniFile.Exists) { pricRepo.UserRepository = new IniFileConfigRepository(iniFile); } pricRepo.Reload(); } // URL routes { // files RouteTable.Routes.Add(new Route ( "files", new FilesHttpHandler() )); // messages RouteTable.Routes.Add(new Route ( "messages", new MessagesHttpHandler() )); } // ServiceLocator { this.GlobalCompositionCatalog = new AggregateCatalog(); this.GlobalCompositionCatalog.Catalogs.Add(new AssemblyCatalog(typeof(global::MarcelJoachimKloubert.CloudNET.Classes.ICloudApp).Assembly)); this.GlobalCompositionContainer = new CompositionContainer(this.GlobalCompositionCatalog, true); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CloudNET.Classes.ICloudApp>(app); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CloudNET.Classes.Security.IPrincipalRepository>(pricRepo); this.GlobalCompositionContainer.ComposeExportedValue <global::System.Web.HttpApplication>(this); var innerLocator = new ExportProviderServiceLocator(this.GlobalCompositionContainer); this.GlobalServiceLocator = new DelegateServiceLocator(innerLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.IServiceLocator>(this.GlobalServiceLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.Impl.DelegateServiceLocator>(this.GlobalServiceLocator); this.GlobalCompositionContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.ServiceLocation.Impl.ExportProviderServiceLocator>(innerLocator); ServiceLocator.SetLocator(this.GlobalServiceLocator); } this.Application[APP_VAR_APPCONTEXT] = app; this.Application[APP_VAR_PRINCIPALS] = pricRepo; }
// Protected Methods (3) /// <summary> /// /// </summary> /// <see cref="AppServerBase.OnInitialize(IAppServerInitContext, ref bool)" /> protected override void OnInitialize(IAppServerInitContext initContext, ref bool isInitialized) { this.Arguments = (initContext.Arguments ?? Enumerable.Empty <string>()).OfType <string>() .ToArray(); this.WorkingDirectory = initContext.WorkingDirectory; if (string.IsNullOrWhiteSpace(this.WorkingDirectory)) { this.WorkingDirectory = Environment.CurrentDirectory; } this.StartupConfig = new IniFileConfigRepository(Path.Combine(this.WorkingDirectory, "config.ini"), false); var trustedAssemblyKeys = this.LoadTrustedAssemblyKeyList(); this.EntityAssemblies = new SynchronizedCollection <Assembly>(); this.RefreshEntityAssemblyList(); // service locator CompositionContainer compContainer; AggregateCatalog compCatalog; DelegateServiceLocator serviceLocator; { compCatalog = new AggregateCatalog(); compCatalog.Catalogs .Add(this.TrustedCompositionCatalog = new StrongNamedAssemblyPartCatalog(trustedAssemblyKeys)); this.ReinitTrustedCompositionCatalog(); compContainer = new CompositionContainer(compCatalog, isThreadSafe: true); var mefServiceLocator = new ExportProviderServiceLocator(compContainer); serviceLocator = new DelegateServiceLocator(mefServiceLocator); serviceLocator.RegisterSingleProvider <global::MarcelJoachimKloubert.CLRToolbox.Templates.Text.Html.IHtmlTemplate>(WebInterfaceHandler.GetHtmlTemplate); } // logger AggregateLogger logger; DelegateLogger loggerFuncs; { loggerFuncs = new DelegateLogger(); logger = new AggregateLogger(); logger.Add(loggerFuncs); var outerLogger = initContext.Logger; if (outerLogger != null) { logger.Add(outerLogger); } serviceLocator.RegisterMultiProvider(this.GetAllLoggers, false); compContainer.ComposeExportedValue <global::MarcelJoachimKloubert.CLRToolbox.Diagnostics.ILoggerFacade>(new AsyncLogger(logger)); } this.LoggerFuncs = loggerFuncs; this.Logger = logger; this.GlobalCompositionCatalog = compCatalog; this.GlobalCompositionContainer = compContainer; compContainer.ComposeExportedValue <global::MarcelJoachimKloubert.ApplicationServer.ApplicationServer>(this); compContainer.ComposeExportedValue <global::MarcelJoachimKloubert.ApplicationServer.IAppServer>(this); this.GlobalServiceLocator = serviceLocator; ServiceLocator.SetLocatorProvider(this.GetGlobalServiceLocator); }