A logger that logs to SharePoint's ULS.
Inheritance: ILogger
コード例 #1
0
        public static IList<Assembly> GetAssemblies(IList<string> gacFolders, Func<string, bool> assemblyNameCondition, Func<string, bool> assemblyVersionCondition)
        {
            var assemblyList = new List<Assembly>();
            var logger = new TraceLogger("GSoft.Dynamite", "GSoft.Dynamite", false);

            try
            {
                foreach (string folder in gacFolders)
                {
                    var path = Path.Combine(FolderPath, folder);
                    if (Directory.Exists(path))
                    {
                        string[] assemblyFolders = Directory.GetDirectories(path);

                        foreach (string assemblyFolder in assemblyFolders)
                        {
                            ProcessFolder(assemblyFolder, assemblyNameCondition, assemblyVersionCondition, assemblyList, logger);
                        }
                    }
                }
            }
            catch (Exception error)
            {
                logger.Fatal(string.Format(CultureInfo.InvariantCulture, "GACAssemblyLocator failed to scan GAC. Error: {0}", error.ToString()));
            }

            return assemblyList;
        }
        /// <summary>
        /// Registers the modules type bindings
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        protected override void Load(ContainerBuilder builder)
        {
            // Logging
            #if DEBUG
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, true);     // Logger with debug output
            builder.RegisterInstance<ILogger>(logger);
            #else
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, false);    // Logger without debug output
            builder.RegisterInstance<ILogger>(logger);
            #endif

            // Binding
            var entitySchemaBuilder = new EntitySchemaBuilder<SharePointDataRowEntitySchema>();
            var cachedBuilder = new CachedSchemaBuilder(entitySchemaBuilder, logger);

            builder.RegisterType<SharePointDataRowEntitySchema>();
            builder.RegisterInstance<IEntitySchemaBuilder>(cachedBuilder);
            builder.RegisterType<TaxonomyValueDataRowConverter>();
            builder.RegisterType<TaxonomyValueCollectionDataRowConverter>();
            builder.RegisterType<TaxonomyValueConverter>();
            builder.RegisterType<TaxonomyValueCollectionConverter>();
            builder.RegisterType<SharePointEntityBinder>().As<ISharePointEntityBinder>().InstancePerSite();  // Singleton-per-site entity binder

            // Cache
            builder.RegisterType<CacheHelper>().As<ICacheHelper>();

            // Definitions
            builder.RegisterType<ContentTypeBuilder>();
            builder.RegisterType<FieldHelper>();

            // Globalization + Variations (with default en-CA as source + fr-CA as destination implementation)
            builder.RegisterType<ResourceLocator>().As<IResourceLocator>();     // It's the container user's responsibility to register a IResourceLocatorConfig implementation
            builder.RegisterType<MuiHelper>();
            builder.RegisterType<DateHelper>();
            builder.RegisterType<RegionalSettingsHelper>();

            builder.RegisterType<DefaultVariationDirector>().As<IVariationDirector>();
            builder.RegisterType<CanadianEnglishAndFrenchVariationBuilder>().As<IVariationBuilder>();
            builder.RegisterType<VariationExpert>().As<IVariationExpert>();

            // Lists
            builder.RegisterType<ListHelper>();
            builder.RegisterType<ListLocator>();
            builder.RegisterType<ListSecurityHelper>();

            // MasterPages
            builder.RegisterType<MasterPageHelper>();
            builder.RegisterType<ExtraMasterPageBodyCssClasses>().As<IExtraMasterPageBodyCssClasses>();

            // Repositories
            builder.RegisterType<FolderRepository>();
            builder.RegisterType<QueryHelper>().As<IQueryHelper>();

            // Security
            builder.RegisterType<SecurityHelper>();
            builder.RegisterType<UserHelper>();

            // Serializers
            builder.RegisterType<ServiceStackSerializer>().As<ISerializer>().SingleInstance();

            // Setup
            builder.RegisterType<FieldValueInfo>().As<IFieldValueInfo>();
            builder.RegisterType<FolderInfo>().As<IFolderInfo>();
            builder.RegisterType<PageInfo>().As<IPageInfo>();
            builder.RegisterType<TaxonomyInfo>().As<ITaxonomyInfo>();
            builder.RegisterType<TaxonomyMultiInfo>().As<ITaxonomyMultiInfo>();

            builder.RegisterType<FolderMaker>().As<IFolderMaker>();
            builder.RegisterType<PageCreator>();

            // Taxonomy
            builder.RegisterType<SiteTaxonomyCacheManager>().As<ISiteTaxonomyCacheManager>().SingleInstance();
            builder.RegisterType<TaxonomyService>().As<ITaxonomyService>().InstancePerSite();
            builder.RegisterType<TaxonomyHelper>();

            // Timer Jobs
            builder.RegisterType<TimerJobExpert>().As<ITimerJobExpert>();

            // Utils
            builder.RegisterType<EventReceiverHelper>();
            builder.RegisterType<SearchHelper>();
            builder.RegisterType<CustomActionHelper>();
            builder.RegisterType<ContentOrganizerHelper>();

            // Web config
            builder.RegisterType<WebConfigModificationHelper>();

            // Web Parts
            builder.RegisterType<WebPartHelper>();
        }
コード例 #3
0
        /// <summary>
        /// Triggers ServiceLocator bootstrapping (scans the GAC for assemblies with a name
        /// that matches *.ServiceLocator.DLL, by convention).
        /// </summary>
        /// <param name="web">The context's SPWeb. Keep null if none available.</param>
        /// <param name="site">The context's SPSite. Keep null if none available.</param>
        /// <param name="webApplication">The context's SPWebApplication. Keep null if none available.</param>
        /// <param name="farm">The context's SPFarm. Keep null if none available.</param>
        private void EnsureServiceLocatorAccessor(SPWeb web, SPSite site, SPWebApplication webApplication, SPFarm farm)
        {
            if (this.GetLocatorAccessor(web, site, webApplication, farm) == null)
            {
                lock (this.lockObject)
                {
                    if (this.GetLocatorAccessor(web, site, webApplication, farm) == null)
                    {
                        try
                        {
                            // 1) Scan the GAC for any DLL matching the *.ServiceLocator.DLL pattern
                            var matchingAssemblies = GacAssemblyLocator.GetAssemblies(new List<string>() { "GAC_MSIL" }, assemblyFileName => assemblyFileName.Contains(".ServiceLocator"));

                            Assembly serviceLocatorAssembly = null;
                            Type accessorType = null;

                            if (matchingAssemblies.Any())
                            {
                                if (matchingAssemblies.Count > 1)
                                {
                                    // 2) If more than one service locator is found, we must disambiguate. We have to use the 
                                    //    contextual SPWeb, SPSite, SPWebApp or SPFarm objects and extract the preferred service 
                                    //    locator assembly name setting from their property bag.
                                    //    The SPWeb's property bag is inspected first, if available, then the SPSite's RootWeb property
                                    //    bag, then the SPWebApp's, then the SPFarm's property bag as a last resort.
                                    string contextObjectWhereDiscriminatorWasFound;
                                    string serviceLocatorAssemblyNameDiscriminator = FindServiceLocatorAccessorTypeNameFromMostSpecificPropertyBag(web, site, webApplication, farm, out contextObjectWhereDiscriminatorWasFound);

                                    string allServiceLocatorAssemblyNames = string.Join(";", matchingAssemblies.Select(locatorAssembly => locatorAssembly.FullName).ToArray());
                                    string basicDisambiguationErrorMessage = string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Failed to disambiguate between all DLLs in the GAC that match the *.ServiceLocator.DLL filename pattern. All matching assemblies in GAC: {0}.",
                                        allServiceLocatorAssemblyNames);

                                    if (!string.IsNullOrEmpty(serviceLocatorAssemblyNameDiscriminator))
                                    {
                                        // We found a ServiceLocator assembly name in one of the context's Property Bags.
                                        serviceLocatorAssembly = matchingAssemblies.FirstOrDefault(assembly => assembly.FullName.Contains(serviceLocatorAssemblyNameDiscriminator));

                                        if (serviceLocatorAssembly == null)
                                        {
                                            throw new InvalidOperationException(basicDisambiguationErrorMessage +
                                                " The discriminator found in one of the context's Property Bags (value=" + serviceLocatorAssemblyNameDiscriminator +
                                                ", property bag location=" + contextObjectWhereDiscriminatorWasFound + ") did not match either of the " + 
                                                matchingAssemblies.Count + " ServiceLocator DLLs available in GAC. The discriminator value should match one of the DLLs so that we can determine which to use.");
                                        }
                                    }
                                    else
                                    {
                                        // We failed to find a discriminator setting in all of the context's Property Bags
                                        throw new InvalidOperationException(basicDisambiguationErrorMessage +
                                            " You cannot begin injection from the root application container if more that one ServiceLocator assembly exists in the GAC." +
                                            " You must begin injection with one of the following methods on your ISharePointServiceLocator: BeginLifetimeScope(SPFeature) or" +
                                            " BeginLifetimeScope(SPWeb) or BeginLifetimeScope(SPSite) or BeginLifetimeScope(SPWebApplication) or BeginLifetimeScope(SPFarm)," +
                                            " depending on your context. IMPORTANT: The property bags on the context' SPWeb, SPSite, SPWebApplication and SPFarm will be inspected" +
                                            " (in that order) to find a value for the key '" + KeyServiceLocatorAssemblyName + "'. This discriminator value will indicate to Dynamite's" +
                                            " AddOnProvidedServiceLocator which concrete add-on's ServiceLocator DLL to use in the current context.");
                                    }
                                }

                                if (serviceLocatorAssembly != null)
                                {
                                    // Only one matching assembly, find its accessor class
                                    accessorType = FindServiceLocatorAccessorType(serviceLocatorAssembly);
                                }
                                else
                                {
                                    // Only one ServiceLocator DLL found in GAC. There is no ambiguity: use this locator.
                                    serviceLocatorAssembly = matchingAssemblies[0];
                                }

                                if (serviceLocatorAssembly != null)
                                {
                                    // At this point we figured out the right matching assembly: find its accessor class within its types
                                    accessorType = FindServiceLocatorAccessorType(serviceLocatorAssembly);
                                }
                            }
                            else
                            {
                                // Not even one DLL in GAC matches the *.ServiceLocator.DLL pattern
                                throw new InvalidOperationException("Failed to find any assembly in the GAC that matches the *.ServiceLocator.DLL pattern.");
                            }

                            if (accessorType != null)
                            {
                                // 3) Create the accessor instance
                                this.AddLocatorAccessor(web, site, webApplication, farm,  (ISharePointServiceLocatorAccessor)Activator.CreateInstance(accessorType));
                            }
                            else
                            {
                                throw new InvalidOperationException("Failed to find implementation of ISharePointServiceLocatorAccessor for AddOnProvidedServiceLocator. Your ServiceLocator assembly (" + serviceLocatorAssembly.FullName + ") should expose its static container through that interface.");
                            }
                        }
                        catch (InvalidOperationException exception)
                        {
                            var logger = new TraceLogger("GSoft.Dynamite", "GSoft.Dynamite", false);
                            logger.Error(
                                "AddOnProvidedServiceLocator Initialization Error - An error occured while trying to find a DLL matching the pattern *ServiceLocator.dll in the GAC. The FallbackServiceLocator will be used instead as a last resort (no AddOn registration module will be registered). Exception: {0}", 
                                exception.ToString());

                            // Either no assembly in the GAC matches the pattern *.ServiceLocator.DLL pattern, 
                            // or in the matching assembly that was found, no class implements ISharePointServiceLocatorAccessor.
                            // In this case, use our default all-available-Dynamite-modules-only service locator
                            this.AddLocatorAccessor(web, site, webApplication, farm,  new FallbackServiceLocator());
                        }
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Registers the modules type bindings
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        protected override void Load(ContainerBuilder builder)
        {
            // Logging
            #if DEBUG
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, true);     // Logger with debug output
            builder.RegisterInstance<ILogger>(logger);
            #else
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, false);    // Logger without debug output
            builder.RegisterInstance<ILogger>(logger);
            #endif

            // Binding
            builder.RegisterType<EntitySchemaFactory>().Named<IEntitySchemaFactory>("decorated");
            builder.RegisterDecorator<IEntitySchemaFactory>((c, inner) => new CachedEntitySchemaFactory(inner, c.Resolve<ILogger>()), fromKey: "decorated");
            builder.RegisterType<SharePointEntityBinder>().As<ISharePointEntityBinder>().InstancePerSite();  // Singleton-per-site entity binder

            builder.RegisterType<FieldValueWriter>().As<IFieldValueWriter>();
            var writers = new[]
            {
                typeof(StringValueWriter),
                typeof(BooleanValueWriter),
                typeof(IntegerValueWriter),
                typeof(DoubleValueWriter),
                typeof(DateTimeValueWriter),
                typeof(GuidValueWriter),
                typeof(TaxonomyValueWriter),
                typeof(TaxonomyValueCollectionWriter),
                typeof(LookupValueWriter),
                typeof(LookupValueCollectionWriter),
                typeof(PrincipalValueWriter),
                typeof(UserValueWriter),
                typeof(UserValueCollectionWriter),
                typeof(UrlValueWriter),
                typeof(ImageValueWriter),
                typeof(MediaValueWriter)
            };
            writers.ToList().ForEach(w => builder.RegisterType(w).As<IBaseValueWriter>().SingleInstance());

            builder.RegisterType<FieldValueReader>().As<IFieldValueReader>();
            var readers = new[]
            {
                typeof(StringValueReader),
                typeof(BooleanValueReader),
                typeof(IntegerValueReader),
                typeof(DoubleValueReader),
                typeof(DateTimeValueReader),
                typeof(GuidValueReader),
                typeof(TaxonomyValueReader),
                typeof(TaxonomyValueCollectionReader),
                typeof(LookupValueReader),
                typeof(LookupValueCollectionReader),
                typeof(PrincipalValueReader),
                typeof(UserValueReader),
                typeof(UserValueCollectionReader),
                typeof(UrlValueReader),
                typeof(ImageValueReader),
                typeof(MediaValueReader)
            };
            readers.ToList().ForEach(r => builder.RegisterType(r).As<IBaseValueReader>().SingleInstance());

            // Branding
            builder.RegisterType<MasterPageHelper>().As<IMasterPageHelper>();
            builder.RegisterType<ExtraMasterPageBodyCssClasses>().As<IExtraMasterPageBodyCssClasses>();
            builder.RegisterType<ComposedLookRepository>().As<IComposedLookRepository>();
            builder.RegisterType<DisplayTemplateHelper>().As<IDisplayTemplateHelper>();
            builder.RegisterType<ImageRenditionHelper>().As<IImageRenditionHelper>();

            // Cache
            builder.RegisterType<CacheHelper>().As<ICacheHelper>();

            // CAML query builder and utilities
            builder.RegisterType<CamlBuilder>().As<ICamlBuilder>();
            builder.RegisterType<CamlUtils>().As<ICamlUtils>();
            builder.RegisterType<QueryHelper>().As<IQueryHelper>();

            // Catalogs
            builder.RegisterType<CatalogHelper>().As<ICatalogHelper>();

            // Configuration
            builder.RegisterType<PropertyBagHelper>().As<IPropertyBagHelper>();
            builder.RegisterType<PropertyBagConfiguration>().As<IConfiguration>();
            builder.RegisterType<WebConfigModificationHelper>().As<IWebConfigModificationHelper>();

            // ContentTypes
            builder.RegisterType<ContentTypeHelper>().As<IContentTypeHelper>();

            // Documents
            builder.RegisterType<ContentOrganizerHelper>().As<IContentOrganizerHelper>();

            // Events
            builder.RegisterType<EventReceiverHelper>().As<IEventReceiverHelper>();

            // Fields
            builder.RegisterType<FieldHelper>().As<IFieldHelper>();
            builder.RegisterType<FieldLocator>().As<IFieldLocator>();
            builder.RegisterType<FieldSchemaHelper>().As<IFieldSchemaHelper>();
            builder.RegisterType<FieldLookupHelper>().As<IFieldLookupHelper>();

            // Folders
            builder.RegisterType<FolderHelper>().As<IFolderHelper>();

            // Files
            builder.RegisterType<FileHelper>().As<IFileHelper>();

            // Globalization + Variations (with default en-CA as source + fr-CA as destination implementation)
            builder.RegisterType<ResourceLocator>().As<IResourceLocator>();

            // It's the container user's responsibility to register a IResourceLocatorConfig implementation
            builder.RegisterType<DefaultResourceLocatorConfig>().As<IResourceLocatorConfig>();
            builder.RegisterType<MuiHelper>().As<IMuiHelper>();

            builder.RegisterType<VariationExpert>().As<IVariationExpert>();
            builder.RegisterType<VariationHelper>().As<IVariationHelper>();
            builder.RegisterType<VariationSyncHelper>().As<IVariationSyncHelper>();

            // Lists
            builder.RegisterType<ListHelper>().As<IListHelper>();
            builder.RegisterType<ListLocator>().As<IListLocator>();
            builder.RegisterType<ListSecurityHelper>().As<IListSecurityHelper>();
            builder.RegisterType<PublishedLinksEditor>().As<IPublishedLinksEditor>();

            // Monitoring
            builder.RegisterType<AggregateTimeTracker>().As<IAggregateTimeTracker>().InstancePerSite();

            // Navigation
            builder.RegisterType<NavigationService>().As<INavigationService>();
            builder.RegisterType<NavigationHelper>().As<INavigationHelper>();
            builder.RegisterType<VariationNavigationHelper>().As<IVariationNavigationHelper>();

            // Pages
            builder.RegisterType<PageHelper>().As<IPageHelper>();

            // Repositories
            builder.RegisterType<ItemLocator>().As<IItemLocator>();

            // Search
            builder.RegisterType<SearchHelper>().As<ISearchHelper>();
            builder.RegisterType<QueryRuleHelper>().As<IQueryRuleHelper>();

            // Security
            builder.RegisterType<SecurityHelper>().As<ISecurityHelper>();
            builder.RegisterType<UserHelper>().As<IUserHelper>();

            // Serializers
            builder.RegisterType<XmlHelper>().As<IXmlHelper>();
            builder.RegisterType<JsonNetSerializer>().As<ISerializer>().SingleInstance();

            // Taxonomy
            builder.RegisterType<PerRequestSiteTaxonomyCacheManager>().As<ISiteTaxonomyCacheManager>();
            builder.RegisterType<TaxonomyService>().As<ITaxonomyService>();

            //// Example of monitored (profiled) instance - a typical decorator pattern use case:
            ////builder.RegisterType<TaxonomyService>().Named<ITaxonomyService>("decorated").InstancePerSite();
            ////builder.RegisterDecorator<ITaxonomyService>((c, inner) => new MonitoredTaxonomyService(inner, c.Resolve<IAggregateTimeTracker>()), fromKey: "decorated");

            builder.RegisterType<TaxonomyHelper>().As<ITaxonomyHelper>();

            // Timer Jobs
            builder.RegisterType<TimerJobHelper>().As<ITimerJobHelper>();

            // Utils
            builder.RegisterType<CustomActionHelper>().As<ICustomActionHelper>();
            builder.RegisterType<CatchallExceptionHandler>().As<ICatchallExceptionHandler>();

            // Web Parts
            builder.RegisterType<WebPartHelper>().As<IWebPartHelper>();

            // Features
            builder.RegisterType<FeatureDependencyActivator>().As<IFeatureDependencyActivator>();
        }
        /// <summary>
        /// Registers the modules type bindings
        /// </summary>
        /// <param name="container">The container on which to register type bindings</param>
        public void Register(IUnityContainer container)
        {
            // Logging
            #if DEBUG
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, true);     // Logger with debug output
            container.RegisterInstance<ILogger>(logger);
            #else
            var logger = new TraceLogger(this.logCategoryName, this.logCategoryName, false);    // Logger without debug output
            container.RegisterInstance<ILogger>(logger);
            #endif

            // Binding
            var builder = new EntitySchemaBuilder<SharePointEntitySchema>();
            var cachedBuilder = new CachedSchemaBuilder(builder, logger);
            container.RegisterInstance<IEntitySchemaBuilder>(cachedBuilder);
            container.RegisterType<TaxonomyValueConverter>();
            container.RegisterType<TaxonomyValueCollectionConverter>();

            container.RegisterType<ISharePointEntityBinder, SharePointEntityBinder>(new ContainerControlledLifetimeManager());      // Singleton entity binder

            // Cache
            container.RegisterType<ICacheHelper, CacheHelper>();

            // Definitions
            container.RegisterType<ContentTypeBuilder>();
            container.RegisterType<FieldHelper>();

            // Globalization + Variations (with default en-CA as source + fr-CA as destination implementation)
            container.RegisterInstance<IResourceLocator>(new ResourceLocator(this.defaultResourceFileNames));
            container.RegisterType<MuiHelper>();
            container.RegisterType<DateHelper>();
            container.RegisterType<RegionalSettingsHelper>();

            container.RegisterType<IVariationDirector, DefaultVariationDirector>();
            container.RegisterType<IVariationBuilder, CanadianEnglishAndFrenchVariationBuilder>();
            container.RegisterType<IVariationExpert, VariationExpert>();

            // Lists
            container.RegisterType<ListHelper>();
            container.RegisterType<ListSecurityHelper>();

            // Master Pages
            container.RegisterType<MasterPageHelper>();
            container.RegisterType<IExtraMasterPageBodyCssClasses, ExtraMasterPageBodyCssClasses>();

            // Repositories
            container.RegisterType<FolderRepository>();
            container.RegisterType<ListLocator>();
            container.RegisterType<IQueryHelper, QueryHelper>();

            // Security
            container.RegisterType<SecurityHelper>();
            container.RegisterType<UserHelper>();

            // Setup
            container.RegisterType<IFieldValueInfo, FieldValueInfo>();
            container.RegisterType<IFolderInfo, FolderInfo>();
            container.RegisterType<IPageInfo, PageInfo>();
            container.RegisterType<ITaxonomyInfo, TaxonomyInfo>();
            container.RegisterType<ITaxonomyMultiInfo, TaxonomyMultiInfo>();

            container.RegisterType<IFolderMaker, FolderMaker>();
            container.RegisterType<PageCreator>();

            // Taxonomy
            container.RegisterType<ISiteTaxonomyCacheManager, SiteTaxonomyCacheManager>();
            container.RegisterType<ITaxonomyService, TaxonomyService>();
            container.RegisterType<TaxonomyService>();
            container.RegisterType<TaxonomyHelper>();

            // Timer Jobs
            container.RegisterType<ITimerJobExpert, TimerJobExpert>();

            // Utilities
            container.RegisterType<EventReceiverHelper>();
            container.RegisterType<SearchHelper>();
            container.RegisterType<CustomActionHelper>();
            container.RegisterType<ContentOrganizerHelper>();

            // Web config
            container.RegisterType<WebConfigModificationHelper>();

            // Web Parts
            container.RegisterType<WebPartHelper>();
        }