Example #1
0
        private ContainerExportProvider SetUpExportProviderForAutofac()
        {
            // Autofac container builder
            var builder = new ContainerBuilder();

            // Register reward service
            builder.Register(c => new ChannelFactory<IRewardServiceClient>(
                new BasicHttpBinding(),
                new EndpointAddress("http://localhost:60112/RewardService.svc")))
              .SingleInstance();

            builder.Register(c => c.Resolve<ChannelFactory<IRewardServiceClient>>().CreateChannel()).UseWcfSafeRelease();

            var container = builder.Build();

            // Adapt and export to MEF
            var adapter = new AutofacContainerAdapter(container);
            return new ContainerExportProvider(adapter);
        }
        private void ConfigureMef()
        {
            var adapter = new AutofacContainerAdapter(_context);
            var containerExportProvider = new ContainerExportProvider(adapter);
            var scope = _context as ISharingLifetimeScope;
            if (scope != null)
            {
                var parentScope = scope.ParentLifetimeScope;
                var parent = parentScope != null ? parentScope.ResolveOptional<CompositionIntegration>() : null;
                if (parent != null)
                {
                    // Get the parent ContainerExportProvider
                    var parentContainerExportProvider = (ContainerExportProvider) parent.Providers.Where(
                        ep => typeof (ContainerExportProvider).IsAssignableFrom(ep.GetType())).First();

                    // Collect all the exports provided by the parent container and add
                    // them to the child export provider
                    foreach (var definition in parentContainerExportProvider.FactoryExportProvider.ReadOnlyDefinitions)
                    {
                        containerExportProvider.FactoryExportProvider.Register(
                            definition.ContractType,
                            definition.RegistrationName);
                    }

                    // Grab all the parent export providers except the container ones
                    var exportProviders = new List<ExportProvider>(
                        parent.Providers.Where(
                            ep => !typeof (ContainerExportProvider).IsAssignableFrom(ep.GetType())))
                                              {containerExportProvider};

                    var catalog = new AggregateCatalog(parent.Catalogs);
                        //NOTE: this might result in duplicate exports... need to investigate.

                    Register = true;
                    _providers = exportProviders.ToArray();
                    Catalogs.Add(catalog);
                }
                else
                {
                    Register = true;
                    _providers = new ExportProvider[] {containerExportProvider};
                }
            }
        }