コード例 #1
0
        static VersionHandlingHttpControllerSelector()
        {
            Assembly  currentAssembly         = typeof(VersionHandlingHttpControllerSelector).Assembly;
            string    currentAssemblyLocation = currentAssembly.Location;
            string    currentAssemblyName     = currentAssembly.GetName().Name;
            string    controllersXMLPath      = ControllersXMLPath.IsNotNullOrEmpty() ? ControllersXMLPath : Path.Combine(currentAssemblyLocation, "ControllerDispatchment", "ControllerEntityVersionsAndNamesMapping", "ControllerEntityVersionsAndNamesMapping.xml");
            XDocument xDocForControllersXML   = XDocument.Load(controllersXMLPath);

            _controllersXML = XMLUtility.DeSerialize <ControllersXML>(xDocForControllersXML.ToString());
            string controllersXSDPath         = ControllersXSDPath.IsNotNullOrEmpty() ? ControllersXSDPath : ("RestfulWebAPI.ControllerDispatchment.ControllerEntityVersionsAndNamesMapping.ControllerEntityVersionsAndNamesMapping.xsd");
            string controllersXSDAssemblyName = ControllersXSDAssemblyName.IsNotNullOrEmpty() ? ControllersXSDAssemblyName : currentAssemblyName;
            string xmlValidationErrors        = XSDUtility.Validate(controllersXSDAssemblyName, ControllersXSDPath, xDocForControllersXML.ToString());

            if (xmlValidationErrors.IsNotNullOrEmpty())
            {
                HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.InternalServerError, xmlValidationErrors);
            }

            string    entitiesXMLPath    = EntitiesXMLPath.IsNotNullOrEmpty() ? EntitiesXMLPath : Path.Combine(currentAssemblyLocation, "ControllerDispatchment", "ControllerEntityVersionsAndNamesMapping", "EntitiesAndIDTypesMapping.xml");
            XDocument xDocForEntitiesXML = XDocument.Load(entitiesXMLPath);

            _entitiesXML = XMLUtility.DeSerialize <EntitiesXML>(xDocForEntitiesXML.ToString());
            string entitiesXSDPath         = EntitiesXSDPath.IsNotNullOrEmpty() ? EntitiesXSDPath : ("RestfulWebAPI.ControllerDispatchment.ControllerEntityVersionsAndNamesMapping.EntitiesAndIDTypesMapping.xsd");
            string entitiesXSDAssemblyName = EntitiesXSDAssemblyName.IsNotNullOrEmpty() ? EntitiesXSDAssemblyName : currentAssemblyName;

            xmlValidationErrors = XSDUtility.Validate(entitiesXSDAssemblyName, entitiesXSDPath, xDocForEntitiesXML.ToString());
            if (xmlValidationErrors.IsNotNullOrEmpty())
            {
                HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.InternalServerError, xmlValidationErrors);
            }

            //Register this cache in Unity DI Container with Singleton lifetime
            _cache = Container.Instance.Resolve <ICache <string, HttpControllerDescriptor> >();
        }
        private static PolicyBuilder BuildPolicyBuilderFromPollyTransientFailureExceptionsXMLFile()
        {
            return(ExceptionHandlingUtility.WrapFuncWithExceptionHandling(() =>
            {
                PolicyBuilder policyBuilder = null;
                XDocument xDoc = XDocument.Load(Path.Combine(typeof(BasicPollyExceptionHandler).Assembly.Location, "ExceptionHandling", "PollyBasedExceptionHandling", "PollyTransientFailureExceptions.xml"));
                _pollyTransientFailureExceptions = XMLUtility.DeSerialize <PollyTransientFailureExceptions>(xDoc.ToString());
                _splittedTransientFailureExceptions = _pollyTransientFailureExceptions.TransientFailureExceptions.SelectMany(x => x.CommaSeperatedTransientFailureExceptions.Split(",", StringSplitOptions.RemoveEmptyEntries)).Distinct();

                if (_splittedTransientFailureExceptions.IsNotNullOrEmpty())
                {
                    string firstTransientFailureException = _splittedTransientFailureExceptions.First();
                    string assemblyName = _pollyTransientFailureExceptions.TransientFailureExceptions.SingleOrDefault(x => x.CommaSeperatedTransientFailureExceptions.Contains(firstTransientFailureException)).AssemblyName;
                    Type firstTransientFailureExceptionType = MetaDataUtility.GetType(assemblyName, firstTransientFailureException);
                    Type[] transientFailureExceptionTypesArray = new Type[1];
                    transientFailureExceptionTypesArray[0] = firstTransientFailureExceptionType;
                    policyBuilder = MetaDataUtility.InvokeStaticMethod <Policy, PolicyBuilder>("Handle", transientFailureExceptionTypesArray);

                    IEnumerable <string> transientFailureExceptionsOtherThanTheFirst = _splittedTransientFailureExceptions.Skip(1);
                    if (transientFailureExceptionsOtherThanTheFirst.IsNotNullOrEmpty())
                    {
                        transientFailureExceptionsOtherThanTheFirst.ForEach(x =>
                        {
                            assemblyName = _pollyTransientFailureExceptions.TransientFailureExceptions.SingleOrDefault(y => y.CommaSeperatedTransientFailureExceptions.Contains(x)).AssemblyName;
                            Type transientFailureExceptionTypeForOtherThanTheFirst = MetaDataUtility.GetType(assemblyName, x);
                            Type[] transientFailureExceptionTypesArrayForOtherThanTheFirst = new Type[1];
                            transientFailureExceptionTypesArrayForOtherThanTheFirst[0] = transientFailureExceptionTypeForOtherThanTheFirst;
                            policyBuilder = MetaDataUtility.InvokeInstanceMethod <PolicyBuilder, PolicyBuilder>(policyBuilder, "Or", transientFailureExceptionTypesArrayForOtherThanTheFirst);
                        }
                                                                            );
                    }
                }
                return policyBuilder;
            }, _staticLoggerInstance));
        }