ServiceStack:视图中未使用的自定义应用设置

本文关键字:自定义 应用 设置 未使用 视图 ServiceStack | 更新日期: 2023-09-27 18:15:49

我与ServiceStack相处得很好,但遇到了一个问题,我目前无法工作。在Global.asax.cs Configure()方法中,我声明了一个基于数据库的AppSettings,如下所示:

            // Create app settings, based on dictionary settings from central database
            Dictionary<string, string> configSettings = null;
            using (var db = container.Resolve<IDbConnectionFactory>().Open())
            {
                configSettings = db.Dictionary<string, string>(db.From<ConfigSetting>());
            }
            var dicSettings = new DictionarySettings(configSettings);
            // Register app settings for injection
            container.Register<IAppSettings>(dicSettings);

这在注入AppSettings的代码中工作得很好:

public class RemoteEmailValidator : IEmailValidator
{
    public IAppSettings AppSettings { get; set; }
    public bool ValidEmail(string email)
    {
        try
        {
            if (String.IsNullOrEmpty(email) || email.Length > 256)
            {
                return false;
            }
            RestClient client = new RestClient();
            string baseURL = AppSettings.Get("MailgunAPIBaseURL", "");
            if (String.IsNullOrEmpty(baseURL))
            {
                // If we can't check email in-depth, then it's OK
                return true;
            }
            else
            {
                client.BaseUrl = new Uri(baseURL);
                client.Authenticator =
                        new HttpBasicAuthenticator(AppSettings.Get("MailgunAPIUserName", ""),
                                                   AppSettings.Get("MailgunAPIPublicKey", ""));
                RestRequest request = new RestRequest();
                request.Resource = AppSettings.Get("MailgunAPIEmailValidationResource", "");
                request.AddParameter("address", email);
                var response = client.Execute(request);
                dynamic content = DynamicJson.Deserialize(response.Content);
                return content.is_valid == "true";
            }
        }
        catch { } // Suppress any errors, because email validation is nice to have
        return true;
    }
}

如您所期望的那样从数据库中检索值。但是,当我尝试从视图访问AppSettings时,值从web.config返回。例如,在my register.cshtml:

string maxHouseholdMembers = HostContext.AppSettings.Get("MaxMembersPerHousehold", "20");

值不是根据数据库中的值而变化,而是根据更新web.config而变化。所以,我需要做任何额外的配置,让我的视图使用我在global.asax.cs Configure()方法中创建的自定义AppSettings吗?

ServiceStack:视图中未使用的自定义应用设置

找到答案,而我实际上是张贴的问题。只需要在Configure()方法中添加额外的一行:

            // Create app settings, based on dictionary settings from central database
            Dictionary<string, string> configSettings = null;
            using (var db = container.Resolve<IDbConnectionFactory>().Open())
            {
                configSettings = db.Dictionary<string, string>(db.From<ConfigSetting>());
            }
            var dicSettings = new DictionarySettings(configSettings);
            AppSettings = dicSettings;

最后一行(AppSettings = dicSettings)完成了这个任务,并覆盖了标准的AppSettings。