Skip to content

Commit

Permalink
configuration sample updated to aspnetcore3
Browse files Browse the repository at this point in the history
  • Loading branch information
OdeToCode committed Nov 8, 2019
1 parent 2f17838 commit e98b028
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 83 deletions.
26 changes: 6 additions & 20 deletions All/src/Configuration/Configuration.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>Configuration</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Configuration</PackageId>
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>77a3781f-cfb6-4e96-bd55-9b07e16e0e83</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<None Update="wwwroot\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />
</ItemGroup>

</Project>
</Project>
11 changes: 9 additions & 2 deletions All/src/Configuration/GreetingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

Expand All @@ -18,8 +19,14 @@ public async Task Invoke(HttpContext context,
ISecretNumber secretNumber,
IOptions<MessageConfiguration> configuration)
{
await context.Response.WriteAsync(secretNumber.ComputeNumber().ToString());
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (configuration.Value == null) throw new ArgumentException(nameof(configuration.Value));
if (configuration.Value.Messages == null) throw new ArgumentException(nameof(configuration.Value.Messages));
if (configuration.Value.Messages.Salutation == null) throw new ArgumentException(nameof(configuration.Value.Messages.Salutation));

await context.Response.WriteAsync(configuration.Value.Messages.Salutation);
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(secretNumber.ComputeNumber().ToString());
}
}
}
1 change: 0 additions & 1 deletion All/src/Configuration/Messsages.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Configuration
{

public class MessageConfiguration
{
public Messages Messages { get; set; }
Expand Down
15 changes: 9 additions & 6 deletions All/src/Configuration/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace Configuration
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
var host = WebHost
.CreateDefaultBuilder()
.UseUrls("http://localhost:5000")
.UseStartup<Startup>()
.ConfigureAppConfiguration(b => {
b.AddUserSecrets<Startup>();
b.AddJsonFile("config.json", optional: false);
})
.Build();

host.Run();
}
}
Expand Down
25 changes: 0 additions & 25 deletions All/src/Configuration/Properties/launchSettings.json

This file was deleted.

25 changes: 7 additions & 18 deletions All/src/Configuration/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,24 @@

namespace Configuration
{




public class Startup
{
public Startup(IHostingEnvironment environment)
{
var builder = new ConfigurationBuilder();
builder.SetBasePath(environment.WebRootPath)
.AddJsonFile("config.json")
.AddUserSecrets<Startup>()
.AddEnvironmentVariables();
public IConfiguration Configuration { get; }

Configuration = builder.Build();
public Startup(IConfiguration configuation)
{
Configuration = configuation;
}

public IConfiguration Configuration { get; set; }

public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
//services.AddOptions<MessageConfiguration>(Configuration);
services.AddLogging();
services.AddScoped<ISecretNumber>(provider => new SecretNumber(Configuration));
services.Configure<MessageConfiguration>(Configuration);
services.AddSingleton<ISecretNumber, SecretNumber>();
}

public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseMiddleware<GreetingMiddleware>();
}
}
Expand Down
2 changes: 0 additions & 2 deletions All/src/Configuration/hosting.ini

This file was deleted.

9 changes: 0 additions & 9 deletions All/src/Configuration/web.config

This file was deleted.

0 comments on commit e98b028

Please sign in to comment.