コード例 #1
0
ファイル: ExposedRestServerAction.cs プロジェクト: naice/Jens
 public ExposedRestServerAction(ExposedRestServerService restServerService, MethodInfo methodInfo)
 {
     RestServerService = restServerService;
     _methodInfo       = methodInfo;
 }
コード例 #2
0
        private void TryExposeRestServerService(Type RestServerServiceType, RestServerServiceInstanceType instanceType)
        {
            ExposedRestServerService exposedRestServerService = new ExposedRestServerService(_restServerDependencyResolver)
            {
                ServiceType  = RestServerServiceType,
                InstanceType = instanceType,
            };

            foreach (var method in RestServerServiceType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                var methodAttribute = method.GetCustomAttribute <RestServerServiceCallAttribute>();
                if (methodAttribute == null)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(methodAttribute.Route))
                {
                    throw new ArgumentException($"Invalid route given: {methodAttribute.Route}", nameof(RestServerServiceCallAttribute.Route));
                }

                var routeStr = MakeRoute(methodAttribute.Route);

                if (IsRouteAlreadyRegistered(routeStr))
                {
                    throw new ArgumentException($"Route already registered. {routeStr}", nameof(RestServerServiceCallAttribute.Route));
                }

                Type inputType       = null;
                bool isBodyRequested = false;
                var  parameters      = method.GetParameters();

                if (parameters != null && parameters.Length > 2)
                {
                    _logger.Warn($"{nameof(RestServerServiceRouteHandler)}: Method Parameter missmatch. Too many parameters. {routeStr}");
                    continue;
                }

                if (parameters != null && parameters.Length > 0)
                {
                    var parameterInfo     = parameters[0];
                    var parameterTypeInfo = parameterInfo.ParameterType.GetTypeInfo();
                    if (!parameterTypeInfo.IsClass)
                    {
                        _logger.Warn($"{nameof(RestServerServiceRouteHandler)}: Method Parameter missmatch. Parameter is no class! {routeStr}");
                        continue;
                    }

                    inputType = parameterInfo.ParameterType;
                    if (inputType == typeof(string))
                    {
                        isBodyRequested = true;
                    }
                }

                if (parameters != null && parameters.Length == 2)
                {
                    if (parameters[1].ParameterType != typeof(string))
                    {
                        _logger.Warn($"{nameof(RestServerServiceRouteHandler)}: Method Parameter missmatch. Two parameters found but the last one is no string. {routeStr}");
                        continue;
                    }
                    isBodyRequested = true;
                }

                var exposedRestServerAction = new ExposedRestServerAction(exposedRestServerService, method)
                {
                    IsBodyRequested = isBodyRequested,
                    OutputType      = method.ReturnType,
                    InputType       = inputType,
                    Route           = routeStr,
                    Methods         = methodAttribute.Methods?.Split(',').Select(str => str.Trim().ToUpper()).ToArray() ?? new string[0]
                };
                exposedRestServerAction.CompileInputParameters();
                exposedRestServerService.Routes.Add(exposedRestServerAction);

                _logger.Info($"{nameof(RestServerServiceRouteHandler)}: \"{RestServerServiceType.FullName}\" exposed API method \"{exposedRestServerAction.Route.Replace("{", "{{").Replace("}", "}}")}\".");
            }

            // We got any routes exposed?
            if (exposedRestServerService.Routes.Count > 0)
            {
                if (exposedRestServerService.InstanceType == RestServerServiceInstanceType.SingletonStrict)
                {
                    exposedRestServerService.GetInstance(null);
                }

                _exposedRestServerServices.Add(exposedRestServerService);
            }
        }