81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
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
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
// 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();
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
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 =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|