1
我與ServiceStack相處得非常好,但遇到了一個我目前無法工作的問題。在我的Global.asax.cs配置()方法,我宣佈一個基於數據庫的AppSettings如下:ServiceStack:未在視圖中使用的自定義應用設置
// 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?
是的,你只需要設置AppHost AppSettings,你不需要在IOC中註冊,因爲ServiceStack會註冊它,如果它不存在 – mythz