This repo contains several projects that provide with extended functionality for DotNetCore
framework. The projects are split into two category:
-
Bet.Extensions
generic functionality forDotNetCore
in general.Bet.Extensions.Options
- includes Application Options andBind()
validations.Bet.Extensions.Logging
- includes shared/common logging functionality.Bet.Extensions.ML
- includes Machine Learning library around ML.NET.Bet.Extensions.Hosting
- includes Generic Hosting functionality.Bet.Extensions.AzureVault
- includes Azure Vault functionality.Bet.Extensions
- includes extensions methods forDotNetCore
.
-
Bet.AspNetCore
specific functionality for web applications.Bet.AspNetCore.HealthChecks
contains HealthChecks for most common scenarios of the web application.Bet.AspNetCore.Logging
contains logging functionality forAspNetCore
applications.Bet.AspNetCore
- default location forAspNetCore
.Bet.AspNetCore.ReCapture
- ability to verify users submissions.
This library provides a helper classes for Microsoft C# based Machine Learning Library ML.NET.
To include Machine Learning prediction the following can be added:
services.AddModelPredictionEngine<SpamInput, SpamPrediction>(mlOptions =>
{
mlOptions.MLContext = () =>
{
var mlContext = new MLContext();
mlContext.ComponentCatalog.RegisterAssembly(typeof(LabelTransfomer).Assembly);
mlContext.Transforms.CustomMapping<LabelInput, LabelOutput>(LabelTransfomer.Transform, nameof(LabelTransfomer.Transform));
return mlContext;
};
mlOptions.CreateModel = (mlContext) =>
{
using (var fileStream = File.OpenRead("MLContent/SpamModel.zip"))
{
return mlContext.Model.Load(fileStream);
}
};
},"SpamModel");
Then in the API Controller:
[Route("api/[controller]")]
[ApiController]
public class PredictionController : ControllerBase
{
private readonly IModelPredictionEngine<SentimentObservation, SentimentPrediction> _sentimentModel;
private readonly IModelPredictionEngine<SpamInput, SpamPrediction> _spamModel;
public PredictionController(
IModelPredictionEngine<SentimentObservation, SentimentPrediction> sentimentModel,
IModelPredictionEngine<SpamInput, SpamPrediction> spamModel)
{
_sentimentModel = sentimentModel ?? throw new ArgumentNullException(nameof(sentimentModel));
_spamModel = spamModel ?? throw new ArgumentNullException(nameof(spamModel));
}
[HttpPost()]
public ActionResult<SentimentPrediction> GetSentiment(SentimentObservation input)
{
return _sentimentModel.Predict(input);
}
// GET /api/prediction/spam?text=Hello World
[HttpGet]
[Route("spam")]
public ActionResult<SpamPrediction> PredictSpam([FromQuery]string text)
{
return _spamModel.Predict(new SpamInput { Message = text});
}
}
Azure Vault is a great utility but what if your company doesn't allow you to have secret and client id. What if you want to use Docker Containers on your local machine and be able to authenticate your Azure Vault. This utility provides this functionality.
AppAuthenticaion CLI tool Readme
AspNetCore
has very flexible Configuration providers framework. Application can load Configurations from various locations. The last provider overrides the values of the same kind of keys.
Since it has such flexibility was introduces the validation of the Configurations was missing until 2.2
. AsptNetCore
2.2 release introduced extension methods on OptionsBuilder
which if registered will be validated on the first use of the Options, but there are cases where application needs to terminate at startup if the required configurations are missing. This is where the collection of the libraries from this solution come in.
dotnet add package Bet.AspNetCore
dotnet add package Bet.Extensions.Options
To enable validation add before any Configuration validation are added in Startup.cs
within ConfigureServices
services.AddConfigurationValidation();
Then add validation for Configurations as action call back or as data annotations
services.ConfigureWithValidation<FakeOptions>(Configuration, opt =>
{
if (opt.Id > 0 && !string.IsNullOrWhiteSpace(opt.Name))
{
return true;
}
return false;
}, "This didn't validated.");
services.ConfigureWithDataAnnotationsValidation<FakeOptionsWithDataAnnotations>(configuration, sectionName: "FakeOptions");
dotnet add package Bet.Extensions.Hosting
dotnet add package Bet.Extensions.Options
To enable validation use UseStartupFilter
in Program.cs
var host = new HostBuilder()
.UseStartupFilter().Build();
Usage of ConfigureWithDataAnnotationsValidation
or ConfigureWithValidation
the same as in IWebHost
Simply add this package to your application and use Bind
validation extensions methods
dotnet add package Bet.Extensions.Options
var options = config.Bind<MyOptionsWithDatatAnnotations>(options);
dotnet add package Bet.Extensions.AzureVault
This provider requires the following configurations to be present in any other configuration providers.
The ClientId
and ClientSecret
are not needed if used within Visual Studio.Net or in Azure Web Application with MSI enable.
"AzureVault": {
"BaseUrl": "https://kdcllc.vault.azure.net/",
"ClientId": "",
"ClientSecret": ""
}
At minimum this provider requires to have BaseUrl and will authenticated based on the Visual Studio.Net
Credentials. For Docker
Containers and other environments where Microsoft MSI can't be used provide ClientId
and ClientSecret
.
In order to use Azure Key Vault register it with IServiceCollection
.
configBuilder.AddAzureKeyVault(hostingEnviromentName:hostContext.HostingEnvironment.EnvironmentName);
Add the following to the project
dotnet add package Bet.Extensions.Logging
Sometimes we need to have tracing information enable to see where the configuration details are loaded from.
new HostBuilder()
.ConfigureAppConfiguration((context, configBuilder) =>
{
configBuilder.Build().DebugConfigurations();
}
Add the following to the project
dotnet add package Bet.AspNetCore.Logging
To enable Azure ApplicationInsights
and/or LogAnalytics
Seriglog sinks add the following in Program.cs
:
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console()
.AddApplicationInsights(hostingContext.Configuration)
.AddAzureLogAnalytics(hostingContext.Configuration);
})
Make sure that the following Options values are supplied.
"ApplicationInsights": {
"InstrumentationKey": "",
"EnableEvents": true,
"EnableTraces": true
},
"AzureLogAnalytics": {
"WorkspaceId": "",
"AuthenticationId": ""
}