| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Class ServiceCollectionExtensions | Apq.Cfg API </title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta name="title" content="Class ServiceCollectionExtensions | Apq.Cfg API ">
-
- <meta name="description" content="IServiceCollection 扩展方法">
- <link rel="icon" href="../favicon.svg">
- <link rel="stylesheet" href="../public/docfx.min.css">
- <link rel="stylesheet" href="../public/main.css">
- <meta name="docfx:navrel" content="../toc.html">
- <meta name="docfx:tocrel" content="toc.html">
-
- <meta name="docfx:rel" content="../">
-
-
-
- <meta name="loc:inThisArticle" content="In this article">
- <meta name="loc:searchResultsCount" content="{count} results for "{query}"">
- <meta name="loc:searchNoResults" content="No results for "{query}"">
- <meta name="loc:tocFilter" content="Filter by title">
- <meta name="loc:nextArticle" content="Next">
- <meta name="loc:prevArticle" content="Previous">
- <meta name="loc:themeLight" content="Light">
- <meta name="loc:themeDark" content="Dark">
- <meta name="loc:themeAuto" content="Auto">
- <meta name="loc:changeTheme" content="Change theme">
- <meta name="loc:copy" content="Copy">
- <meta name="loc:downloadPdf" content="Download PDF">
- <script type="module" src="./../public/docfx.min.js"></script>
- <script>
- const theme = localStorage.getItem('theme') || 'auto'
- document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
- </script>
- </head>
- <body class="tex2jax_ignore" data-layout="" data-yaml-mime="ManagedReference">
- <header class="bg-body border-bottom">
- <nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
- <div class="container-xxl flex-nowrap">
- <a class="navbar-brand" href="../index.html">
- <img id="logo" class="svg" src="../images/logo-small.svg" alt="Apq.Cfg">
- Apq.Cfg
- </a>
- <button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
- <i class="bi bi-three-dots"></i>
- </button>
- <div class="collapse navbar-collapse" id="navpanel">
- <div id="navbar">
- <form class="search" role="search" id="search">
- <i class="bi bi-search"></i>
- <input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
- </form>
- </div>
- </div>
- </div>
- </nav>
- </header>
- <main class="container-xxl">
- <div class="toc-offcanvas">
- <div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
- <div class="offcanvas-header">
- <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
- <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
- </div>
- <div class="offcanvas-body">
- <nav class="toc" id="toc"></nav>
- </div>
- </div>
- </div>
- <div class="content">
- <div class="actionbar">
- <button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
- <i class="bi bi-list"></i>
- </button>
- <nav id="breadcrumb"></nav>
- </div>
- <article data-uid="Apq.Cfg.ServiceCollectionExtensions">
- <h1 id="Apq_Cfg_ServiceCollectionExtensions" data-uid="Apq.Cfg.ServiceCollectionExtensions" class="text-break">
- Class ServiceCollectionExtensions
- </h1>
- <div class="facts text-secondary">
- <dl><dt>Namespace</dt><dd><a class="xref" href="Apq.html">Apq</a>.<a class="xref" href="Apq.Cfg.html">Cfg</a></dd></dl>
- <dl><dt>Assembly</dt><dd>Apq.Cfg.dll</dd></dl>
- </div>
- <div class="markdown summary"><p>IServiceCollection 扩展方法</p>
- </div>
- <div class="markdown conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static class ServiceCollectionExtensions</code></pre>
- </div>
- <dl class="typelist inheritance">
- <dt>Inheritance</dt>
- <dd>
- <div><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object">object</a></div>
- <div><span class="xref">ServiceCollectionExtensions</span></div>
- </dd>
- </dl>
- <dl class="typelist inheritedMembers">
- <dt>Inherited Members</dt>
- <dd>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object)</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object, object)</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone">object.MemberwiseClone()</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object, object)</a>
- </div>
- <div>
- <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>
- </div>
- </dd></dl>
- <h2 class="section" id="methods">Methods
- </h2>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder_System_IServiceProvider__" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Apq.Cfg.CfgBuilder,System.IServiceProvider})">
- AddApqCfg(IServiceCollection, Action<CfgBuilder, IServiceProvider>)
-
- </h3>
- <div class="markdown level1 summary"><p>添加 Apq.Cfg 配置服务(支持访问 IServiceProvider)</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddApqCfg(this IServiceCollection services, Action<CfgBuilder, IServiceProvider> configure)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>configure</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-2">Action</a><<a class="xref" href="Apq.Cfg.CfgBuilder.html">CfgBuilder</a>, <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.iserviceprovider">IServiceProvider</a>></dt>
- <dd><p>配置构建器委托,接收 CfgBuilder 和 IServiceProvider</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder_System_IServiceProvider___examples">Examples</h4>
- <pre><code class="lang-csharp">// 使用 Data Protection 加密
- services.AddDataProtection();
- services.AddApqCfg((builder, sp) => builder
- .AddJson("appsettings.json", level: 0, writeable: false)
- .AddDataProtectionEncryption(sp.GetRequiredService<IDataProtectionProvider>())
- .AddSensitiveMasking());</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder_System_IServiceProvider___remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>此重载允许在配置构建过程中访问已注册的服务,
- 适用于需要依赖其他服务(如 IDataProtectionProvider)的场景。</p>
- </div>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Apq.Cfg.CfgBuilder})">
- AddApqCfg(IServiceCollection, Action<CfgBuilder>)
-
- </h3>
- <div class="markdown level1 summary"><p>添加 Apq.Cfg 配置服务</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddApqCfg(this IServiceCollection services, Action<CfgBuilder> configure)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>configure</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-1">Action</a><<a class="xref" href="Apq.Cfg.CfgBuilder.html">CfgBuilder</a>></dt>
- <dd><p>配置构建器委托,用于配置各种配置源</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder___examples">Examples</h4>
- <pre><code class="lang-csharp">services.AddApqCfg(cfg => cfg
- .AddJson("config.json", level: 0)
- .AddJson($"config.{environment}.json", level: 1)
- .AddEnvironmentVariables(prefix: "APP_", level: 2));</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder___remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>此方法会同时注册 ICfgRoot 和 IConfigurationRoot 服务,
- 使您可以在应用程序中同时使用 Apq.Cfg 和 Microsoft.Extensions.Configuration 的 API。</p>
- </div>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Func_System_IServiceProvider_Apq_Cfg_ICfgRoot__" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Func{System.IServiceProvider,Apq.Cfg.ICfgRoot})">
- AddApqCfg(IServiceCollection, Func<IServiceProvider, ICfgRoot>)
-
- </h3>
- <div class="markdown level1 summary"><p>添加 Apq.Cfg 配置服务(使用工厂方法)</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddApqCfg(this IServiceCollection services, Func<IServiceProvider, ICfgRoot> factory)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>factory</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.func-2">Func</a><<a class="xref" href="https://learn.microsoft.com/dotnet/api/system.iserviceprovider">IServiceProvider</a>, <a class="xref" href="Apq.Cfg.ICfgRoot.html">ICfgRoot</a>></dt>
- <dd><p>配置根工厂方法,接收服务提供者并返回配置根实例</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Func_System_IServiceProvider_Apq_Cfg_ICfgRoot___examples">Examples</h4>
- <pre><code class="lang-csharp">services.AddApqCfg(sp => {
- var env = sp.GetRequiredService<IWebHostEnvironment>();
- return new CfgBuilder()
- .AddJson("config.json", level: 0)
- .AddJson($"config.{env.EnvironmentName}.json", level: 1)
- .AddEnvironmentVariables(prefix: "APP_", level: 2)
- .Build();
- });</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Func_System_IServiceProvider_Apq_Cfg_ICfgRoot___remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>使用工厂方法可以访问其他已注册的服务,实现更复杂的配置逻辑。
- 工厂方法只会在首次请求配置时调用一次。</p>
- </div>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfgWithValidation_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfgWithValidation*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfgWithValidation_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__System_Boolean_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfgWithValidation(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Apq.Cfg.CfgBuilder},System.Boolean)">
- AddApqCfgWithValidation(IServiceCollection, Action<CfgBuilder>, bool)
-
- </h3>
- <div class="markdown level1 summary"><p>添加 Apq.Cfg 配置服务并启用验证</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddApqCfgWithValidation(this IServiceCollection services, Action<CfgBuilder> configure, bool validateOnStartup = true)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>configure</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-1">Action</a><<a class="xref" href="Apq.Cfg.CfgBuilder.html">CfgBuilder</a>></dt>
- <dd><p>配置构建器委托</p>
- </dd>
- <dt><code>validateOnStartup</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.boolean">bool</a></dt>
- <dd><p>是否在启动时验证配置,默认为 true</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfgWithValidation_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__System_Boolean__examples">Examples</h4>
- <pre><code class="lang-csharp">services.AddApqCfgWithValidation(cfg => cfg
- .AddJson("config.json", level: 0)
- .AddValidation(v => v
- .Required("Database:ConnectionString")
- .Range("Database:Port", 1, 65535)));</code></pre>
- <h4 class="section">Exceptions</h4>
- <dl class="parameters">
- <dt><a class="xref" href="Apq.Cfg.Validation.ConfigValidationException.html">ConfigValidationException</a></dt>
- <dd><p>当 validateOnStartup 为 true 且验证失败时抛出</p>
- </dd>
- </dl>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__System_String_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddApqCfg``1(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Apq.Cfg.CfgBuilder},System.String)">
- AddApqCfg<TOptions>(IServiceCollection, Action<CfgBuilder>, string)
-
- </h3>
- <div class="markdown level1 summary"><p>添加 Apq.Cfg 配置服务并绑定强类型配置</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddApqCfg<TOptions>(this IServiceCollection services, Action<CfgBuilder> configure, string sectionKey) where TOptions : class, new()</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>configure</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-1">Action</a><<a class="xref" href="Apq.Cfg.CfgBuilder.html">CfgBuilder</a>></dt>
- <dd><p>配置构建器委托,用于配置各种配置源</p>
- </dd>
- <dt><code>sectionKey</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
- <dd><p>配置节键名,用于定位 TOptions 的配置数据</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section">Type Parameters</h4>
- <dl class="parameters">
- <dt><code>TOptions</code></dt>
- <dd><p>配置选项类型</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__System_String__examples">Examples</h4>
- <pre><code class="lang-csharp">// 定义配置选项类
- public class DatabaseOptions
- {
- public string? ConnectionString { get; set; }
- public int Timeout { get; set; } = 30;
- }
- // 注册配置服务并绑定强类型配置
- services.AddApqCfg<DatabaseOptions>(cfg => cfg
- .AddJson("config.json", level: 0)
- .AddEnvironmentVariables(prefix: "APP_", level: 2),
- "Database");
- // 使用配置
- var dbOptions = serviceProvider.GetRequiredService<IOptions<DatabaseOptions>>().Value;</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_CfgBuilder__System_String__remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>此方法会注册 ICfgRoot、IConfigurationRoot 和 IOptions<TOptions> 服务。
- 配置选项类必须有无参构造函数。</p>
- </div>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddConfigValidator_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddConfigValidator*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddConfigValidator_Microsoft_Extensions_DependencyInjection_IServiceCollection_Apq_Cfg_Validation_IConfigValidator_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddConfigValidator(Microsoft.Extensions.DependencyInjection.IServiceCollection,Apq.Cfg.Validation.IConfigValidator)">
- AddConfigValidator(IServiceCollection, IConfigValidator)
-
- </h3>
- <div class="markdown level1 summary"><p>添加配置验证器到服务集合</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddConfigValidator(this IServiceCollection services, IConfigValidator validator)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>validator</code> <a class="xref" href="Apq.Cfg.Validation.IConfigValidator.html">IConfigValidator</a></dt>
- <dd><p>验证器实例</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <a id="Apq_Cfg_ServiceCollectionExtensions_AddConfigValidator_" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddConfigValidator*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_AddConfigValidator_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_Validation_ConfigValidationBuilder__" data-uid="Apq.Cfg.ServiceCollectionExtensions.AddConfigValidator(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Apq.Cfg.Validation.ConfigValidationBuilder})">
- AddConfigValidator(IServiceCollection, Action<ConfigValidationBuilder>)
-
- </h3>
- <div class="markdown level1 summary"><p>添加配置验证器到服务集合</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection AddConfigValidator(this IServiceCollection services, Action<ConfigValidationBuilder> configure)</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>configure</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-1">Action</a><<a class="xref" href="Apq.Cfg.Validation.ConfigValidationBuilder.html">ConfigValidationBuilder</a>></dt>
- <dd><p>验证规则配置委托</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_AddConfigValidator_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_Action_Apq_Cfg_Validation_ConfigValidationBuilder___examples">Examples</h4>
- <pre><code class="lang-csharp">services.AddApqCfg(cfg => cfg.AddJson("config.json", level: 0));
- services.AddConfigValidator(v => v
- .Required("Database:ConnectionString")
- .Range("Database:Port", 1, 65535));</code></pre>
- <a id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.ConfigureApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String_" data-uid="Apq.Cfg.ServiceCollectionExtensions.ConfigureApqCfg``1(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.String)">
- ConfigureApqCfg<TOptions>(IServiceCollection, string)
-
- </h3>
- <div class="markdown level1 summary"><p>配置强类型选项(从 ICfgRoot 绑定),支持嵌套对象和集合</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection ConfigureApqCfg<TOptions>(this IServiceCollection services, string sectionKey) where TOptions : class, new()</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>sectionKey</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
- <dd><p>配置节键名,用于定位 TOptions 的配置数据</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section">Type Parameters</h4>
- <dl class="parameters">
- <dt><code>TOptions</code></dt>
- <dd><p>配置选项类型</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String__examples">Examples</h4>
- <pre><code class="lang-csharp">// 定义配置选项类
- public class DatabaseOptions
- {
- public string? ConnectionString { get; set; }
- public int Timeout { get; set; } = 30;
- public RetryOptions Retry { get; set; } = new();
- }
- public class RetryOptions
- {
- public int Count { get; set; } = 3;
- public int Delay { get; set; } = 1000;
- }
- // 注册配置服务
- services.AddApqCfg(cfg => cfg
- .AddJson("config.json", level: 0)
- .AddEnvironmentVariables(prefix: "APP_", level: 2));
- // 绑定强类型配置
- services.ConfigureApqCfg<DatabaseOptions>("Database");
- // 使用配置
- var dbOptions = serviceProvider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
- var retryCount = dbOptions.Retry.Count;</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String__remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>此方法会注册 IOptions<TOptions>、IOptionsMonitor<TOptions> 和 IOptionsSnapshot<TOptions> 服务。
- 配置选项类必须有无参构造函数。
- 支持嵌套对象和集合类型的自动绑定。</p>
- </div>
- <a id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg_" data-uid="Apq.Cfg.ServiceCollectionExtensions.ConfigureApqCfg*"></a>
- <h3 id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String_System_Action___0__" data-uid="Apq.Cfg.ServiceCollectionExtensions.ConfigureApqCfg``1(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.String,System.Action{``0})">
- ConfigureApqCfg<TOptions>(IServiceCollection, string, Action<TOptions>)
-
- </h3>
- <div class="markdown level1 summary"><p>配置强类型选项并启用配置变更监听</p>
- </div>
- <div class="markdown level1 conceptual"></div>
- <div class="codewrapper">
- <pre><code class="lang-csharp hljs">public static IServiceCollection ConfigureApqCfg<TOptions>(this IServiceCollection services, string sectionKey, Action<TOptions> onChange) where TOptions : class, new()</code></pre>
- </div>
- <h4 class="section">Parameters</h4>
- <dl class="parameters">
- <dt><code>services</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合</p>
- </dd>
- <dt><code>sectionKey</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.string">string</a></dt>
- <dd><p>配置节键名,用于定位 TOptions 的配置数据</p>
- </dd>
- <dt><code>onChange</code> <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.action-1">Action</a><TOptions></dt>
- <dd><p>配置变更回调函数,当配置发生变化时调用</p>
- </dd>
- </dl>
- <h4 class="section">Returns</h4>
- <dl class="parameters">
- <dt><a class="xref" href="https://learn.microsoft.com/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection">IServiceCollection</a></dt>
- <dd><p>服务集合,支持链式调用</p>
- </dd>
- </dl>
- <h4 class="section">Type Parameters</h4>
- <dl class="parameters">
- <dt><code>TOptions</code></dt>
- <dd><p>配置选项类型</p>
- </dd>
- </dl>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String_System_Action___0___examples">Examples</h4>
- <pre><code class="lang-csharp">// 定义配置选项类
- public class DatabaseOptions
- {
- public string? ConnectionString { get; set; }
- public int Timeout { get; set; } = 30;
- }
- // 注册配置服务并监听变更
- services.ConfigureApqCfg<DatabaseOptions>("Database", options => {
- Console.WriteLine($"数据库连接字符串已更新: {options.ConnectionString}");
- // 执行必要的重新连接逻辑
- ReconnectDatabase(options.ConnectionString);
- });
- void ReconnectDatabase(string connectionString)
- {
- // 实现数据库重新连接逻辑
- }</code></pre>
- <h4 class="section" id="Apq_Cfg_ServiceCollectionExtensions_ConfigureApqCfg__1_Microsoft_Extensions_DependencyInjection_IServiceCollection_System_String_System_Action___0___remarks">Remarks</h4>
- <div class="markdown level1 remarks"><p>此方法会注册 IOptions<TOptions>、IOptionsMonitor<TOptions> 和 IOptionsSnapshot<TOptions> 服务,
- 同时添加一个配置变更监听器。
- 变更回调会在配置源发生变化且导致 TOptions 实例更新时触发。
- 返回的 IDisposable 对象可用于取消监听,但已自动注册到服务容器,会在应用关闭时释放。</p>
- </div>
- </article>
- </div>
- <div class="affix">
- <nav id="affix"></nav>
- </div>
- </main>
- <div class="container-xxl search-results" id="search-results"></div>
- <footer class="border-top text-secondary">
- <div class="container-xxl">
- <div class="flex-fill">
- Apq.Cfg - .NET 统一配置组件库 (基于 .NET 10.0,兼容 .NET 8.0)
- </div>
- </div>
- </footer>
- </body>
- </html>
|