WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Events.WebAPI.Util.Extensions;
|
||||
|
||||
public static class AddJsonPatchSupportExtension
|
||||
{
|
||||
public static void AddJsonPatchSupport(this MvcOptions options)
|
||||
{
|
||||
options.InputFormatters.Insert(0, GetJsonPatchInputFormatter());
|
||||
}
|
||||
|
||||
// Add Newtonsoft only to the JSON Patch formatter so the rest of the API keeps System.Text.Json.
|
||||
private static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
|
||||
{
|
||||
var builder = new ServiceCollection()
|
||||
.AddLogging()
|
||||
.AddControllers()
|
||||
.AddNewtonsoftJson()
|
||||
.Services
|
||||
.BuildServiceProvider();
|
||||
|
||||
return builder
|
||||
.GetRequiredService<IOptions<MvcOptions>>()
|
||||
.Value
|
||||
.InputFormatters
|
||||
.OfType<NewtonsoftJsonPatchInputFormatter>()
|
||||
.First();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace Events.WebAPI.Util.Extensions;
|
||||
|
||||
public static class DictionaryExtensions
|
||||
{
|
||||
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key) where TValue : new() where TKey : notnull
|
||||
{
|
||||
if (!dict.ContainsKey(key))
|
||||
{
|
||||
var item = new TValue();
|
||||
dict[key] = item;
|
||||
return item;
|
||||
}
|
||||
else
|
||||
{
|
||||
return dict[key];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Events.WebAPI.Util.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Class with useful extensions for exceptions handling
|
||||
/// </summary>
|
||||
public static class ExceptionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// return complete hierarchy of an exception. It checks whether the exception has inner exception,
|
||||
/// and if it has, then it appends inner exception message.
|
||||
/// Then it looks for inner exception of the inner exceptions, and so on.
|
||||
/// </summary>
|
||||
/// <param name="exc">Exception which message hiearchy should be obtained</param>
|
||||
/// <returns>String containing all exception hierarchy messages</returns>
|
||||
public static string CompleteExceptionMessage(this Exception? exc)
|
||||
{
|
||||
StringBuilder sb = new();
|
||||
while (exc != null)
|
||||
{
|
||||
sb.AppendLine(exc.Message);
|
||||
exc = exc.InnerException;
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user