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));
        }
Beispiel #2
0
        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            if (_controllerNameInRequestObject.IsNullOrEmpty())
            {
                _controllerNameInRequestObject = GetControllerNameFromRequest(request);
                if (_controllerNameInRequestObject.IsNullOrEmpty())
                {
                    HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.NotFound, "Controller not found", request);
                }
            }
            if (_controllerVersionInRequestObject == 0)
            {
                _controllerVersionInRequestObject = GetControllerVersion(request);
            }
            string cacheKey = _controllerNameInRequestObject + ":" + _controllerVersionInRequestObject + ":" + request.Method.ToString();

            if (_cache.Contains(cacheKey))
            {
                return(_cache.Get(cacheKey));
            }
            string controllerName = GetControllerName(request);

            if (controllerName.IsNullOrEmpty())
            {
                HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.NotFound, "Controller not found", request);
            }
            string currentAssemblyName = this.GetType().Assembly.GetName().Name;
            Type   controllerType      = null;

            try
            {
                controllerType = GetControllerTypeForControllerNotPresentInControllersXMLFile(request, controllerName);
                if (controllerType.IsNull())
                {
                    controllerType = MetaDataUtility.GetType(currentAssemblyName, controllerName);
                }
            }
            catch
            {
                controllerType = MetaDataUtility.GetType(currentAssemblyName, controllerName);
            }
            if (controllerType.IsNull())
            {
                HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.NotFound, controllerName + " not found", request);
            }
            HttpControllerDescriptor httpControllerDescriptor = new HttpControllerDescriptor();

            httpControllerDescriptor.Configuration  = _configuration;
            httpControllerDescriptor.ControllerType = controllerType;
            httpControllerDescriptor.ControllerName = controllerType.Name;
            _cache.Add(cacheKey, httpControllerDescriptor);
            return(httpControllerDescriptor);
        }
Beispiel #3
0
        private Type GetControllerTypeForControllerNotPresentInControllersXMLFile(HttpRequestMessage request, string controllerName)
        {
            string domainAssemblyName = DomainAssemblyName.IsNotNullOrEmpty() ? DomainAssemblyName : typeof(BaseEntity <>).Assembly.GetName().Name;

            string[] genericArgumentsClassNames = new string[1];
            genericArgumentsClassNames[0] = controllerName;
            if (request.Method == HttpMethod.Get)
            {
                EntityXML entityXML = _entitiesXML.EntityXMLs.SingleOrDefault(x => x.Name.Equals(controllerName, StringComparison.InvariantCultureIgnoreCase));
                string    TIdType   = entityXML.IsNotNull() ? entityXML.IdType : _entitiesXML.DefaultIDType;
                Type[]    extraExplicitGenericArgumentTypes = new Type[1];
                extraExplicitGenericArgumentTypes[0] = MetaDataUtility.GetType("mscorlib", TIdType);
                return(MetaDataUtility.GetGenericType(typeof(RestfulAPIQuery <,>), domainAssemblyName, genericArgumentsClassNames, extraExplicitGenericArgumentTypes));
            }
            else
            {
                return(MetaDataUtility.GetGenericType(typeof(RestfulAPICommand <>), domainAssemblyName, genericArgumentsClassNames));
            }
            //TODO - Need to take care of the Elastic Search API Controller as well
        }