示例#1
0
        public static PermissionServiceLocator Initialize()
        {
            Open.SPF.Utility.TraceUtility.WriteTrace(typeof(PermissionServiceLocator), "Initialze", Open.SPF.Utility.TraceUtility.TraceType.Begin);
            IUnityContainer          container = new UnityContainer();
            PermissionServiceLocator instance  = new PermissionServiceLocator(container);

            container.RegisterInstance <IServiceLocator>(instance);
            Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(new ServiceLocatorProvider(instance.GetServiceLocatorInstance));

            UnityConfigurationSection section = null;

            try
            {
                Open.SPF.Utility.TraceUtility.WriteTrace(typeof(PermissionServiceLocator), "Initialze", "section.Containers[0].Configure(container)", null, Open.SPF.Utility.TraceUtility.TraceType.Begin);
                section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                if ((section != null) && (section.Containers.Count > 0))
                {
                    section.Configure(container, "defaultContainer");
                }
                else
                {
                    Open.SPF.Utility.EventLogUtility.LogWarningMessage("The Unity configuration section was not found or was not properly defined.");
                }
                Open.SPF.Utility.TraceUtility.WriteTrace(typeof(PermissionServiceLocator), "Initialze", "section.Containers[0].Configure(container)", null, Open.SPF.Utility.TraceUtility.TraceType.End);
            }
            catch (Exception ex)
            {
                Open.SPF.Utility.EventLogUtility.LogWarningMessage(String.Format("There was an error reading the Unity configuration section:\r\n\r\n{0}", Open.SPF.Utility.EventLogUtility.FormatExceptionMessage(ex)));
            }

            object test = instance.GetInstance <Open.SPF.Core.IPermissionManager>();

            if (test == null)
            {
                Open.SPF.Utility.EventLogUtility.LogWarningMessage("Unity configuration did not contain an element for IPermissionManager. Registering a default instance instead.");
                container.RegisterInstance(typeof(IPermissionManager), new PermissionManager(), new PerThreadLifetimeManager());
            }

            test = instance.GetInstance <Open.SPF.Core.IPermissionCache>();
            if (test == null)
            {
                Open.SPF.Utility.EventLogUtility.LogWarningMessage("Unity configuration did not contain an element for IPermissionCache. Registering a default instance instead.");
                container.RegisterInstance(typeof(IPermissionCache), new PermissionCache(), new PerThreadLifetimeManager());
            }

            Open.SPF.Utility.TraceUtility.WriteTrace(typeof(PermissionServiceLocator), "Initialze", null, String.Format("Locator instance {0} NULL.", ((instance != null) ? "is NOT" : "is")), Open.SPF.Utility.TraceUtility.TraceType.End);
            return(instance);
        }
        public static PermissionServiceLocator Initialize(IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException("Services", "A Service Collection is required for initialization.");
            }

            TraceUtility.WriteInformationTrace(typeof(PermissionServiceLocator), "Initialze", Open.SPF.Utility.TraceUtility.TraceType.Begin);

            ServiceRegistration serviceRegistration = new ServiceRegistration();

            serviceRegistration.ConfigureServices(services);

            ServiceProvider          serviceProvider          = services.BuildServiceProvider();
            PermissionServiceLocator permissionServiceLocator = new PermissionServiceLocator(serviceProvider);

            services.AddSingleton(typeof(IServiceLocator), permissionServiceLocator);

            object test = permissionServiceLocator.GetInstance <IServiceLocator>();

            if (test == null)
            {
                TraceUtility.LogWarningMessage("Unity configuration did not contain an element for IServiceLocator.");
            }

            //test = instance.GetInstance<Open.SPF.Core.IPermissionCache>();
            //if (test == null)
            //{
            //    Open.SPF.Utility.EventLogUtility.LogWarningMessage("Unity configuration did not contain an element for IPermissionCache. Registering a default instance instead.");
            //    container.RegisterInstance(typeof(IPermissionCache), new PermissionCache(), new PerThreadLifetimeManager());
            //}

            //Open.SPF.Utility.TraceUtility.WriteTrace(typeof(PermissionServiceLocator), "Initialze", null, String.Format("Locator instance {0} NULL.", ((instance != null) ? "is NOT" : "is")), Open.SPF.Utility.TraceUtility.TraceType.End);
            //return instance;
            _instance = permissionServiceLocator;

            return(permissionServiceLocator);
        }