57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using xApi.Models;
|
|
using xApi.Constants;
|
|
|
|
namespace xApi.Extensions
|
|
{
|
|
public static class DIHelperExtensions
|
|
{
|
|
/// <summary>
|
|
/// Extract Application Configuration based on appSetting file ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static ApiConfiguration? GetApiConfiguration(
|
|
this IConfiguration source
|
|
)
|
|
{
|
|
//
|
|
var section = source
|
|
.GetSection(ConfigurationNodeNames.ApiConfigurationNodeName);
|
|
var result = section.Get<ApiConfiguration>();
|
|
if (result is null)
|
|
{
|
|
//
|
|
result = new ApiConfiguration
|
|
{
|
|
Name = source[nameof(ApiConfiguration.Name)],
|
|
Version = source[nameof(ApiConfiguration.Version)],
|
|
Description = source[nameof(ApiConfiguration.Description)],
|
|
Contact = source.GetSection(nameof(ApiConfiguration.Contact)).Get<ApiConfigurationContact>()
|
|
};
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inject Specific Registered Service from IServiceCollection
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T? GetRegisteredService<T>(this IServiceCollection source)
|
|
{
|
|
//
|
|
T? result = default;
|
|
var serviceProvider = source.BuildServiceProvider();
|
|
if (serviceProvider is not null)
|
|
{
|
|
result = serviceProvider.GetService<T>();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |