public void Setup()
 {
     this.policyInjector =
         new PolicyInjector(new UnityServiceLocator(
                                new UnityContainer()
                                .AddNewExtension <Interception>()
                                .AddNewExtension <TransientPolicyBuildUpExtension>()));
 }
Example #2
0
        static ApiClient()
        {
            //设置策略注入
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            var injector = new PolicyInjector(configurationSource);

            PolicyInjection.SetPolicyInjector(injector);
        }
Example #3
0
        private void AddExceptionPolicy(PolicyInjector factory, string exceptionPolicyName, params IMatchingRule[] rules)
        {
            RuleDrivenPolicy exceptionPolicy = new RuleDrivenPolicy();

            exceptionPolicy.RuleSet.AddRange(rules);
            exceptionPolicy.Handlers.Add(new ExceptionCallHandler(exceptionPolicyName));

            factory.Policies.Add(exceptionPolicy);
        }
Example #4
0
        private void AddNoopPolicy(PolicyInjector factory, params IMatchingRule[] rules)
        {
            RuleDrivenPolicy p = new RuleDrivenPolicy("Noop");

            p.RuleSet.AddRange(rules);
            p.Handlers.Add(new NoopCallHandler());

            factory.Policies.Add(p);
        }
Example #5
0
        static APIClient()
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            var injector = new PolicyInjector(configurationSource);

            try {
                PolicyInjection.SetPolicyInjector(injector);
            } catch { }
        }
Example #6
0
        private void AddPropertiesPolicy(PolicyInjector factory)
        {
            RuleDrivenPolicy       policy = new RuleDrivenPolicy("Intercept balance policy");
            MemberNameMatchingRule rule   = new MemberNameMatchingRule("get_Balance");

            policy.RuleSet.Add(rule);

            countHandler = new CallCountHandler();
            policy.Handlers.Add(countHandler);
            factory.Policies.Add(policy);
        }
Example #7
0
        private void AddOverloadsPolicy(PolicyInjector factory)
        {
            RuleDrivenPolicy         policy  = new RuleDrivenPolicy("NullStringPolicy");
            TagAttributeMatchingRule tagRule = new TagAttributeMatchingRule("NullString");

            policy.RuleSet.Add(tagRule);

            policy.Handlers.Add(new MakeReturnNullHandler());

            factory.Policies.Add(policy);
        }
Example #8
0
        public static Operation GetInstance()
        {
            if (single == null)
            {
                IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
                PolicyInjector       policyInjector      = new PolicyInjector(configurationSource);
                PolicyInjection.SetPolicyInjector(policyInjector);

                single = PolicyInjection.Create <Operation>();
            }

            return(single);
        }
Example #9
0
        public void Setup()
        {
            this.callCounter = new CallCounter();

            var container = new UnityContainer();

            container.AddNewExtension <Interception>();
            container.AddNewExtension <TransientPolicyBuildUpExtension>();
            container.RegisterInstance <CallCounter>(this.callCounter);
            var serviceLocator = new UnityServiceLocator(container);

            this.policyInjector = new PolicyInjector(serviceLocator);
        }
Example #10
0
        public void Setup()
        {
            this.handler = new CallCountHandler();

            var container = new UnityContainer();

            container.AddNewExtension <Interception>();
            container.AddNewExtension <TransientPolicyBuildUpExtension>();
            container.Configure <Interception>()
            .AddPolicy("policy")
            .AddMatchingRule(new AlwaysMatchingRule())
            .AddCallHandler(this.handler);

            this.policyInjector = new PolicyInjector(new UnityServiceLocator(container));
        }
Example #11
0
 /// <summary>
 /// Stores the configuration source that should be used to derive the
 /// <see cref="PolicySet"/> for interception.
 /// </summary>
 /// <param name="configSource">The configuration source.</param>
 public void SetPolicyConfigurationSource(IConfigurationSource configSource)
 {
     if (this.configSource == null || this.configSource != configSource)
     {
         lock (policiesLock)
         {
             if (this.configSource == null || this.configSource != configSource)
             {
                 this.configSource = configSource;
                 PolicyInjectorFactory injectorFactory =
                     new PolicyInjectorFactory(configSource);
                 injector = injectorFactory.Create();
             }
         }
     }
 }
 /// <summary>
 /// Stores the configuration source that should be used to derive the
 /// <see cref="PolicySet"/> for interception.
 /// </summary>
 /// <param name="configSource">The configuration source.</param>
 public void SetPolicyConfigurationSource(IConfigurationSource configSource)
 {
     if (this.configSource == null || this.configSource != configSource)
     {
         lock (policiesLock)
         {
             if (this.configSource == null || this.configSource != configSource)
             {
                 this.configSource = configSource;
                 PolicyInjectorFactory injectorFactory =
                     new PolicyInjectorFactory(configSource);
                 injector = injectorFactory.Create();
             }
         }
     }
 }
        public void ShouldLogOnlyToCategoriesGivenInConfig()
        {
            using (var configSource = new FileConfigurationSource("LogCallHandler.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        LoggingTarget target = injector.Create<LoggingTarget>();
                        target.DoSomething(1, "two", 3.0);
                    }

                    Assert.AreEqual(1, eventLog.NewEntries().Select(le => le.Category == "Default Category").Count());
                }
            }
        }
        public void ShouldLogOnlyToCategoriesGivenInConfig()
        {
            using (var configSource = new FileConfigurationSource("LogCallHandler.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        LoggingTarget target = injector.Create <LoggingTarget>();
                        target.DoSomething(1, "two", 3.0);
                    }

                    Assert.AreEqual(1, eventLog.NewEntries().Select(le => le.Category == "Default Category").Count());
                }
            }
        }
        public void ShouldCombineWithPoliciesDefinedInConfiguration()
        {
            using (var configSource = new FileConfigurationSource("CombinesWithConfig.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        var typeWhichUndergoesLoggingOnMethodCall =
                            injector.Create <TypeWhichUndergoesAttributeBasedLogging>();

                        typeWhichUndergoesLoggingOnMethodCall.TestMethod();

                        typeWhichUndergoesLoggingOnMethodCall.MyProperty = "hello";
                    }

                    Assert.AreEqual(2,
                                    eventLog.NewEntries().Select(le => le.Message.Contains("This is before the call")).
                                    Count());
                }
            }
        }
        public void ShouldCombineWithPoliciesDefinedInConfiguration()
        {
            using (var configSource = new FileConfigurationSource("CombinesWithConfig.config", false))
            {
                using (var eventLog = new EventLogTracker("Application"))
                {
                    using (var injector = new PolicyInjector(configSource))
                    {
                        var typeWhichUndergoesLoggingOnMethodCall =
                            injector.Create<TypeWhichUndergoesAttributeBasedLogging>();

                        typeWhichUndergoesLoggingOnMethodCall.TestMethod();

                        typeWhichUndergoesLoggingOnMethodCall.MyProperty = "hello";
                    }

                    Assert.AreEqual(2,
                                    eventLog.NewEntries().Select(le => le.Message.Contains("This is before the call")).
                                        Count());
                }
            }
        }
Example #17
0
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            PolicyInjector policyInjector = null;

            if (string.IsNullOrEmpty(this._policyInjectorName))
            {
                policyInjector = new PolicyInjectorFactory().Create();
            }
            else
            {
                policyInjector = new PolicyInjectorFactory().Create(this._policyInjectorName);
            }

            Type   serviceType     = instanceContext.Host.Description.ServiceType;
            object serviceInstance = Activator.CreateInstance(serviceType);

            if (!this._serviceContractType.IsInterface && !serviceType.IsMarshalByRef && policyInjector is RemotingPolicyInjector)
            {
                return(serviceInstance);
            }

            return(policyInjector.Wrap(serviceInstance, this._serviceContractType));
        }
Example #18
0
 public void Setup()
 {
     this.container = new MockUnityContainer();
     this.container.AddNewExtension <Interception>();
     this.policyInjector = new PolicyInjector(new UnityServiceLocator(this.container));
 }
        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();
            }
        }
Example #20
0
        private void AddCallCountingDalPolicy(PolicyInjector factory)
        {
            RuleDrivenPolicy typeMatchPolicy = GetCallCountingPolicy();

            factory.Policies.Add(typeMatchPolicy);
        }