This solution should help me explain my desire

This commit is contained in:
Tracy Pearson
2020-10-19 12:55:29 -04:00
parent 4e9964435b
commit 92445d4e7a
18 changed files with 280 additions and 71 deletions

View File

@@ -1,14 +1,21 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RepositoryLibrary;
using SharedLibrary.Models;
namespace ApiService
{
@@ -24,6 +31,7 @@ namespace ApiService
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IRepository, LocalRepository>();
services.AddControllers();
}
@@ -37,6 +45,30 @@ namespace ApiService
app.UseRouting();
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (Exception ex)
{
var error = new RepositoryError
{
Information = ex.Message,
StackTrack = ex.StackTrace
};
var dc = new DataContractJsonSerializer(typeof(RepositoryError));
using (var ms = new MemoryStream())
{
dc.WriteObject(ms, error);
var response = new StringBuilder(Encoding.UTF8.GetString(ms.ToArray()));
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(response.ToString());
}
}
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>