Skip to content

Commit

Permalink
Merge pull request #30881 from dotnet/main
Browse files Browse the repository at this point in the history
Merge to Live
  • Loading branch information
Rick-Anderson authored Oct 31, 2023
2 parents 58c5294 + e249ab9 commit cf0336d
Show file tree
Hide file tree
Showing 76 changed files with 41,148 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"array": {
"entries": {
"0": "value00",
"1": "value10",
"2": "value20",
"4": "value40",
"5": "value50"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Position": {
"Title": "Dev My Config title",
"Name": "Dev My Config Smith"
},
"MyKey": "MyConfig.Development.json Value",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Position": {
"Title": "Production writer",
"Name": "Production Smith"
},
"MyKey": "Production appsettings.Production Value",
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Position": {
"Title": "My Config title",
"Name": "My Config Smith"
},
"MyKey": "MyConfig.json Value",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MyKey="MyIniConfig.ini Value from Dev"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MyKey="MyIniConfig.ini Value"

[Position]
Title="My INI Config title"
Name="My INI Config name"

[Logging:LogLevel]
Default=Information
Microsoft=Warning
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"section0": {
"key0": "value00",
"key1": "value01"
},
"section1": {
"key0": "value10",
"key1": "value11"
},
"section2": {
"subsection0": {
"key0": "value200",
"key1": "value201"
},
"subsection1": {
"key0": "value210",
"key1": "value211"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<MyKey>MyXMLFile Value</MyKey>
<Position>
<Title>Title from MyXMLFile</Title>
<Name>Name from MyXMLFile</Name>
</Position>
<Logging>
<LogLevel>
<Default>Information</Default>
<Microsoft>Warning</Microsoft>
</LogLevel>
</Logging>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<section name="section0">
<key name="key0">value 00</key>
<key name="key1">value 01</key>
</section>
<section name="section1">
<key name="key0">value 10</key>
<key name="key1">value 11</key>
</section>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ConfigSample.Options
{
#region snippet
public class ArrayExample
{
public string[]? Entries { get; set; }
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ConfigSample.Options
{
public class ColorOptions
{
public const string Color = "Color";

public string Foreground { get; set; } = String.Empty;
public string Background { get; set; } = String.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ConfigSample.Options;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static class MyConfigServiceCollectionExtensions
{
public static IServiceCollection AddConfig(
this IServiceCollection services, IConfiguration config)
{
services.Configure<PositionOptions>(
config.GetSection(PositionOptions.Position));
services.Configure<ColorOptions>(
config.GetSection(ColorOptions.Color));

return services;
}

public static IServiceCollection AddMyDependencyGroup(
this IServiceCollection services)
{
services.AddScoped<IMyDependency, MyDependency>();
services.AddScoped<IMyDependency2, MyDependency2>();

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ConfigSample.Options
{
#region snippet
public class PositionOptions
{
public const string Position = "Position";

public string Title { get; set; } = String.Empty;
public string Name { get; set; } = String.Empty;
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.DependencyInjection.ConfigSample.Options
{
public interface IMyDependency
{
void WriteMessage(string message);
}

public class MyDependency : IMyDependency
{
private readonly ILogger<MyDependency> _logger;

public MyDependency(ILogger<MyDependency> logger)
{
_logger = logger;
}

public void WriteMessage(string message)
{
_logger.LogInformation(
$"MyDependency.WriteMessage called. Message: {message}");
}
}
public interface IMyDependency2
{
void WriteMessage(string message);
}

public class MyDependency2 : IMyDependency2
{
private readonly ILogger<MyDependency2> _logger;

public MyDependency2(ILogger<MyDependency2> logger)
{
_logger = logger;
}

public void WriteMessage(string message)
{
_logger.LogInformation(
$"MyDependency2.WriteMessage called. Message: {message}");
}
}

public static class MyDependencyServiceCollectionExtensions
{
public static IServiceCollection AddMyDependencyGroup(this IServiceCollection services)
{
services.AddScoped<IMyDependency, MyDependency>();
return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page
@model ConfigSample.ArrayModel

Markup not displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using ConfigSample.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace ConfigSample
{
// <snippet>
public class ArrayModel : PageModel
{
private readonly IConfiguration Config;
public ArrayExample? _array { get; private set; }

public ArrayModel(IConfiguration config)
{
Config = config;
}

public ContentResult OnGet()
{
_array = Config.GetSection("array").Get<ArrayExample>();
if (_array == null)
{
throw new ArgumentNullException(nameof(_array));
}
string s = String.Empty;

for (int j = 0; j < _array.Entries.Length; j++)
{
s += $"Index: {j} Value: {_array.Entries[j]} \n";
}

return Content(s);
}
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model ErrorModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;

namespace ConfigSample.Pages;
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ConfigSample.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;

public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}

public void OnGet()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page
@model Index2Model

<p>This content is never used. The preceding directives are required in Razor Pages.</p>
Loading

0 comments on commit cf0336d

Please sign in to comment.