Esempio n. 1
0
        private void CheckConfigRefresh(string projectName = null)
        {
            var jsonFile = string.IsNullOrEmpty(projectName)
                ? new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, ConfigHelper.DefaultAppSettingsFile))
                : new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, projectName,
                                            ConfigHelper.DefaultAppSettingsFile));
            var jsonLastTime = string.IsNullOrEmpty(projectName) ? _appJsonLastWriteTime : _currentAppJsonLastWriteTime;

            if (jsonFile.Exists && (jsonLastTime == null || jsonLastTime != jsonFile.LastWriteTime))
            {
                try
                {
                    if (string.IsNullOrEmpty(projectName))
                    {
                        _appJsonLastWriteTime = jsonFile.LastWriteTime;
                        this._appsettingsJson =
                            Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(
                                CopyHelper.ReadAllText(jsonFile.FullName));
                    }
                    else
                    {
                        _currentAppJsonLastWriteTime = jsonFile.LastWriteTime;
                        this._currentAppsettingsJson =
                            Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(
                                CopyHelper.ReadAllText(jsonFile.FullName));
                    }
                }
                catch (Exception e)
                {
                    logger.Info(e.ToString());
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 文件的内容
        /// </summary>
        public string GetContent()
        {
            if (FileInfo != null)
            {
                Content = CopyHelper.ReadAllText(FilePath);
                return(Content);
            }

            return(string.Empty);
        }
Esempio n. 3
0
        public async Task <RenderResult> RenderAsync(string path, object model, dynamic viewBag, RouteValueDictionary routevalues, string area,
                                                     ViewType viewType)
        {
            var re = new RenderResult
            {
                Html   = "",
                Status = 200
            };

            if (string.IsNullOrEmpty(path) || path.Equals("/"))
            {
                return(re);
            }
            var nomarlPath = path;

            try
            {
                if (routevalues != null && routevalues.ContainsKey("url"))
                {
                    path = routevalues["url"].ToString();
                }

                //只拿第一层路径
                var entryPointName = path.Split('/').FirstOrDefault();
                if (string.IsNullOrEmpty(entryPointName))
                {
                    return(re);
                }

                entryPointName = entryPointName.ToLowerInvariant();
                var indexHtml = new FileModel(_hostingEnvironment, entryPointName, "index.html");
                if (!indexHtml.IsExist)
                {
                    return(re);
                }

                var jsonFile = new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, "appsettings.json"));
                if (jsonFile.Exists && (_appJsonLastWriteTime == null || _appJsonLastWriteTime != jsonFile.LastWriteTime))
                {
                    _appJsonLastWriteTime = jsonFile.LastWriteTime;
                    try
                    {
                        this._appsettingsJson = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(CopyHelper.ReadAllText(jsonFile.FullName));
                    }
                    catch (Exception e)
                    {
                        logger.Info(e.ToString());
                    }
                }

                var html = indexHtml.GetContent();
                re.Html = html;

                var cacheKey = entryPointName + "_" + indexHtml.LastModifyTime.ToString("yyyMMddHHmmss");

                var     jsFileContent  = new FileModel(_hostingEnvironment, entryPointName, "server.js");
                dynamic serverJsResult = null;
                if (jsFileContent.IsExist)
                {
                    var exports = engine
                                  .CommonJS()
                                  .RegisterInternalModule("server", typeof(Server))
                                  .RunMain(jsFileContent.FilePath);

                    try
                    {
                        var jsValue = exports.AsObject().Get("main").Invoke(new JsValue(nomarlPath)).ToString();
                        if (!string.IsNullOrEmpty(jsValue) && jsValue != "null" && jsValue != "undefined")
                        {
                            serverJsResult = JObject.Parse(jsValue);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.ToString());
                    }
                }

                if (serverJsResult == null)
                {
                    serverJsResult = new JObject();
                }

                serverJsResult.Env = new JObject();
                if (_appsettingsJson != null)
                {
                    foreach (var jsonItem in _appsettingsJson)
                    {
                        serverJsResult.Env[jsonItem.Key] = jsonItem.Value;
                    }
                }

                try
                {
                    var cacheResult = _engine.Handler.Cache.RetrieveTemplate(cacheKey);
                    if (cacheResult.Success)
                    {
                        var itemple = cacheResult.Template.TemplatePageFactory();
                        itemple.DisableEncoding = true;
                        string result2 = await _engine.RenderTemplateAsync(itemple, serverJsResult);

                        re.Html = result2;
                        return(re);
                    }

                    string result = await _engine.CompileRenderStringAsync(cacheKey, html, serverJsResult);

                    if (!cacheList.TryGetValue(entryPointName, out var oldCache))
                    {
                        cacheList.TryAdd(entryPointName, cacheKey);
                    }
                    else
                    {
                        //之前有缓存了 就清掉
                        _engine.Handler.Cache.Remove(oldCache);
                        cacheList[entryPointName] = cacheKey;
                    }

                    re.Html = result;
                }
                catch (Exception e)
                {
                    logger.Error(e.ToString());
                }
            }
            catch (Exception e1)
            {
                //ignore
                logger.Error(e1.ToString());
            }

            return(re);
        }