Skip to content

Instantly share code, notes, and snippets.

@dylanberry
Last active May 24, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanberry/bd4e501505b4d0022574151b7f85519c to your computer and use it in GitHub Desktop.
Save dylanberry/bd4e501505b4d0022574151b7f85519c to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.Serialization;
namespace VersionCheck.Common
{
[Serializable]
public class ClientVersionNotSupportedException : Exception, ISerializable
{
public string MinimumSupportedClientVersionString { get; }
public string ApiVersionString { get; }
public string ClientVersionString { get; }
public ClientVersionNotSupportedException(string minimumSupportedClientVersionString, string apiVersionString, string clientVersionString)
{
MinimumSupportedClientVersionString = minimumSupportedClientVersionString;
ApiVersionString = apiVersionString;
ClientVersionString = clientVersionString;
}
protected ClientVersionNotSupportedException(SerializationInfo info, StreamingContext context)
{
MinimumSupportedClientVersionString = info.GetString(nameof(MinimumSupportedClientVersionString));
ApiVersionString = info.GetString(nameof(ApiVersionString));
ClientVersionString = info.GetString(nameof(ClientVersionString));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// NOTE: $type is a hint to allow simple deserialization to the type.
// This could also be done with a large amount of code and fussing around with MediaTypeFormatters.
info.AddValue("$type", GetType().FullName);
info.AddValue(nameof(MinimumSupportedClientVersionString), MinimumSupportedClientVersionString);
info.AddValue(nameof(ApiVersionString), ApiVersionString);
info.AddValue(nameof(ClientVersionString), ClientVersionString);
}
}
}
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using VersionCheck.API.Config;
using VersionCheck.API.VersionCheck;
using VersionCheck.Common;
namespace VersionCheck.API
{
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.AddMvc(options =>
{
options.Filters.Add(typeof(ApiVersionFilter));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddScoped<IVersionCheckService, VersionCheckService>();
services.AddScoped<MinimumClientVersionFilter>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler(
options => {
options.Run(HandleExceptions);
}
);
if (env.IsDevelopment())
{
//app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
private async Task HandleExceptions(HttpContext context)
{
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex?.Error is ClientVersionNotSupportedException versionEx)
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonConvert.SerializeObject(versionEx)).ConfigureAwait(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment