Last active
February 1, 2021 14:34
-
-
Save aidapsibr/b2003d03e29cb3b306ceca6884ad93e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Identity; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Configuration.AzureAppConfiguration; | |
using Microsoft.Extensions.Hosting; | |
using System; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace Example | |
{ | |
/// <summary> | |
/// Main Program | |
/// </summary> | |
public class Program | |
{ | |
private static string _appConfigEndpointPattern = "https://{0}-{1}-{2}.azconfig.io"; | |
/// <summary> | |
/// Entry point | |
/// </summary> | |
/// <param name="args"></param> | |
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args) | |
.Build() | |
.Run(); | |
} | |
/// <summary> | |
/// Create IWebHostBuilder | |
/// </summary> | |
/// <param name="args"></param> | |
/// <returns></returns> | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.ConfigureAppConfiguration((hostingContext, config) => | |
{ | |
var settings = config.Build(); | |
var environmentName = settings["EnvironmentNameOverride"] ?? hostingContext.HostingEnvironment.EnvironmentName; | |
var regionName = settings["Datacenter"]; | |
var productName = settings["AppConfig:AppConfigName"]; | |
var credentials = new DefaultAzureCredential( | |
new DefaultAzureCredentialOptions | |
{ | |
/* broken on linux, currently unreliable to be honest */ | |
ExcludeSharedTokenCacheCredential = true, | |
// some devs have a Service Principal in ENV vars, don't want that, we want the developer to auth. | |
ExcludeEnvironmentCredential = true, | |
/* By using azure CLI login, in combination with a volume mount of $HOME/.azure we can pass | |
* our local machine tokens into the docker container that gets run to debug. | |
*/ | |
ExcludeAzureCliCredential = false, | |
// these work for local development when NOT in docker, so leaving them enabled, but they don't work in docker. | |
ExcludeInteractiveBrowserCredential = false, | |
ExcludeVisualStudioCredential = false, | |
ExcludeVisualStudioCodeCredential = false, | |
// managed identity will work fine if available (system OR user assigned) | |
ExcludeManagedIdentityCredential = false, | |
// set this to use User Assigned Managed Identity | |
//ManagedIdentityClientId = "" | |
}); | |
var appConfigUriString = string.Format(_appConfigEndpointPattern, environmentName, regionName, productName); | |
config.AddAzureAppConfiguration(options => | |
{ | |
options.Connect(new Uri(appConfigUriString, UriKind.Absolute), credentials) | |
.ConfigureKeyVault(kv => | |
{ | |
kv.SetCredential(credentials); | |
}) | |
// Load all configuration values with no label | |
.Select(KeyFilter.Any, LabelFilter.Null) | |
// Add/Override with any configuration values specific to current environment | |
.Select(KeyFilter.Any, environmentName.ToLower()); | |
}); | |
var thing = hostingContext; | |
}); | |
webBuilder.UseStartup<Startup>(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. | |
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base | |
#AZ CLI installed at the base layer so that Developer login works in debug mode | |
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash | |
WORKDIR /app | |
EXPOSE 80 | |
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build | |
WORKDIR /src | |
COPY ["Host/Host.csproj", "Host/"] | |
RUN dotnet restore "Host/Host.csproj" | |
COPY . . | |
WORKDIR "/src/Host" | |
RUN dotnet build --no-restore "Host.csproj" -c Release -o /app/build | |
FROM build AS publish | |
RUN dotnet publish "Host.csproj" -c Release -o /app/publish | |
FROM base AS final | |
WORKDIR /app | |
COPY --from=publish /app/publish . | |
ENTRYPOINT ["dotnet", "Host.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | |
<AssemblyName>Host</AssemblyName> | |
<RootNamespace>Host</RootNamespace> | |
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | |
<!-- | |
By using azure CLI login, in combination with a volume mount of $HOME/.azure (/root) we can pass | |
our local machine tokens into the docker container that gets run to debug. | |
--> | |
<DockerfileRunArguments>--mount type=bind,source="$(USERPROFILE)\.azure",target=/root/.azure</DockerfileRunArguments> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.1.1" /> | |
<PackageReference Include="Azure.Identity" Version="1.3.0" /> | |
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.11.0" /> | |
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.2" /> | |
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.2.1" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0" /> | |
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.2" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" /> | |
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.1.0" /> | |
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.10" /> | |
<PackageReference Include="NSwag.AspNetCore" Version="13.1.3" /> | |
<PackageReference Include="Polly" Version="7.2.1" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment