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> >(); }
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); }
public override string GetControllerName(HttpRequestMessage request) { ContractUtility.Requires <ArgumentNullException>(request.IsNotNull(), "request cannot be null"); if (_controllerNameInRequestObject.IsNullOrEmpty()) { _controllerNameInRequestObject = GetControllerNameFromRequest(request); if (_controllerNameInRequestObject.IsNullOrEmpty()) { HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.NotFound, "Controller not found", request); } } if (_controllerVersionInRequestObject == 0) { _controllerVersionInRequestObject = GetControllerVersion(request); } IEnumerable <ControllerXML> controllerXMLsInControllersXML = _controllersXML.ControllerXMLs.Where(x => x.EntityName.Equals(_controllerNameInRequestObject, StringComparison.InvariantCultureIgnoreCase)); if (controllerXMLsInControllersXML.IsNullOrEmpty()) { return(string.Empty); } controllerXMLsInControllersXML.ToList().ForEach(x => { x.Version = x.Version.Replace("v", string.Empty).Replace("V", string.Empty); } ); ControllerXML controllerXMLForSuppliedVersionNumber = null; if (_controllerVersionInRequestObject == 0) { double maxVersionNumber = controllerXMLsInControllersXML.Max(x => Convert.ToDouble(x.Version.IsNotNullOrEmpty() ? x.Version : "0")); controllerXMLForSuppliedVersionNumber = _controllersXML.ControllerXMLs.SingleOrDefault(x => x.EntityName == _controllerNameInRequestObject && (x.Version.IsNotNullOrEmpty() ? x.Version : "0") == maxVersionNumber.ToString()); } else { controllerXMLForSuppliedVersionNumber = _controllersXML.ControllerXMLs.SingleOrDefault(x => x.EntityName == _controllerNameInRequestObject && x.Version == _controllerVersionInRequestObject.ToString()); } if (controllerXMLForSuppliedVersionNumber.IsNull()) { return(string.Empty); } return(controllerXMLForSuppliedVersionNumber.ControllerName.IsNotNullOrEmpty() ? controllerXMLForSuppliedVersionNumber.ControllerName : _controllerNameInRequestObject); }
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { IHttpController httpController = null; string controllerName = controllerDescriptor.ControllerName.IsNotNullOrEmpty() ? controllerDescriptor.ControllerName : controllerDescriptor.ControllerType.Name; try { httpController = Container.Instance.Resolve(controllerDescriptor.ControllerType) as IHttpController; if (httpController.IsNull()) { httpController = Container.Instance.Resolve(controllerDescriptor.ControllerType, controllerDescriptor.ControllerName) as IHttpController; } } catch { httpController = Container.Instance.Resolve(controllerDescriptor.ControllerType, controllerDescriptor.ControllerName) as IHttpController; } if (httpController.IsNull()) { HttpResponseUtility.ThrowHttpResponseError(HttpStatusCode.InternalServerError, controllerName + " not registered in Unity DI container", request); } return(httpController); }