I created a custom validation attribute that I want to use for my API controller DTOs. This attribute needs values from the configured options, that's why I'm injecting them in the constructor, so that I can use the options service later on in the IsValid and FormatErrorMessage method.
internal class MyValidationAttribute : ValidationAttribute
{
private readonly IOptionsMonitor<MyOptions> myOptionsMonitor;
public MyValidationAttribute(IOptionsMonitor<MyOptions> myOptionsMonitor)
{
this.myOptionsMonitor = myOptionsMonitor;
}
public override bool IsValid(object value)
{
// ... use myOptionsMonitor here ...
return false;
}
public override string FormatErrorMessage(string name)
{
// ... use myOptionsMonitor here ...
return string.Empty;
}
}
Unfortunately when I want to use this as an attribute in my DTO
internal class MyDTO
{
[MyValidationAttribute]
public string Foo { get; set; }
}
I get the error message
There is no argument given that corresponds to the required formal
parameter 'myOptionsMonitor' of
'MyValidationAttribute.MyValidationAttribute(IOptionsMonitor)'
Is there a way I can use dependency injection for validation attributes? I know that I can use the ValidationContext like so
internal class MyValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
IOptionsMonitor<MyOptions> myOptionsMonitor = validationContext.GetService<IOptionsMonitor<MyOptions>>();
// ...
return ValidationResult.Success;
}
return new ValidationResult("Something failed");
}
}
But I want to use the FormatErrorMessage method from the base class and this has no access to the options service.
My current solution
For now, this is the code I'm using
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
internal class CustomValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
IOptionsMonitor<MyOptions> myOptionsMonitor = validationContext.GetService<IOptionsMonitor<MyOptions>>();
Dictionary<string, string> myMap = myOptionsMonitor.CurrentValue.MyMap;
string key = value.ToString() ?? string.Empty;
if (myMap.ContainsKey(key))
return ValidationResult.Success;
string[] formattedKeys = myMap.Keys.Select(key => $"'{key}'").ToArray();
string keysText = string.Join(" / ", formattedKeys);
string errorMessage = $"Invalid value. Valid ones are {keysText}";
return new ValidationResult(errorMessage);
}
}
Attributes are not designed for this purpose. But you can use action filters instead.
Let`s make your attribute as simple as it can be, we don't need any validation logic there.
[AttributeUsage(AttributeTargets.Property)]
public class CustomValidationAttribute : Attribute
{ }
For my example I created service that we are going to inject
public class SomeService
{
public bool IsValid(string str)
{
return str == "Valid";
}
}
and a class that we are going to validate
public class ClassToValidate
{
[CustomValidation]
public string ValidStr { get; set; } = "Valid";
[CustomValidation]
public string InvalidStr { get; set; } = "Invalid";
}
Now we can finally create action filter to validate our properties. In the snippet below, we hook into ASP.NET Core pipeline to execute code just before our controller action executes. Here I get action arguments and try to find CustomValidationAttribute on any property. If it is there, grab the value from the property, cast to type (I simply invoke .ToString()) and pass to your service. Based on value that is returned from service, we continue execution or add error to ModelState dictionary.
public class CustomValidationActionFilter : ActionFilterAttribute
{
private readonly SomeService someService;
public CustomValidationActionFilter(SomeService someService)
{
this.someService = someService;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var actionArguments = context.ActionArguments;
foreach (var actionArgument in actionArguments)
{
var propertiesWithAttributes = actionArgument.Value
.GetType()
.GetProperties()
.Where(x => x.GetCustomAttributes(true).Any(y => y.GetType() == typeof(CustomValidationAttribute)))
.ToList();
foreach (var property in propertiesWithAttributes)
{
var value = property.GetValue(actionArgument.Value).ToString();
if (someService.IsValid(value))
continue;
else
context.ModelState.AddModelError(property.Name, "ModelState is invalid!!!");
}
}
base.OnActionExecuting(context);
}
}
Don't forget to add your filter to the pipeline in Startup.cs!
services.AddMvc(x =>
{
x.Filters.Add(typeof(CustomValidationActionFilter));
});
Update:
If you strictly want to use dependency injection inside attribute, you could use service locator anti-pattern. For that we need to emulate DependencyResolver.Current from ASP.NET MVC
public class CustomValidationAttribute : ValidationAttribute
{
private IServiceProvider serviceProvider;
public CustomValidationAttribute()
{
serviceProvider = AppDependencyResolver.Current.GetService<IServiceProvider>();
}
public override bool IsValid(object value)
{
// scope is required for scoped services
using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<SomeService>();
return base.IsValid(value);
}
}
}
public class AppDependencyResolver
{
private static AppDependencyResolver _resolver;
public static AppDependencyResolver Current
{
get
{
if (_resolver == null)
throw new Exception("AppDependencyResolver not initialized. You should initialize it in Startup class");
return _resolver;
}
}
public static void Init(IServiceProvider services)
{
_resolver = new AppDependencyResolver(services);
}
private readonly IServiceProvider _serviceProvider;
public object GetService(Type serviceType)
{
return _serviceProvider.GetService(serviceType);
}
public T GetService<T>()
{
return (T)_serviceProvider.GetService(typeof(T));
}
private AppDependencyResolver(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
}
It should be initialized in Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
AppDependencyResolver.Init(app.ApplicationServices);
// other code
}
Related
I have an API with multiple endpoints. I'd like to add a property to all endpoint responses, without adding it to each endpoint response model individually.
Ex:
public class MyClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
Calling either endpoint returns a JSON representation of MyClass or MyOtherClass as appropriate - i.e.
{ "MyProperty":"Hello" } or { "MyOtherProperty":"World" }
I want to add a property, say a string ApiName, to all endpoints in the API, so that the result of the above code would be either (as appropriate)
{ "MyProperty":"Hello", "ApiName":"My awesome API" }
or
{ "MyOtherProperty":"World", "ApiName":"My awesome API" }
Is there a way to hook into the JSON-stringified result just before returning and add a top-level property like that? If so, I presume I'd have to wire it up in startup.cs, so I've been looking at app.UseEndpoints(...) methods, but haven't found anything that's worked so far. Either it's not added the property, or it's replaced the original result with the new property.
Thanks in advance!
Use Newtonsoft.Json in your net web api
Register a custom contract resolver in Startup.cs:
builder.Services.AddControllers()
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = CustomContractResolver.Instance);
The implementation:
public class CustomContractResolver : DefaultContractResolver {
public static CustomContractResolver Instance { get; } = new CustomContractResolver();
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
// add new property
...
properties.Add(newProp);
return properties;
}}
See more Json.net Add property to every class containing of a certain type
You can add a base class with the shared property. Should work for both XML and JSON.
public class MyApiClass
{
public string ApiName => "MyAwesomeApi";
}
public class MyClass : MyApiClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass : MyApiClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
My 0.02 cents says to implement an abstract base class.
Abstract class inheritance look similar to a standard inheritance.
public class MyClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyOtherProperty { get; set; } = "World";
}
However the abstract class will allow you to implement additional features in the event you need them in the future.
public abstract class MyAbstractClass{
[JsonPropertyName("API Name")]
public string ApiName{get;set;}="My Aweomse API";
//Just a thought if you want to keep track of the end point names
//while keeping your object names the same
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string EndPointName{
get{
return get_endpoint_name();
}}
private string get_endpoint_name(){
return this.GetType().Name;
}
//May as well make it easy to grab the JSON
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string As_JSON{
get {
return to_json();
}}
private string to_json(){
object _myObject = this;
string _out;
JsonSerializerOptions options =
new JsonSerializerOptions {
WriteIndented = true };
_out =
JsonSerializer.Serialize(_myObject, options);
return _out;
}
}
Probably should have implemented a generic return object, then you could just loop through the task results. I suppose you still can if you have the task return only the JSON string.
public static void run(){
Task<MyClass> _t0 = task0();
Task<MyOtherClass> _t1 = task1();
Task[] _tasks = new Task[]{_t0,_t1};
Task.WhenAll(_tasks).Wait();
Console.WriteLine(""
+$"{_t1.Result.ApiName}:\n"
+$"End Point: {_t1.Result.EndPointName}:\n"
+$"JSON:\n{_t1.Result.As_JSON}");
Console.WriteLine(""
+$"{_t0.Result.ApiName}:\n"
+$"End Point: {_t0.Result.EndPointName}:\n"
+$"JSON:\n{_t0.Result.As_JSON}");
}
private static Task<MyClass> task0(){
return Task.Run(()=>{
Console.WriteLine("Task 0 Doing Something");
return new MyClass();
});
}
private static Task<MyOtherClass> task1(){
return Task.Run(()=>{
Console.WriteLine("Task 1 Doing Something");
return new MyOtherClass();
});
}
And of course the aweosome...awesome:-) results:
Another thought is that you could implement your two different tasks as abstract methods, but that's a different conversation all together.
In addition to all of the great answers, I prefer to use Action Filter and ExpandoObject.
In Program File you should add your custom action Filter.
builder.Services.AddControllers(opt =>
{
opt.Filters.Add<ResponseHandler>();
});
and ResponseHandler acts like below:
public class ResponseHandler : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (var propertyInfo in (context.Result as ObjectResult).Value.GetType().GetProperties())
{
var currentValue = propertyInfo.GetValue((context.Result as ObjectResult).Value);
expando.Add(propertyInfo.Name, currentValue);
}
dynamic result = expando as ExpandoObject;
result.ApiName = context.ActionDescriptor.RouteValues["action"].ToString();
context.Result = new ObjectResult(result);
}
public void OnActionExecuting(ActionExecutingContext context)
{
}
}
I have a .Net 5 app and want to add validators for my configurations. Given this sample options
public sealed class DatabaseOptions
{
public string ConnectionString { get; set; }
}
I currently validate it with this implementation
public sealed class DatabaseOptionsValidator : IValidateOptions<DatabaseOptions>
{
public ValidateOptionsResult Validate(string name, DatabaseOptions databaseOptions)
{
List<string> validationFailures = new List<string>();
if (string.IsNullOrEmpty(databaseOptions.ConnectionString))
validationFailures.Add($"{nameof(databaseOptions.ConnectionString)} is required.");
// ...
if (validationFailures.Any())
{
return ValidateOptionsResult.Fail(validationFailures);
}
return ValidateOptionsResult.Success;
}
}
I would like to avoid implementing my own validation checks and error messages since I know data annotations already get the job done.
I modified the options model to this
public sealed class DatabaseOptions
{
[Required]
[MinLength(9999999)] // for testing purposes
public string ConnectionString { get; set; }
}
and was hoping to find a way to trigger the model validation
public sealed class DatabaseOptionsValidator : IValidateOptions<DatabaseOptions>
{
public ValidateOptionsResult Validate(string name, DatabaseOptions databaseOptions)
{
List<string> validationFailures = new List<string>();
// trigger the model validation and add every error to the validationFailures list
if (validationFailures.Any())
{
return ValidateOptionsResult.Fail(validationFailures);
}
return ValidateOptionsResult.Success;
}
}
but unfortunately I wasn't able to do so. The debugger hits the validator but how can I trigger the validation inside the Validate method?
Please have a look at the comments since my solution is already available!
Based on Rodrigo Rodrigues answer I created my own options validator based on data annotations
public sealed class OptionsValidator<TOptions> : IValidateOptions<TOptions> where TOptions : class
{
public ValidateOptionsResult Validate(string name, TOptions options)
{
ValidationContext validationContext = new ValidationContext(options);
List<ValidationResult> validationResults = new List<ValidationResult>();
bool noValidationErrorsOccured = Validator.TryValidateObject(options, validationContext, validationResults, true);
if (noValidationErrorsOccured) {
return ValidateOptionsResult.Success;
}
IEnumerable<string> validationFailures = validationResults.Select(validationResult => validationResult.ErrorMessage);
return ValidateOptionsResult.Fail(validationFailures);
}
}
So whenever I want to add a validator to my DI container I can make use of this extension method
public static IServiceCollection AddOptionsValidator<TOptions>(this IServiceCollection serviceCollection) where TOptions : class
=> serviceCollection.AddSingleton<IValidateOptions<TOptions>, OptionsValidator<TOptions>>();
There is a technique I use for validating data annotations in my netcore apps, not using IValidateOptions, but implementing a custom validator, and registering it as PostConfigure.
You can find valuable assets in the namespace System.ComponentModel.DataAnnotations.
Something like this:
// Custom validator for data annotations
public static class Validation {
public static void ValidateDataAnotations<TOptions>(TOptions options) {
var context = new ValidationContext(options);
var results = new List<ValidationResult>();
Validator.TryValidateObject(options, context, results, validateAllProperties: true);
if (results.Any()) {
var aggrErrors = string.Join(' ', results.Select(x => x.ErrorMessage));
var count = results.Count;
var configType = typeof(TOptions).Name;
throw new ApplicationException($"Found {count} configuration error(s) in {configType}: {aggrErrors}");
}
}
}
Then, you register this static method in you composition root (probably Startup.cs):
public void ConfigureServices(IConfiguration configuration, IServiceCollection serviceCollection) {
// (...)
serviceCollection.Configure<DatabaseOptions>(configuration.GetSection(nameof(DatabaseOptions)));
// invalid configuration values will break at this point
serviceCollection.PostConfigure<DatabaseOptions>(Validation.ValidateDataAnotations);
}
I am working on an ASP.Net Core API which will be hosted on OnPrem or Azure.
I have two DB contexts, KeyVaultDB and SQLDB. Both have the same tables.
I have an interface:
public interface ISecretsProvider
{
Dictionary<string,string> GetSecretNames(string env);
string GetSecret(string key);
bool SecretsExistForEnv(string ev);
}
This is implemented in a base class:
public class SecretsProvider : ISecretsProvider
{
public Dictionary<string,string> GetSecretNames(string env)
{
var result = _context.KeyVaultVersions.Select();
...
...
}
public virtual string GetSecret(string key)
{
throw new NotImplementedException();
}
public bool SecretsExistForEnv(string env)
{
var result = _context.KeyVautlVersions.Select();
...
...
}
}
I have two derived classes:
public class KeyVaultSecretProvider : SecretsProvider
{
public override string GetSecret(string key)
{
....
...
}
}
public class SQLSecretProvider : SecretsProvider
{
public override string GetSecret(string key)
{
....
...
}
}
In my Startup.CS, based on an environment variable, I do dependency injection of either of the derived classes:
var envVariable = Environment.GetEnvironmentVariable("");
if(envVariable.Equals("true")
{
services.AddTransient<IsecretsProvider, KeyVaultSecretProvider>();
}
else
{
services.AddTransient<IsecretsProvider, SQLSecretProvider>();
}
And in a different class:
public class ConfigProvider
{
private ISecretsProvider _secretsProvider;
public ConfigProvider(ISecretsProvider provider)
{
_secretsProvider = provider;
}
public JObject GetConfig(string env)
{
var result = _secretsProvider.GetSecretNames(env);
...
...
}
}
I don't know how to assign the _context in the base class. In the startup, if KeyVaultSecretsProvider is injected, KeyVaultDB context must be used to fetch secretnames in the base class.
If the SQLSecretsProvider is injected, the SQLDB context must be used to fetch secretnames in the base class.
container.RegisterType<IDataContextFactory<MyDataContext>, DefaultDataContextFactory<MyDataContext>>(new PerRequestLifetimeManager());
Created a PerRequestLifetimeManager using OperationContext but it does not seem call setValue function at all, it always trys to go to GetValue() function which always retruns null since nothing has been set.
My goal is to create a lifetimeManager for dbconetxt that will give me a new dbContext per method call. transient is not an option since it won;t work for join query.
public class WcfOperationContext : IExtension<OperationContext>
{
private readonly IDictionary<string, object> items;
private WcfOperationContext()
{
items = new Dictionary<string, object>();
}
public IDictionary<string, object> Items
{
get { return items; }
}
public static WcfOperationContext Current
{
get
{
WcfOperationContext context = OperationContext.Current.Extensions.Find<WcfOperationContext>();
if (context == null)
{
context = new WcfOperationContext();
OperationContext.Current.Extensions.Add(context);
}
return context;
}
}
public void Attach(OperationContext owner) { }
public void Detach(OperationContext owner) { }
}
public class PerRequestLifetimeManager : LifetimeManager
{
private string key;
public PerRequestLifetimeManager()
{
key = Guid.NewGuid().ToString();
}
public override object GetValue()
{
if (WcfOperationContext.Current == null)
{
return null;
}
else
{
return WcfOperationContext.Current.Items[key];
}
}
public override void RemoveValue()
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Remove(key);
}
}
public override void SetValue(object newValue)
{
if (WcfOperationContext.Current != null)
{
WcfOperationContext.Current.Items.Add(key, newValue);
}
}
}
My solution for this was to use this nuget package: UnityWCF
The Service should be instantiated by Unity and new instance per call.
For this use this settings on the service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ...
Inject DbContext where you need. And register in Unity like this:
container.RegisterType<DbContext, YourDbContext>(new HierarchicalLifetimeManager(), ...);
I trying to implement localization in .net core 1.0 application using IStringLocalizer. I am able to do the localization for the view for which I have written something like this
private readonly IStringLocalizer<AboutController> _localizer;
public AboutController(IStringLocalizer<AboutController> localizer)
{
_localizer = localizer;
}
public IActionResult About()
{
ViewBag.Name = _localizer["Name"];
Return View();
}
So this is working fine, however I am curious how can I use IStringLocalizer in CustomAttribute from where I will be getting localized validation message.
Model
public partial class LMS_User
{
[RequiredFieldValidator("FirstNameRequired")]
public string FirstName { get; set; }
[RequiredFieldValidator("LastNameRequired")]
public string LastName { get; set; }
}
from model I have passed the resource key to custom attribute where I will be retrieving the localized message.
Custom Attribute
public class RequiredFieldValidator: ValidationAttribute , IClientModelValidator
{
private readonly string resourcekey = string.Empty;
public RequiredFieldValidator(string resourceID)
{
resourcekey = resourceID;
}
}
public void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
// Here I want to get localized message using SQL.
var errorMessage = "This field is required field.";
MergeAttribute(context.Attributes, "data-val", "true");
MergeAttribute(context.Attributes, "data- val-Required",errorMessage);
}
private static bool MergeAttribute(IDictionary<string, string> attributes, string key, string value)
{
if (attributes.ContainsKey(key))
{
return false;
}
attributes.Add(key, value);
return true;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return ValidationResult.Success;
}
So, how can I use IStringLocalizer in custom attribute ? I want to do this using SQL.
Any help on this appreaciated !
I like to implement localization as a service.
public RequiredFieldValidator(IStringLocalizer localizationService, string resourceID)
{
resourcekey = resourceID;
localization = localizationService;
}
public void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
// Here I want to get localized message using SQL.
var errorMessage = lozalization["requiredFieldMessage"];
MergeAttribute(context.Attributes, "data-val", "true");
MergeAttribute(context.Attributes, "data- val-Required",errorMessage);
}
you can choose implement the interface using resource strings, accessing to database to get the translations,... Here i'm implementing one method accessing to resource strings, assuming that the resources are in the same project.
public class LocalizationService : IStringLocalizer {
public LocalizedString this[string name] {
return new LocalizedString(name, Properties.Resources.GetString(name));
}
//implement the rest of methods of IStringLocalizer
}