如何在ServiceStack中使用MultiAppSetting类替换自定义AppSetting类?

时间:2016-05-09 17:31:06

标签: c# .net servicestack amazon-dynamodb

我们决定在我们的应用程序中使用新的DynamoDbAppSettings类来利用DynamoDb。我们目前正在使用从AppSettings继承的自定义类(下面显示的类的一部分):

public class MyAppSettings : AppSettings
{
    public ApplicationEnvironment Environment
    {
        get { return Get("Environment", ApplicationEnvironment.Development); }
    }

    public List<string> AdministratorEmails
    {
        get { return Get("AdminEmailAddresses", new List<string>()); }
    }

    public string CompanyReadConnectionString
    {
        get
        {
            string settingsName = "CompanyReadConnectionString_{0}".Fmt(Environment);
            return Get(settingsName, string.Empty);
        }
    }
 }

我不太清楚如何转换到MultiAppSettings。例如,我们目前注册我们的AppSettings如下:

//Custom App settings
container.RegisterAutoWired<MyAppSettings>();
MyAppSettings appSettings = container.Resolve<MyAppSettings>();

然后我可以使用appSettings变量轻松访问我的应用程序设置,在自定义类中检查所有默认值等,以及没有&#34;魔术字符串&#34;分散在我的申请表中。举个例子,我可以通过以下方式轻松获得调试模式:

appSettings.DebugMode

对于DynamoDb,我添加了代码中显示的代码:

MultiAppSettings multiAppSettings = new MultiAppSettings(
      new DynamoDbAppSettings(
      new PocoDynamo(AwsConfig.CreateAmazonDynamoDb()), true),
            new MyAppSettings());

但我现在还不清楚如何使用它。我如何或者可以拥有一个类似上面显示的自定义类,可以使用MultiAppSettings?如果是这样,我如何注册并访问我的应用程序设置?在声明MultiAppSetting变量时,使用我现有的自定义类作为回退是否合适?有关使用DynamoDbAppSettings的更多指示将非常感激。

1 个答案:

答案 0 :(得分:1)

AWSApps AppHost通过在AppHost构造函数中分配MultiAppSettings,显示了使用DynamoDbAppSettingsbase.AppSettings的示例,例如:

public AppHost() : base("AWS Examples", typeof(AppHost).Assembly)
{
#if !DEBUG
    //Deployed RELEASE build uses Config settings in DynamoDb
    AppSettings = new MultiAppSettings(
        new DynamoDbAppSettings(newPocoDynamo(AwsConfig.CreateAmazonDynamoDb()),
            initSchema:true),
        new AppSettings());
#endif
}

现在将替换base.AppSettings提供程序,只需查看您的Web.config <appSettings/>,首先查看DynamoDB中的config,然后在特定条目不存在时回退到Web.config appSettings。

E.g。这在AppHost.Configure()populate API Keys for each registered AuthProvider中使用:

return new AuthFeature(() => new AuthUserSession(),
    new IAuthProvider[]
    {
        new CredentialsAuthProvider(),              //HTML User/Pass 
        new BasicAuthProvider(),                    //HTTP Basic Auth
        new DigestAuthProvider(AppSettings),        //HTTP Digest Auth
        new TwitterAuthProvider(AppSettings),       //Twitter
        new FacebookAuthProvider(AppSettings),      //Facebook
        new YahooOpenIdOAuthProvider(AppSettings),  //Yahoo OpenId
        new OpenIdOAuthProvider(AppSettings),       //Custom OpenId
        new GoogleOAuth2Provider(AppSettings),      //Google OAuth2
        new LinkedInOAuth2Provider(AppSettings),    //LinkedIn OAuth2
        new GithubAuthProvider(AppSettings),        //GitHub OAuth Provider
    })
{
    HtmlRedirect = "/awsauth/",
    IncludeRegistrationService = true,
};

IAppSettings会自动在Funq中注册,因此您的服务可以像访问任何其他依赖项一样访问它,例如:

public class MyServices : Service
{
    public IAppSettings AppSettings { get; set; }
}