Ejemplo n.º 1
0
        private void Start(IConsulClient consulClient, ILoggerFactory logerFactory)
        {
            if (ServiceInfo != null)
            {
                var configFilePath = Path.Combine(ServiceInfo.Location, "config.json");
                ConfigurationManager.Setup(configFilePath, logerFactory);
                ServiceResourceManager.Load();
                CacheProviderFactory.Init();

                var lifetime = GetMicroserviceLifetime();
                lifetime?.Start();

                ServiceInfo.StartedAt = DateTime.Now;
            }

            consulClient.RegisterMicroServiceAsync(ServiceInfo);
        }
Ejemplo n.º 2
0
        private static void Load(string configFilePath)
        {
            var content = File.ReadAllText(configFilePath);

            Settings = JsonConvert.DeserializeObject <Settings>(content);

            var dir = Path.GetDirectoryName(configFilePath);

            Settings.API.Path = Path.Combine(dir, Settings.API.Path);
            Settings.CSI.Path = Path.Combine(dir, Settings.CSI.Path);
            Settings.CRI.Path = Path.Combine(dir, Settings.CRI.Path);

            Settings.Repository.Path = isDevelopment
                ? Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
                : Path.Combine(dir, Settings.Repository.Path);

            if (!Directory.Exists(Settings.API.Path))
            {
                Directory.CreateDirectory(Settings.API.Path);
            }
            if (!Directory.Exists(Settings.CSI.Path))
            {
                Directory.CreateDirectory(Settings.CSI.Path);
            }
            if (!Directory.Exists(Settings.CRI.Path))
            {
                Directory.CreateDirectory(Settings.CRI.Path);
            }
            if (!Directory.Exists(Settings.Repository.Path))
            {
                Directory.CreateDirectory(Settings.Repository.Path);
            }

            ServiceResourceManager.Load();

            OnLoaded?.Invoke(null, null);
        }
Ejemplo n.º 3
0
        public bool Execute(HttpContext context)
        {
            var           abPath = context.Request.Path.Value;
            ConfiguredAPI api;

            if (!ServiceResourceManager.TryMapRoute(abPath, out api))
            {
                LoggerManager.Logger.LogDebug($"No API is found for request `{abPath}`.");
                return(false);
            }
            LoggerManager.Logger.LogDebug($"API {api.name} is found to for the requst `{abPath}`.");

            if (context.Request.Method != api.method)
            {
                var message = string.Format("HTTP Method '{0}' is not support, expected: '{1}'", context.Request.Method, api.method);
                CompositeResponse(context, JsonConvert.SerializeObject(new { statusCode = 405, errorMessage = message }), 405);
            }
            else
            {
                ApiInvokingArgs invokingArgs = null;
                IdentityInfo    identity     = null;

                try
                {
                    var param = RequestDataParser.Parse(api.parameter.query, api.parameter.body, context);
                    invokingArgs = new ApiInvokingArgs(context, api, param);
                    ApiInvocationHandler.OnInvoking(invokingArgs);

                    var result = ExcuteApi(api, context, param);
                    CompositeResponse(context, JsonConvert.SerializeObject(result));
                    identity = context.Items.ContainsKey("identity") ? context.Items["identity"] as IdentityInfo : null;

                    ApiInvocationHandler.OnInvoked(new ApiInvokedArgs(invokingArgs, identity, result));
                }
                catch (LogicalException ex)
                {
                    var result = ResponseResultResolver.Resolve(context, ex.ErrorCode, ex.Message, null);
                    CompositeResponse(context, JsonConvert.SerializeObject(result));
                    identity = context.Items.ContainsKey("identity") ? context.Items["identity"] as IdentityInfo : null;

                    var parameters = invokingArgs?.Param != null?JsonConvert.SerializeObject(invokingArgs?.Param, Formatting.Indented) : "";

                    ApiInvocationHandler.OnInvoked(new ApiInvokedArgs(invokingArgs, identity, result));
                }
                catch (Exception ex)
                {
                    var exception = ex is TargetInvocationException ? ex.InnerException : ex;

                    var message = ConfigurationManager.Settings.API.ShowFullException ? exception.ToString() : "服务出错。";
                    var result  = ResponseResultResolver.Resolve(context, "500", message, null);
                    CompositeResponse(context, JsonConvert.SerializeObject(result));

                    var parameters = invokingArgs?.Param != null?JsonConvert.SerializeObject(invokingArgs?.Param, Formatting.Indented) : "";

                    LoggerManager.Logger.LogError(exception, $"Execute request `{abPath}` failed, data: {Environment.NewLine}{parameters}. {Environment.NewLine}{exception}");
                    identity = context.Items.ContainsKey("identity") ? context.Items["identity"] as IdentityInfo : null;
                    ApiInvocationHandler.OnError(new ApiExceptionArgs(invokingArgs, identity, exception));
                }
            }

            return(true);
        }