<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>cjAliaga Core</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/cjaliaga/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/cjaliaga/feed.xml</link>
    <description>services.addShowsTracker(new TrackSeriesTv())</description>
    <lastBuildDate>Tue, 16 Apr 2019 04:53:41 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Installing NuGet Packages in Azure Functions 2.X Runtime (PREVIEW)</title>
      <link>https://docs.microsoft.com/archive/blogs/cjaliaga/installing-nuget-packages-in-azure-functions-2-x-runtime-preview</link>
      <pubDate>Sun, 21 Jan 2018 20:33:08 GMT</pubDate>
      <dc:creator><![CDATA[Cj Aliaga]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cjaliaga/?p=65</guid>
      <description><![CDATA[Note: The 2.X version is currently in a preview phase so please bare in mind that this could be no...]]></description>
      <content:encoded><![CDATA[<em><strong>Note: </strong>The 2.X version is currently in a preview phase so please bare in mind that this could be no longer valid eventually.</em>

Hi folks,

In the current version of the Azure Functions Runtime (1.x) we are able to install NuGet packages for our C# and F# functions. The way to do it is adding the dependencies to a file named <em><strong>project.json</strong></em>. As you may be thinking this is because when the 1.x version was designed all was about <em><strong>project.json</strong></em> files. There's a really good documentation page where you can read about it: <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp">https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp </a>

During the development of the .NET Core tooling there was a design change, no longer supporting the <strong>project.json </strong>project files and instead moving the project system files to a XML ones, which is the MSBuild supported way. In the new version of the runtime we can find this design change as well. You can find the differences about both systems here: <a href="https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj">https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj</a>
<h3>Adding NuGet Packages to the Function</h3>
As we would do with the <b>1.X</b>, we need to create a file within our function folder to define the function's dependencies. Since we're using the new project system here the file name needs to be <strong>function.proj</strong>.

In the file, inside an ItemGroup section, we will be adding the PackageReference items with the package and the version. For example here I'm installing Microsoft.Azure.Devices 1.5 and Newtonsoft.Json 10:

[xml]
&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
&lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Microsoft.Azure.Devices&quot; Version=&quot;1.5.0&quot;/&gt;
    &lt;PackageReference Include=&quot;Newtonsoft.Json&quot; Version=&quot;10.0.3&quot;/&gt;
  &lt;/ItemGroup&gt;

&lt;/Project&gt;[/xml]

Then just save the file if you're editing it from the portal and the Azure Functions Runtime will install your dependencies and compile your function.

<em>May the <del><strong>PaaS</strong> </del> <strong>Serverless</strong> be with you!</em>

Carlos]]></content:encoded>
    </item>
    <item>
      <title>Working with Azure App Services Application Settings and Connection Strings in ASP.NET Core</title>
      <link>https://docs.microsoft.com/archive/blogs/cjaliaga/working-with-azure-app-services-application-settings-and-connection-strings-in-asp-net-core</link>
      <pubDate>Wed, 10 Aug 2016 15:41:09 GMT</pubDate>
      <dc:creator><![CDATA[Cj Aliaga]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/cjaliaga/?p=15</guid>
      <description><![CDATA[ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which...]]></description>
      <content:encoded><![CDATA[<span>ASP.NET Core’s configuration system has been re-architected from previous versions of ASP.NET, which relied on </span><code class="docutils literal"><span class="pre">System.Configuration</span></code><span> and XML configuration files like </span><code class="docutils literal"><span class="pre">web.config</span></code><span>. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources: from files using built-in support for JSON, XML, and INI formats, as well as from environment variables, command line arguments or an in-memory collection.</span>

Azure App Services gives us the possibility to set these settings directly in the Azure Portal and retrieve them from our Application Code:

<a href="https://msdnshared.blob.core.windows.net/media/2016/08/figure1.png"><img class="aligncenter size-large wp-image-25" alt="figure1" src="https://msdnshared.blob.core.windows.net/media/2016/08/figure1-1024x255.png" width="1024" height="255" /></a>
<h4>App settings</h4>
This section contains name/value pairs that you web app will load on start up.
<ul>
 	<li>For .NET apps like the previous versions of ASP.NET, these settings are injected into your .NET configuration <code>AppSettings</code> at runtime, overriding existing settings.</li>
 	<li>These settings also are injected as environment variables at runtime. For each app setting, two environment variables are created; one with the name specified by the app setting entry, and another with a prefix of APPSETTING_. Both contain the same value.</li>
</ul>
<h4><a href="https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/#connection-strings" id="_connection-strings" title=""></a>Connection strings</h4>
<span>For .NET apps like the previous versions of ASP.NET,</span> these connection strings are injected into your .NET configuration <code>connectionStrings</code> settings at runtime, overriding existing entries where the key equals the linked database name.

These settings will also be available as environment variables at runtime, prefixed with the connection type. The environment variable prefixes are as follows:
<ul>
 	<li>SQL Server: <code>SQLCONNSTR_</code></li>
 	<li>MySQL: <code>MYSQLCONNSTR_</code></li>
 	<li>SQL Database: <code>SQLAZURECONNSTR_</code></li>
 	<li>Custom: <code>CUSTOMCONNSTR_</code></li>
</ul>
For example, if a Custom connection string were named <code>Redis</code>, it would be accessed through the environment variable <code>CUSTOMCONNSTR_Redis</code>.

We can see these environment variables <span>through</span> KUDU: https://&lt;sitename&gt;.scm.azurewebsites.net/Env.cshtml
<h3>Getting these settings from ASP.NET Core</h3>
<strong>UPDATE:</strong> Jeff Sanders wrote an excellent blog explaining how to get these settings and work with them directly from your controllers in an elegant and simple way: <a href="https://blogs.msdn.microsoft.com/jpsanders/2017/05/16/azure-net-core-application-settings/" target="_blank">Azure .NET Core Application Settings</a>

The way to get these settings from our ASP.NET Core is accessing to the injected environment variables. Hence we have to load these environment variables into our Configuration in the Startup.cs file:
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span style="font-family: Consolas"><span lang="EN-US"><span style="color: #0000ff"><span style="font-size: 9.5pt">        public</span></span></span><span style="font-size: 9.5pt"><span lang="EN-US"><span style="color: #000000"> Startup(</span></span><span lang="EN-US"><span style="color: #2b91af">IHostingEnvironment</span></span></span><span lang="EN-US"><span style="font-size: 9.5pt;color: #000000"> env)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span lang="EN-US"><span style="font-family: Consolas"><span style="color: #000000"><span><span style="font-size: 9.5pt">        </span></span><span style="font-size: 9.5pt">{</span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span style="font-family: Consolas"><span lang="EN-US"><span><span style="color: #000000"><span style="font-size: 9.5pt">            </span></span></span></span><span style="font-size: 9.5pt"><span lang="EN-US"><span style="color: #0000ff">var</span></span><span lang="EN-US"><span style="color: #000000"> builder = </span></span><span lang="EN-US"><span style="color: #0000ff">new</span></span><span lang="EN-US"><span style="color: #000000"> </span></span><span lang="EN-US"><span style="color: #2b91af">ConfigurationBuilder</span></span></span><span lang="EN-US"><span style="font-size: 9.5pt;color: #000000">()</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span lang="EN-US"><span style="font-family: Consolas"><span style="color: #000000"><span><span style="font-size: 9.5pt">                </span></span><span style="font-size: 9.5pt">.SetBasePath(env.ContentRootPath)</span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span style="font-family: Consolas"><span lang="EN-US"><span style="color: #000000"><span><span style="font-size: 9.5pt">                </span></span><span style="font-size: 9.5pt">.AddJsonFile(</span></span></span><span style="font-size: 9.5pt"><span lang="EN-US"><span style="color: #a31515">"appsettings.json"</span></span><span lang="EN-US"><span style="color: #000000">, optional: </span></span><span lang="EN-US"><span style="color: #0000ff">false</span></span><span lang="EN-US"><span style="color: #000000">, reloadOnChange: </span></span><span lang="EN-US"><span style="color: #0000ff">true</span></span></span><span lang="EN-US"><span style="font-size: 9.5pt;color: #000000">)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span style="font-family: Consolas"><span lang="EN-US"><span style="color: #000000"><span><span style="font-size: 9.5pt">                </span></span><span style="font-size: 9.5pt">.AddJsonFile(</span></span></span><span style="font-size: 9.5pt"><span lang="EN-US"><span style="color: #a31515">$"appsettings.</span></span><span lang="EN-US"><span style="color: #000000">{env.EnvironmentName}</span></span><span lang="EN-US"><span style="color: #a31515">.json"</span></span><span lang="EN-US"><span style="color: #000000">, optional: </span></span><span lang="EN-US"><span style="color: #0000ff">true</span></span></span><span lang="EN-US"><span style="font-size: 9.5pt;color: #000000">)</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span style="font-family: Consolas"><span style="color: #000000"><span lang="EN-US"><span><span style="font-size: 9.5pt">                </span></span></span><span><span style="font-size: 9.5pt">.AddEnvironmentVariables();</span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span><span style="font-family: Consolas"><span style="color: #000000"><span><span style="font-size: 9.5pt">            </span></span><span style="font-size: 9.5pt">Configuration = builder.Build();</span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span><span style="font-family: Consolas"><span style="color: #000000"><span><span style="font-size: 9.5pt">        </span></span><span style="font-size: 9.5pt">}</span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;line-height: normal"><span><span style="color: #000000;font-family: Consolas"></span></span></p>
&nbsp;

<span>The order in which configuration sources are specified is important, as this establishes the precedence with which settings will be applied if they exist in multiple locations. In the example below, if the same setting exists in both </span><em>appsettings.json</em><span> and in an environment variable, the setting from the environment variable will be the one that is used. The last configuration source specified “wins” if a setting exists in more than one location. The ASP.NET team recommends specifying environment variables last, so that the environment where your app is running can override anything set in deployed configuration files.</span>
<h4></h4>
<h4>Grouped in sections</h4>
In ASP.NET Core we can group our related settings in sections, one group for the connection string, another one for Application Insights configuration, another for  AzureAD...

An example of <code>appsettings.json</code> file would be:

<a href="https://msdnshared.blob.core.windows.net/media/2016/08/figure2.png"><img class="alignnone wp-image-35 size-full" alt="figure2" src="https://msdnshared.blob.core.windows.net/media/2016/08/figure2.png" width="481" height="419" /></a>

If we want, for example, to retrieve the AzureAD's ClientId we could get it through our Configuration: <code>Configuration["Authentication:AzureAd:ClientId"]</code> or the Application Insights' InstrumentationKey: <code>Configuration["ApplicationInsights:InstrumentationKey"]</code>
<h4>Overriding nested keys in Azure App Services (through environment variables)</h4>
To override nested keys in the App Settings section we can define a variable using the full path <code>Authentication:AzureAd:ClientId</code> as name or using double underscore <code>Authentication__AzureAd__ClientId</code>
<h4>Connection Strings</h4>
To get the connection string named "Redis" defined in the <code>appsettings.json</code> file we could just use the same approach that we used earlier: <code>Configuration["ConnectionStrings:Redis"].</code>

However there's a better way to access to the connection strings, an extension method <code>Configuration.GetConnectionString("Redis")</code> which when is loading the keys from the <code>appsettings.json</code> file is just acting as a Shorthand for the <code>ConnectionStrings</code> section but when is working with the environment variables that Azure App Services defines for the connection strings is able to locate the "Redis" connection string regardless of the type the connection string has in the portal: SQL Server, MySQL, SQL Database or Custom.

So we can use <code>Configuration.GetConnectionString("Redis")</code> to get a development connection string from our <code>appsettings.json</code> file and override it setting a different one in the Connection String panel of our Web App when the application is deployed and running in Azure.

&nbsp;

<em>May the <strong>PaaS</strong> be with you!</em>

Carlos.]]></content:encoded>
    </item>
  </channel>
</rss>