示例#1
0
        public Endpoint Create(Assembly modelsAssembly)
        {
            var endpoint = new Endpoint();
            endpoint.Name = modelsAssembly.GetName().Name;
            endpoint.AssemblyGuid = Guid.Parse(modelsAssembly.GetCustomAttribute<GuidAttribute>().Value);
            endpoint.AssemblyVersion = modelsAssembly.GetName().Version.ToString();

            BuildEndpointRoutes(endpoint);

            // models must be built after routes
            BuildEndpointModels(endpoint, modelsAssembly);

            return endpoint;
        }
 public WebQueueServiceWrapper(Endpoint endpoint)
 {
     this.queueEndpoint = endpoint;
 }
示例#3
0
 private void BuildEndpointModels(Endpoint endpoint, Assembly modelsAssembly)
 {
     foreach (var route in endpoint.Routes) {
         AddModelMetadataForType(endpoint, modelsAssembly, route.QualfiedReturnTypeName);
     }
 }
示例#4
0
        private void AddModelMetadataForType(Endpoint endpoint, Assembly modelsAssembly, string qualifiedTypeName)
        {
            // check the loaded GAC assemblies first
            Type type = Type.GetType(qualifiedTypeName);

            // if it's local to the assembly the type resolver won't find it
            if (type == null) {
                // try again with the local assembly resolver
                type = modelsAssembly.GetType(qualifiedTypeName);

                // if that pig is still null, it deserves to fail. wtf.
                if (type == null) {
                    return;
                }
            }

            // alright, strip out any wrapping types
            while (type != null && type.Namespace.StartsWith("System")) {
                // your namespace is system? ugh, see if you have any generic parameters
                var generics = type.GetGenericArguments();

                if (generics.Count() == 1) {
                    type = generics.First();
                }
                else {
                    type = null;
                }
            }

            if (type == null) {
                return;
            }

            Model model = new Model();
            model.Name = type.Name;
            model.QualifiedName = type.FullName;

            if (endpoint.Models.Any(x => x.QualifiedName == model.QualifiedName)) {
                return;
            }

            foreach (var propertyInfo in type.GetProperties()) {
                ModelProperty property = new ModelProperty();
                property.Name = propertyInfo.Name;
                property.QualifiedTypeName = propertyInfo.PropertyType.FullName;

                foreach (var attributeInfo in propertyInfo.GetCustomAttributes()) {
                    ModelPropertyAttribute attribute = new ModelPropertyAttribute();
                    attribute.Name = attributeInfo.GetType().Name;

                    // add this attribute to the property
                    property.Attributes.Add(attribute);
                }

                // this property itself might be a model, so try to add it, just in case
                AddModelMetadataForType(endpoint, modelsAssembly, property.QualifiedTypeName);

                // add this property to the model
                model.Properties.Add(property);
            }

            endpoint.Models.Add(model);
        }
示例#5
0
        private void BuildEndpointRoutes(Endpoint endpoint)
        {
            foreach (var routeEntry in ObservableDirectRouteProvider.Singleton.DirectRoutes) {
                Route route = new Route();
                route.PathFormat = routeEntry.Route.RouteTemplate;

                // skip metadata items, they're internal use only
                if (route.PathFormat.StartsWith("metadata", StringComparison.OrdinalIgnoreCase)) {
                    continue;
                }

                var descriptors = routeEntry.Route.DataTokens["actions"] as HttpActionDescriptor[];
                if (descriptors == null || descriptors.Count() > 1) {
                    continue;
                }

                var descriptor = descriptors.First();

                // set the return type
                route.QualfiedReturnTypeName = descriptor.ReturnType.FullName;
                route.MethodName = descriptor.ActionName;

                // set supported calling methods, presumably there's only one, but just in case
                bool isFirstHttpMethod = true;
                foreach (var httpMethod in descriptor.SupportedHttpMethods) {
                    HttpRequestMethod requestMethod;

                    if (Enum.TryParse<HttpRequestMethod>(httpMethod.Method, true, out requestMethod)) {
                        if (isFirstHttpMethod) {
                            route.HttpMethod = requestMethod;
                            isFirstHttpMethod = false;
                        }
                        else {
                            route.HttpMethod ^= requestMethod;
                        }
                    }
                }

                if (isFirstHttpMethod) {
                    // you got through that and don't have any supported methods, bollux!
                    continue;
                }

                // now do parameters
                foreach (var parameterDescriptor in descriptor.GetParameters()) {
                    RouteParameter parameter = new RouteParameter();
                    parameter.Name = parameterDescriptor.ParameterName;
                    parameter.QualifiedTypeName = parameterDescriptor.ParameterType.FullName;

                    // TODO: Add attributes

                    route.Parameters.Add(parameter);
                }

                // now check for extended query options
                var queryOptionsAttributes = descriptor.GetCustomAttributes<AcceptQueryOptionsAttribute>();
                if (queryOptionsAttributes != null && queryOptionsAttributes.Any()) {
                    var queryOption = queryOptionsAttributes.First().Options;

                    route.AcceptedQueryOptions = queryOption;
                }

                endpoint.Routes.Add(route);
            }
        }