ServiceStack:视图中未使用自定义应用程序设置
ServiceStack: Custom app settings not used in view
我与 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 返回的。例如。在我的 register.cshtml:
string maxHouseholdMembers = HostContext.AppSettings.Get("MaxMembersPerHousehold", "20");
该值不会根据数据库中的值而变化,但会根据更新 web.config 而变化。那么,我需要做任何额外的配置,让我的视图使用我在 global.asax.cs Configure() 方法中创建的自定义 AppSettings 吗?
我在实际发布问题时找到了答案。只需要在 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。
我与 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 返回的。例如。在我的 register.cshtml:
string maxHouseholdMembers = HostContext.AppSettings.Get("MaxMembersPerHousehold", "20");
该值不会根据数据库中的值而变化,但会根据更新 web.config 而变化。那么,我需要做任何额外的配置,让我的视图使用我在 global.asax.cs Configure() 方法中创建的自定义 AppSettings 吗?
我在实际发布问题时找到了答案。只需要在 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。