Created
April 13, 2016 09:36
-
-
Save TehWardy/8780bf86f1c225f86d1d065db1582e91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Core.ACOFI.Api; | |
using Core.Api; | |
using log4net; | |
using Newtonsoft.Json; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using System.Web.Http.Cors; | |
using System.Web.Http.Dispatcher; | |
using System.Web.Http.ExceptionHandling; | |
using System.Web.OData.Extensions; | |
namespace Api | |
{ | |
public static class WebApiStartup | |
{ | |
static readonly ILog log = LogManager.GetLogger(typeof(WebApiStartup)); | |
public static void Configure(HttpConfiguration config) | |
{ | |
log.Info("Initialising WebAPI"); | |
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; | |
config.Services.Replace(typeof(IHttpControllerActivator), new WebApiServiceActivator(config, Startup.Kernel)); | |
config.Services.Replace(typeof(IHttpControllerSelector), new GenericODataControllerSelector(config)); | |
config.EnableCors(new EnableCorsAttribute(origins: "*", headers: "*", methods: "*")); | |
config.Services.Add(typeof(IExceptionLogger), new ApiExceptionLogger()); | |
SetupOData(config); | |
config.EnsureInitialized(); | |
} | |
public static void SetupOData(HttpConfiguration config) | |
{ | |
log.Info("Initialising OData"); | |
config.EnableUnqualifiedNameCall(unqualifiedNameCall: true); | |
config.AddODataQueryFilter(); | |
config.MapHttpAttributeRoutes(); | |
var builder = new ACOFIModelBuilder(); | |
log.Info(" Initialising API context" + builder.GetType().Name.Replace("ModelBuilder", "")); | |
var modelContainer = builder.Build(); | |
config.MapODataServiceRoute( | |
routeName: modelContainer.Context + "Api", | |
routePrefix: "", | |
model: modelContainer.Model, | |
pathHandler: modelContainer.PathHandler, | |
routingConventions: modelContainer.RoutingConventions | |
); | |
} | |
} | |
public class ApiExceptionLogger : IExceptionLogger | |
{ | |
static readonly ILog log = LogManager.GetLogger(typeof(ApiExceptionLogger)); | |
public async Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken) | |
{ | |
log.Error("Exception occurred in request: "); | |
log.Error(context.Request.Method + " " + context.Request.RequestUri.OriginalString); | |
log.Error(await context.Request.Content.ReadAsStringAsync()); | |
log.Error(context.Exception.Message); | |
log.Error(context.Exception.StackTrace); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment