示例#1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var assembly = new AssemblyCatalog(Assembly.GetEntryAssembly());
            var catalog  = new AggregateCatalog();

            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("."));
            var _container        = new CompositionContainer(catalog);
            var mefServiceLocator = new MefServiceLocator(_container);

            ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
            _container.ComposeExportedValue <IServiceLocator>(mefServiceLocator);
            MainViewModel viewModel = new MainViewModel();
            MainWindow    window    = new MainWindow();

            window.DataContext = viewModel;

            try
            {
                _container.ComposeParts(viewModel);
            }
            catch (CompositionException compositionException)
            {
                System.Console.WriteLine(compositionException.ToString());
            }

            //viewModel.func();
            viewModel.ReloadTasks();
            //viewModel.CreateTask();
            window.Show();
        }
示例#2
0
        internal static IServiceLocator GetServiceLocator(Assembly assembly, CompositionBatch batch)
        {
            var assemblyLocation = assembly.Location;
            var file             = new FileInfo(assemblyLocation);

            var catalogs = new List <ComposablePartCatalog>
            {
                new AssemblyCatalog(assembly),
                new DirectoryCatalog(file.DirectoryName ?? ".")
            };


            var catalog = new AggregateCatalog(catalogs);


            var container = new CompositionContainer(catalog,
                                                     CompositionOptions.DisableSilentRejection |
                                                     CompositionOptions.IsThreadSafe);


            var serviceLocator = new MefServiceLocator(container);

            batch.AddExportedValue(container);
            batch.AddExportedValue <IServiceLocator>(serviceLocator);

            container.Compose(batch);

            return(serviceLocator);
        }
示例#3
0
        internal static IServiceLocator GetServiceLocator(Assembly assembly, CompositionBatch batch)
        {
            var assemblyLocation = assembly.Location;
            var file = new FileInfo(assemblyLocation);

            var catalogs = new List<ComposablePartCatalog>
            {
                new AssemblyCatalog(assembly),
                new DirectoryCatalog(file.DirectoryName ?? ".")
            };

            var catalog = new AggregateCatalog(catalogs);

            var container = new CompositionContainer(catalog,
                                                        CompositionOptions.DisableSilentRejection |
                                                        CompositionOptions.IsThreadSafe );

            var serviceLocator = new MefServiceLocator(container);

            batch.AddExportedValue(container);
            batch.AddExportedValue<IServiceLocator>(serviceLocator);

            container.Compose(batch);

            return serviceLocator;
        }
示例#4
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            var assembly =
               new AssemblyCatalog(Assembly.GetEntryAssembly());


            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("."));


            var compositionContainer
                = new CompositionContainer(catalog);
            compositionContainer.ComposeParts(this);

            var locator = new MefServiceLocator(compositionContainer);
            ServiceLocator.SetLocatorProvider(() => locator);


            ViewModelManager.ViewModelShowEvent
                += vm => ViewManager.ViewShow(vm);
            ViewModelManager.ViewModelCloseEvent
                += vm => ViewManager.ViewClose(vm);

            var mainWindowViewModel = new MainWindowViewModel();
            compositionContainer.ComposeParts(mainWindowViewModel);
           
            MainWindow mainWindow = new MainWindow {DataContext = mainWindowViewModel};
            mainWindow.Show();
        }
示例#5
0
 protected void Application_Start(object sender, EventArgs e)
 {
     var assembly = new AssemblyCatalog(typeof(Global).Assembly);//текущую сборку заносим в контейнер
     var catalog = new AggregateCatalog();
     catalog.Catalogs.Add(assembly);
     catalog.Catalogs.Add(new DirectoryCatalog("bin"));
     var container = new CompositionContainer(catalog);
     var mefServiceLocator = new MefServiceLocator(container);
     ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
     container.ComposeExportedValue<IServiceLocator>(mefServiceLocator);
     container.ComposeParts(this);
 }
 protected void Application_Start(object sender, EventArgs e)
 {
     var assembly = new AssemblyCatalog(typeof(Global).Assembly); //this is how to add curent assembly
     var catalog = new AggregateCatalog();
     catalog.Catalogs.Add(assembly);
     catalog.Catalogs.Add(new DirectoryCatalog("bin")); //this is where our DLLs are located. "." is for WPF app, "bin" is for WEB app
     var container = new CompositionContainer(catalog);
     var mefServiceLocator = new MefServiceLocator(container);
     ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
     container.ComposeExportedValue<IServiceLocator>(mefServiceLocator);
     container.ComposeExportedValue(container);
 }
 public static void AssemblyInit(TestContext context)
 {
     AggregateCatalog aggregate = new AggregateCatalog(
         new AssemblyCatalog(Assembly.LoadFrom("FACCTS.Server.dll")),
         new AssemblyCatalog(Assembly.LoadFrom("FACCTS.Server.Data.dll")),
         new AssemblyCatalog(Assembly.LoadFrom("FACCTS.Server.DataContracts.dll"))
         );
     Contailer = new CompositionContainer(aggregate);
     InitializeContainerData();
     
     MefServiceLocator locator = new MefServiceLocator(Contailer);
     ServiceLocator.SetLocatorProvider(() => locator);
 }
示例#8
0
 private static CompositionContainer ConfigureContainer()
 {
     var path = HostingEnvironment.MapPath("~/bin");
     if (path == null) throw new Exception("Unable to find the path");
     var aggregateCatalog = new AggregateCatalog(new DirectoryCatalog(path, "FACCTS.Server.*.dll"));
     aggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
     var container = new CompositionContainer(aggregateCatalog);
     Container.Current = container;
     var sl = new MefServiceLocator(container);
     RegisterInstances(container);
     ServiceLocator.SetLocatorProvider(() => sl);
     return container;
 }
        private static void ConfigureComposition()
        {
            var catalog            = new AssemblyCatalog(typeof(Startup).Assembly);
            var container          = new CompositionContainer(catalog);
            var serviceLocator     = new MefServiceLocator(container);
            var dependencyResolver = new MefDependencyResolver(serviceLocator);

            container.ComposeExportedValue <IServiceLocator>(serviceLocator);

            //for web api
            GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;
            //for mvc
            System.Web.Mvc.DependencyResolver.SetResolver(dependencyResolver);
        }
示例#10
0
        public void VerifyGetInstanceThrowsWithMultipleInstances()
        {
            string firstStringInstance  = nameof(firstStringInstance);
            string secondStringInstance = nameof(secondStringInstance);

            var container = new CompositionContainer();

            container.ComposeExportedValue(firstStringInstance);
            container.ComposeExportedValue(secondStringInstance);

            var serviceLocator = new MefServiceLocator(container);

            Assert.Throws <ActivationException>(() => serviceLocator.GetInstance <string>());
        }
示例#11
0
        public void VerifyGetInstanceByTypeReturnsCorrectInstance()
        {
            string stringInstance = nameof(stringInstance);

            var container = new CompositionContainer();

            container.ComposeExportedValue(stringInstance);

            var serviceLocator = new MefServiceLocator(container);

            var instance = serviceLocator.GetInstance(typeof(string));

            Assert.That(instance, Is.EqualTo(stringInstance));
        }
        protected void Application_Start(object sender, EventArgs e)
        {
            var assembly = new AssemblyCatalog(typeof(Global).Assembly); //this is how to add curent assembly
            var catalog  = new AggregateCatalog();

            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("bin")); //this is where our DLLs are located. "." is for WPF app, "bin" is for WEB app
            var container         = new CompositionContainer(catalog);
            var mefServiceLocator = new MefServiceLocator(container);

            ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
            container.ComposeExportedValue <IServiceLocator>(mefServiceLocator);
            container.ComposeExportedValue(container);
        }
示例#13
0
        public void VerifyGetInstanceByKeyAndTypeReturnsCorrectInstance()
        {
            string stringInstance = nameof(stringInstance);

            var container = new CompositionContainer();

            container.ComposeExportedValue("key", stringInstance);
            container.ComposeExportedValue("wrong key", "a string instance");
            container.ComposeExportedValue("another wrong key", "another string instance");

            var serviceLocator = new MefServiceLocator(container);

            var instance = serviceLocator.GetInstance(typeof(string), "key");

            Assert.That(instance, Is.EqualTo(stringInstance));
        }
示例#14
0
 private void ApplicationStartup(object sender, StartupEventArgs e)
 {
     var assembly = new AssemblyCatalog(Assembly.GetEntryAssembly());
     var catalog = new AggregateCatalog();
     catalog.Catalogs.Add(assembly);
     catalog.Catalogs.Add(new DirectoryCatalog("."));
     var compositionContainer = new CompositionContainer(catalog);
     compositionContainer.ComposeParts(this);
     var locator = new MefServiceLocator(compositionContainer);
     ServiceLocator.SetLocatorProvider(()=>locator);
     ViewModelManager.ViewModelShowEvent
         += InstanceViewModelShowEvent;
     ViewModelManager.ViewModelCloseEvent += InstanceViewModelCloseEvent;
     var authViewModel = new AuthorisationViewModel();
     compositionContainer.ComposeParts(authViewModel);
     authViewModel.Initialize(new object());
     ViewModelManager.ViewModelShow(authViewModel);
 }
示例#15
0
        private void App_OnStartup(object sender, StartupEventArgs e)
        {
            var directoryCatalog = new DirectoryCatalog(".");
            var assemblyCatalog  = new AssemblyCatalog(GetType().Assembly);
            var aggregateCatalog = new AggregateCatalog();

            aggregateCatalog.Catalogs.Add(directoryCatalog);
            aggregateCatalog.Catalogs.Add(assemblyCatalog);

            var container = new CompositionContainer(aggregateCatalog);
            var locator   = new MefServiceLocator(container);

            container.ComposeExportedValue <IServiceLocator>(locator);

            container.ComposeParts(this);

            var qqq           = locator.GetInstance <IViewManager>();
            var userViewModel = locator.GetInstance <UserViewModel>();
        }
        protected void InitializeMef()
        {
            var assembly = new AssemblyCatalog(Assembly.GetEntryAssembly());
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("."));
            var container = new CompositionContainer(catalog);
            var mefServiceLocator = new MefServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
            container.ComposeExportedValue<IServiceLocator>(mefServiceLocator);
            container.ComposeParts(this);

            if (StartupViewModelType != null)
            {
                var showableViewModel = Locator.GetInstance(StartupViewModelType) as IShowableViewModel;

                showableViewModel?.Show();
            }
        }
示例#17
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var assembly = new AssemblyCatalog(Assembly.GetEntryAssembly());
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("."));
            var container = new CompositionContainer(catalog);
            var mefServiceLocator = new MefServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
            container.ComposeExportedValue<IServiceLocator>(mefServiceLocator);
            container.ComposeParts(this);

            Locator.GetInstance<IViewManager<IChildViewModel>>();

            var loginViewModel = Locator.GetInstance<LoginViewModel>();
            loginViewModel.Show();
        }
示例#18
0
        public void VerifyGetAllInstancesReturnsCorrectInstances()
        {
            string firstStringInstance  = nameof(firstStringInstance);
            string secondStringInstance = nameof(secondStringInstance);

            var container = new CompositionContainer();

            container.ComposeExportedValue(firstStringInstance);
            container.ComposeExportedValue(secondStringInstance);
            container.ComposeExportedValue(typeof(string));
            container.ComposeExportedValue(Math.PI);

            var serviceLocator = new MefServiceLocator(container);

            var instances = serviceLocator.GetAllInstances <string>();

            Assert.That(instances.ToList(), Has.Count.EqualTo(2));
            Assert.That(instances, Has.Exactly(1).EqualTo(firstStringInstance));
            Assert.That(instances, Has.Exactly(1).EqualTo(secondStringInstance));
        }
示例#19
0
        private void ConfigureIoc()
        {
            var assemblyCatalog  = new AssemblyCatalog(GetType().Assembly);
            var directoryCatalog = new DirectoryCatalog("bin");
            var aggregateCatalog = new AggregateCatalog();

            aggregateCatalog.Catalogs.Add(assemblyCatalog);
            aggregateCatalog.Catalogs.Add(directoryCatalog);

            var container = new CompositionContainer(aggregateCatalog);

            container.ComposeExportedValue(container);

            var serviceLocator = new MefServiceLocator(container);

            ServiceLocator.SetLocatorProvider(() => serviceLocator);
            container.ComposeExportedValue <IServiceLocator>(serviceLocator);

            var dependencyResolver = new MefDependencyResolver(serviceLocator);

            DependencyResolver.SetResolver(dependencyResolver);
            GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;
        }
示例#20
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            FillAppResources();
            var assembly = new AssemblyCatalog(Assembly.GetEntryAssembly());
            var catalog  = new AggregateCatalog();

            catalog.Catalogs.Add(assembly);
            catalog.Catalogs.Add(new DirectoryCatalog("."));
            var container         = new CompositionContainer(catalog);
            var mefServiceLocator = new MefServiceLocator(container);

            ServiceLocator.SetLocatorProvider(() => mefServiceLocator);
            container.ComposeExportedValue <IServiceLocator>(mefServiceLocator);
            container.ComposeParts(this);

            Locator.GetInstance <IViewManager <IChildViewModel> >();

            var loginViewModel = Locator.GetInstance <LoginViewModel>();

            loginViewModel.Show();
        }
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            FormatBqlCommand.Initialize(this);
            GoToDeclarationOrHandlerCommand.Initialize(this);
            base.Initialize();

            IComponentModel componentModel = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;

            if (componentModel == null)
            {
                return;
            }

            InitializeLogger();

            try
            {
                componentModel.DefaultCompositionService.SatisfyImportsOnce(this);

                var container = new CompositionContainer(CompositionOptions.Default, componentModel.DefaultExportProvider);
                container.ComposeExportedValue <CodeAnalysisSettings>(new CodeAnalysisSettingsFromOptionsPage(GeneralOptionsPage));

                // Service Locator
                IServiceLocator serviceLocator = new MefServiceLocator(container);

                if (ServiceLocator.IsLocationProviderSet)
                {
                    serviceLocator = new DelegatingServiceLocator(ServiceLocator.Current, serviceLocator);
                }

                ServiceLocator.SetLocatorProvider(() => serviceLocator);
            }
            catch
            {
                // Exception will be logged in FCEL
            }
        }