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