public void CanSerializeAndDeserializeSettings()
        {
            const string policyName = "policyName";
            const string typeName = "typeName";
            const string handlerName = "handlerName";
            const string handler1Name = "handler1Name";

            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            ExceptionPolicyData policyData = new ExceptionPolicyData(policyName);
            ExceptionTypeData typeData = new ExceptionTypeData(typeName, typeof(Exception), PostHandlingAction.None);
            typeData.ExceptionHandlers.Add(new WrapHandlerData(handlerName, "foo", typeof(InvalidCastException)));
            typeData.ExceptionHandlers.Add(new ReplaceHandlerData(handler1Name, "foo", typeof(InvalidCastException)));
            policyData.ExceptionTypes.Add(typeData);
            settings.ExceptionPolicies.Add(policyData);

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = fileName;
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            config.Sections.Add(ExceptionHandlingSettings.SectionName, settings);
            config.Save();

            config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            settings = (ExceptionHandlingSettings)config.Sections[ExceptionHandlingSettings.SectionName];

            Assert.AreEqual(1, settings.ExceptionPolicies.Count);
            Assert.AreEqual(1, settings.ExceptionPolicies.Get(0).ExceptionTypes.Count);
            Assert.AreEqual(2, settings.ExceptionPolicies.Get(0).ExceptionTypes.Get(0).ExceptionHandlers.Count);
        }
 public void SetUp()
 {
     settings = new ExceptionHandlingSettings();
     configurationSource = new DictionaryConfigurationSource();
     configurationSource.Add(ExceptionHandlingSettings.SectionName, settings);
     configurationSource.Add(
         InstrumentationConfigurationSection.SectionName,
         new InstrumentationConfigurationSection(false, false));
 }
        public void SetUp()
        {
            container = new UnityContainer();

            settings = new ExceptionHandlingSettings();
            configurationSource = new DictionaryConfigurationSource();
            configurationSource.Add(ExceptionHandlingSettings.SectionName, settings);

            container.AddExtension(new EnterpriseLibraryCoreExtension(configurationSource));
        }
Exemplo n.º 4
0
 private static ExceptionHandlingSettings CreateSettings()
 {
     LoggingExceptionHandlerData logData = new LoggingExceptionHandlerData("test", "cat1", 1, System.Diagnostics.TraceEventType.Error, "title", typeof(XmlExceptionFormatter), 4);
     ExceptionTypeData typeData = new ExceptionTypeData("test", typeof(Exception), PostHandlingAction.None);
     typeData.ExceptionHandlers.Add(logData);
     ExceptionPolicyData policy = new ExceptionPolicyData("test");
     policy.ExceptionTypes.Add(typeData);
     ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
     settings.ExceptionPolicies.Add(policy);
     return settings;
 }
        static ExceptionHandlingSettings CreateSettings()
        {
            FaultContractExceptionHandlerData logData = new FaultContractExceptionHandlerData("test");
            logData.FaultContractType = typeof(object).AssemblyQualifiedName;
            logData.ExceptionMessage = "my exception message";
            logData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("property1", "source1"));
            logData.PropertyMappings.Add(new FaultContractExceptionHandlerMappingData("property2", "source2"));

            ExceptionTypeData typeData = new ExceptionTypeData("test", typeof(Exception), PostHandlingAction.None);
            typeData.ExceptionHandlers.Add(logData);
            ExceptionPolicyData policy = new ExceptionPolicyData("test");
            policy.ExceptionTypes.Add(typeData);
            ExceptionHandlingSettings settings = new ExceptionHandlingSettings();
            settings.ExceptionPolicies.Add(policy);
            return settings;
        }
        protected override void Arrange()
        {
            updatableConfigurationSource = new ConfigurationSourceUpdatable();

            exceptionPolicy1 = new ExceptionPolicyData("default");
            exceptionTypeData = new ExceptionTypeData("Exception", "System.Exception", PostHandlingAction.ThrowNewException);
            exceptionPolicy1.ExceptionTypes.Add(exceptionTypeData);

            ehabSettings = new ExceptionHandlingSettings();
            ehabSettings.ExceptionPolicies.Add(exceptionPolicy1);

            updatableConfigurationSource.Add(ExceptionHandlingSettings.SectionName, ehabSettings);

            container = new UnityContainer();
            containerConfigurator = new UnityContainerConfigurator(container);
            EnterpriseLibraryContainer.ConfigureContainer(containerConfigurator, updatableConfigurationSource);
        }
		public void CanDeserializeSerializedConfiguration()
		{
			ExceptionHandlingSettings settings = new ExceptionHandlingSettings();

			ExceptionTypeData typeData11 = new ExceptionTypeData(typeName11, typeof(ArgumentNullException), PostHandlingAction.None);
			typeData11.ExceptionHandlers.Add(new LoggingExceptionHandlerData(handlerName111, handlerCategory111, 100, TraceEventType.Information, handlerMessage111, typeof(ExceptionFormatter), 101));

			ExceptionPolicyData policyData1 = new ExceptionPolicyData(policyName1);
			policyData1.ExceptionTypes.Add(typeData11);

			settings.ExceptionPolicies.Add(policyData1);

			IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();
			sections[ExceptionHandlingSettings.SectionName] = settings;
			IConfigurationSource configurationSource
				= ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

			ExceptionHandlingSettings roSettigs = (ExceptionHandlingSettings)configurationSource.GetSection(ExceptionHandlingSettings.SectionName);

			Assert.IsNotNull(roSettigs);
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Count);

			Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1));
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Count);

			Assert.IsNotNull(roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11));
			Assert.AreSame(typeof(ArgumentNullException), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).Type);
			Assert.AreEqual(1, roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Count);
			Assert.AreSame(typeof(LoggingExceptionHandlerData), roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111).GetType());
			Assert.AreEqual(100, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).EventId);
			Assert.AreEqual(typeof(ExceptionFormatter), ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).FormatterType);
			Assert.AreEqual(handlerCategory111, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).LogCategory);
			Assert.AreEqual(101, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Priority);
			Assert.AreEqual(TraceEventType.Information, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Severity);
			Assert.AreEqual(handlerMessage111, ((LoggingExceptionHandlerData)roSettigs.ExceptionPolicies.Get(policyName1).ExceptionTypes.Get(typeName11).ExceptionHandlers.Get(handlerName111)).Title);
		}
        public void TestCallHandlerCustomFactory()
        {
            PolicyInjectionSettings settings = new PolicyInjectionSettings();
            PolicyData policyData = new PolicyData("policy");
            ExceptionCallHandlerData data = new ExceptionCallHandlerData("exceptionhandler", "Swallow Exceptions");
            data.Order = 5;
            policyData.Handlers.Add(data);
            policyData.MatchingRules.Add(new CustomMatchingRuleData("matchesEverything", typeof(AlwaysMatchingRule)));
            settings.Policies.Add(policyData);

            ExceptionHandlingSettings ehabSettings = new ExceptionHandlingSettings();
            ExceptionPolicyData swallowExceptions = new ExceptionPolicyData("Swallow Exceptions");
            swallowExceptions.ExceptionTypes.Add(new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None));
            ehabSettings.ExceptionPolicies.Add(swallowExceptions);

            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();
            dictConfigurationSource.Add(PolicyInjectionSettings.SectionName, settings);

            IUnityContainer container = new UnityContainer().AddNewExtension<Interception>();
            settings.ConfigureContainer(container, dictConfigurationSource);
            new UnityContainerConfigurator(container).RegisterAll(dictConfigurationSource, ehabSettings);

            InjectionFriendlyRuleDrivenPolicy policy = container.Resolve<InjectionFriendlyRuleDrivenPolicy>("policy");

            ICallHandler handler
                = (policy.GetHandlersFor(new MethodImplementationInfo(null, (MethodInfo)MethodBase.GetCurrentMethod()), container)).ElementAt(0);

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.Order, data.Order);
        }
        public void CanCreateExceptionHandlerFromConfiguration()
        {
            PolicyInjectionSettings settings = new PolicyInjectionSettings();
            PolicyData policyData = new PolicyData("policy");
            policyData.Handlers.Add(new ExceptionCallHandlerData("exceptionhandler", "Swallow Exceptions"));
            policyData.MatchingRules.Add(new CustomMatchingRuleData("matchesEverything", typeof(AlwaysMatchingRule)));
            settings.Policies.Add(policyData);

            ExceptionHandlingSettings ehabSettings = new ExceptionHandlingSettings();
            ExceptionPolicyData swallowExceptions = new ExceptionPolicyData("Swallow Exceptions");
            swallowExceptions.ExceptionTypes.Add(new ExceptionTypeData("Exception", typeof(Exception), PostHandlingAction.None));
            ehabSettings.ExceptionPolicies.Add(swallowExceptions);
            DictionaryConfigurationSource dictConfigurationSource = new DictionaryConfigurationSource();
            dictConfigurationSource.Add(PolicyInjectionSettings.SectionName, settings);
            dictConfigurationSource.Add(ExceptionHandlingSettings.SectionName, ehabSettings);

            using (PolicyInjector injector = new PolicyInjector(dictConfigurationSource))
            {
                TargetType target = injector.Create<TargetType>();
                target.WillThrowException();
            }
        }
		public void SetUp()
		{
			settings = new ExceptionHandlingSettings();
			configurationSource = new DictionaryConfigurationSource();
			configurationSource.Add(ExceptionHandlingSettings.SectionName, settings);
		}
            private void EnsureExceptionHandlingSection()
            {
                exceptionSettingsSection = GetSectionOfType<ExceptionHandlingSettings>();

                if (exceptionSettingsSection == null)
                {
                    var section = new ExceptionHandlingSettings();
                    exceptionSettingsSection = SourceModel.AddSection(ExceptionHandlingSettings.SectionName, section);
                }
            }
        private ExceptionHandlerData GetExceptionHandlerData(ExceptionHandlingSettings settings, string policyName, string exceptionTypeName, string handlerName)
        {
            ExceptionHandlerDataCollection exceptionHandlerDataCollection = GetExceptionHandlerDataCollection(settings, policyName, exceptionTypeName);
            ExceptionHandlerData data = exceptionHandlerDataCollection[handlerName];
            if (data == null)
            {
                throw new ConfigurationException(SR.ExceptionExceptionHanlderNotFoun(handlerName, exceptionTypeName, policyName));
            }

            return data;
        }
 private static ExceptionTypeDataCollection GetExceptionTypeDataCollection(ExceptionHandlingSettings settings, string policyName)
 {
     ExceptionPolicyData exceptionPolicyData = GetExceptionPolicyData(settings, policyName);
     return exceptionPolicyData.ExceptionTypes;
 }
        private static ExceptionTypeData GetExceptionTypeData(ExceptionHandlingSettings settings, string policyName, string exceptionTypeName)
        {
            ExceptionTypeDataCollection exceptionTypeDataCollection = GetExceptionTypeDataCollection(settings, policyName);
            ExceptionTypeData exceptionTypeData = exceptionTypeDataCollection[exceptionTypeName];
            if (exceptionTypeData == null)
            {
                throw new ConfigurationException(SR.ExceptionExceptionTypeNotFound(exceptionTypeName, policyName));
            }

            return exceptionTypeData;
        }
        private static ExceptionPolicyData GetExceptionPolicyData(ExceptionHandlingSettings settings, string policyName)
        {
            ExceptionPolicyDataCollection exceptionPolicyDataCollection = settings.ExceptionPolicies;
            ExceptionPolicyData exceptionPolicyData = exceptionPolicyDataCollection[policyName];
            if (exceptionPolicyData == null)
            {
                throw new ConfigurationException(SR.ExceptionSimpleProviderNotFound(policyName));
            }

            return exceptionPolicyData;
        }