{"id":36910,"date":"2017-11-17T09:41:35","date_gmt":"2017-11-17T17:41:35","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/?p=10995"},"modified":"2017-11-17T09:41:35","modified_gmt":"2017-11-17T17:41:35","slug":"announcing-net-4-7-1-tools-for-the-cloud","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/announcing-net-4-7-1-tools-for-the-cloud\/","title":{"rendered":"Announcing .NET 4.7.1 Tools for the Cloud"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\" alt=\"Packages and Containers\" class=\"alignright size-mediumlarge wp-image-10996\" width=\"350\" height=\"350\" \/>Today we are releasing a set of providers for ASP.NET 4.7.1 that make it easier than ever to deploy your applications to cloud services and take advantage of cloud-scale features.\u00a0 This release includes a new CosmosDb provider for session state and a collection of configuration builders.<\/p>\n<h2>A Package-First Approach<\/h2>\n<p>With previous versions of the .NET Framework, new features were provided \u201cin the box\u201d and shipped with Windows and new versions of the entire framework.\u00a0 This means that you can be assured that your providers and capabilities were available on every current version of Windows.\u00a0 It also means that you had to wait until a new version of Windows to get new .NET Framework features.\u00a0 We have adopted a strategy starting with .NET Framework 4.7 to deliver more abstract features with the framework and deploy providers through the NuGet package manager service.\u00a0 There are no concrete ConfigurationBuilder classes in the .NET Framework 4.7.1, and we are now making available several for your use from the NuGet.org repository.\u00a0 In this way, we can update and deploy new ConfigurationBuilders without requiring a fresh install of Windows or the .NET Framework.<\/p>\n<h2>ConfigurationBuilders Simplify Application Management<\/h2>\n<p>In .NET Framework 4.7.1 we introduced the concept of ConfigurationBuilders.\u00a0 ConfigurationBuilders are objects that allow you to inject application configuration into your .NET Framework 4.7.1 application and continue to use the familiar ConfigurationManager interface to read those values.\u00a0 Sure, you could always write your configuration files to read other config files from disk, but what if you wanted to apply configuration from environment variables?\u00a0 What if you wanted to read configuration from a service, like Azure Key Vault?\u00a0 To work with those configuration sources, you would need to rewrite some non-trivial amount of your application to consume these services.<\/p>\n<p>With ConfigurationBuilders, no code changes are necessary in your application. \u00a0You simply add references from your web.config or app.config file to the ConfigurationBuilders you want to use and your application will start consuming those sources without updating your configuration files on disk.\u00a0 One form of ConfigurationBuilder is the KeyValueConfigBuilder that matches a key to a value from an external source and adds that pair to your configuration.\u00a0 All of the ConfigurationBuilders we are releasing today support this key-value approach to configuration.\u00a0 Lets take a look at using one of these new ConfigurationBuilders, the <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.Environment\/\">EnvironmentConfigBuilder<\/a>.<\/p>\n<p>When you install any of our new ConfigurationBuilders into your application, we automatically allocate the appropriate new configSections in your app.config or web.config file as shown below:<\/p>\n<p>The new \u201cbuilders\u201d section contains information about the ConfigurationBuilders you wish to use in your application.\u00a0 You can declare any number of ConfigurationBuilders, and apply the settings they retrieve to any section of your configuration.\u00a0 Let\u2019s look at applying our environment variables to the appSettings of this configuration.\u00a0 You specify which ConfigurationBuilders to apply to a section by adding the configBuilders attribute to that section, and indicate the name of the defined ConfigurationBuilder to apply, in this case \u201cEnvironment\u201d<\/p>\n<pre>&lt;appSettings configBuilders=\"Environment\"&gt;\n  &lt;add key=\"COMPUTERNAME\" value=\"VisualStudio\" \/&gt;\n&lt;\/appSettings&gt;<\/pre>\n<p>The COMPUTERNAME is a common environment variable set by the Windows operating system that we can use to replace the VisualStudio setting defined here.\u00a0 With the below ASPX page in our project, we can run our application and see the following results.<\/p>\n<p><figure id=\"attachment_11005\" aria-labelledby=\"figcaption_attachment_11005\" class=\"wp-caption aligncenter\" ><img decoding=\"async\" src=\"\" alt=\"AppSettings Reported in the Browser\" class=\"size-mediumlarge wp-image-11005\" width=\"500\" height=\"311\" \/><figcaption id=\"figcaption_attachment_11005\" class=\"wp-caption-text\">AppSettings Reported in the Browser<\/figcaption><\/figure> The COMPUTERNAME setting is overwritten by the environment variable.\u00a0 That\u2019s a nice start, but what if I want to read ALL the environment variables and add them as application settings?\u00a0 You can specify Greedy Mode for the ConfigurationBuilder and it will read all environment variables and add them to your appSettings:<\/p>\n<pre>&lt;add name=\"Environment\" mode=\"Greedy\"\n  type=\"Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral\" \/&gt;<\/pre>\n<p>There are several Modes that you can apply to each of the ConfigurationBuilders we are releasing today:<\/p>\n<ul>\n<li><strong>Greedy<\/strong> \u2013 Read all settings and add them to the section the ConfigurationBuilder is applied to<\/li>\n<li><strong>Strict<\/strong> \u2013 (default) Update only those settings where the key matches the configuration source\u2019s key<\/li>\n<li><strong>Expand<\/strong> \u2013 Operate on the raw XML of the configuration section and do a string replace where the configuration source\u2019s key is found. <\/li>\n<\/ul>\n<p>The Greedy and Strict options only apply when operating on AppSettings or ConnectionStrings sections.\u00a0 Expand can perform its string replacement on any section of your config file.<\/p>\n<p>You can also specify prefixes for your settings to be handled by adding the prefix attribute.\u00a0 This allows you to only read settings that start with a known prefix.\u00a0 Perhaps you only want to add environment variables that start with \u201cAPPSETTING_\u201d, you can update your config file like this:<\/p>\n<pre>&lt;add name=\"Environment\"\n     mode=\"Greedy\" prefix=\"APPSETTING_\" \n     type=\"Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral\" \/&gt;<\/pre>\n<p>Finally, even though using the APPSETTING_ prefix is a nice catch to only read those settings, you may not want your configuration to actually be called \u201cAPPSETTING_Setting\u201d in code.\u00a0 You can use the stripPrefix attribute (default value is false) to omit the prefix when the value is added to your configuration: <figure id=\"attachment_11015\" aria-labelledby=\"figcaption_attachment_11015\" class=\"wp-caption aligncenter\" ><img decoding=\"async\" src=\"\" alt=\"Greedy AppSettings with Prefixes Stripped\" class=\"size-mediumlarge wp-image-11015\" width=\"352\" height=\"350\" \/><figcaption id=\"figcaption_attachment_11015\" class=\"wp-caption-text\">Greedy AppSettings with Prefixes Stripped<\/figcaption><\/figure> Notice that the COMPUTERNAME was not replaced in this mode.\u00a0 You can add a second EnvironmentConfigBuilder to read and apply settings by adding another add statement to the configBuilders section and adding an entry to the configBuilders attribute on the appSettings:<\/p>\n<p>Try using the EnvironmentConfigBuilder from inside a Docker container to inject configuration specific to your running instances of your application.\u00a0 We\u2019ve found that this significantly improves the ability to deploy existing applications in containers without having to rewrite your code to read from alternate configuration sources.<\/p>\n<h2>Secure Configuration with Azure Key Vault<\/h2>\n<p>We are happy to include a <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.Azure\/\">ConfigurationBuilder for Azure Key Vault<\/a> in this initial collection of providers.\u00a0 This ConfigurationBuilder allows you to secure your application using the <a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/key-vault\/\">Azure Key Vault service<\/a>, without any required login information to access the vault.\u00a0 Add this ConfigurationBuilder to your config file and build an add statement like the following:<\/p>\n<pre>&lt;add name=\"AzureKeyVault\"\n     mode=\"Strict\"\n     vaultName=\"MyVaultName\"\n     type=\"Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure\" \/&gt;<\/pre>\n<p>If your application is running on an Azure service that has <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/msi-overview\"><\/a>, this is all you need to read configuration from the vault and add it to your application.\u00a0 Conversely, if you are not running on a service with MSI, you can still use the vault by adding the following attributes: \n*   <strong>clientId<\/strong> \u2013 the Azure Active Directory application key that has access to your key vault\n*   <strong>clientSecret<\/strong> \u2013 the Azure Active Directory application secret that corresponds to the clientId<\/p>\n<p>The same mode, prefix, and stripPrefix features described previously are available for use with the AzureKeyVaultConfigBuilder.\u00a0 You can now configure your application to grab that secret database connection string from the keyvault \u201cconn_mydb\u201d setting with a config file that looks like this:<\/p>\n<p>You can use other vaults by using the uri attribute instead of the vaultName attribute, and providing the URI of the vault you wish to connect to.\u00a0 More information about <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/key-vault\/key-vault-get-started\">getting started configuring key vault<\/a> is available online.<\/p>\n<h2>Other Configuration Builders Available<\/h2>\n<p>Today we are introducing five configuration builders as a preview for you to use and extend:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.Environment\/\">EnvironmentConfigBuilder<\/a> \u2013 Read from environment variables<\/li>\n<li><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.Azure\/\">AzureKeyVaultConfigBuilder<\/a> \u2013 Read from Azure Key Vault<\/li>\n<li><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.UserSecrets\/\">UserSecretsConfigBuilder<\/a> \u2013 Read from a usersecrets file on disk, similar to the ASP.NET Core functionality<\/li>\n<li><a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Configuration.ConfigurationBuilders.Json\/\">SimpleJsonConfigBuilder<\/a> \u2013 Read from a JSON file This new collection of ConfigurationBuilders should help make it easier than ever to secure your applications with Azure Key Vault, or orchestrate your applications when you add them to a container by no longer embedding configuration or writing extra code to handle deployment settings. We plan to fully release the source code and make these providers open source prior to removing the preview tag from them. <\/li>\n<\/ul>\n<h2>Store SessionState in CosmosDb<\/h2>\n<p>Today we are also releasing a session state provider for Azure Cosmos Db.\u00a0 The globally available CosmosDb service means that you can geographically load-balance your ASP.NET application and your users will always maintain the same session state no matter the server they are connected to.\u00a0 This async provider is available as a <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.AspNet.SessionState.CosmosDBSessionStateProviderAsync\/\">NuGet package<\/a> and can be added to your project by installing that package and updating the session state provider in your web.config as follows:<\/p>\n<pre>&lt;connectionStrings \n  &lt;add name=\"myCosmosConnString\" \n       connectionString=\"- YOUR CONNECTION STRING -\"\/&gt;\n&lt;\/connectionStrings&gt;\n&lt;sessionState mode=\"Custom\" customProvider=\"cosmos\"&gt;<\/pre>\n<pre>&lt;providers&gt;\n    &lt;add name=\"cosmos\"\n         type=\"Microsoft.AspNet.SessionState.CosmosDBSessionStateProviderAsync, Microsoft.AspNet.SessionState.CosmosDBSessionStateProviderAsync\"\n         connectionStringName=\"myCosmosConnString\"\/&gt;\n  &lt;\/providers&gt;\n&lt;\/sessionState&gt;\n<\/pre>\n<h2>Summary<\/h2>\n<p>We\u2019re continuing to innovate and update .NET Framework and ASP.NET.\u00a0 With these new providers, they should make it easier to deploy your applications to Azure or make use of containers without having to rewrite your application.\u00a0 Update your applications to .NET 4.7.1 and start using these new providers to make your configuration more secure, and to start using CosmosDb for your session state.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we are releasing a set of providers for ASP.NET 4.7.1 that make it easier than ever to deploy your applications to cloud services and take advantage of cloud-scale features.\u00a0 This release includes a new CosmosDb provider for session state and a collection of configuration builders. A Package-First Approach With previous versions of the .NET [&hellip;]<\/p>\n","protected":false},"author":405,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-36910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>Today we are releasing a set of providers for ASP.NET 4.7.1 that make it easier than ever to deploy your applications to cloud services and take advantage of cloud-scale features.\u00a0 This release includes a new CosmosDb provider for session state and a collection of configuration builders. A Package-First Approach With previous versions of the .NET [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/36910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/405"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=36910"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/36910\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=36910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=36910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=36910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}