Skip to content

Instantly share code, notes, and snippets.

@dylanberry
Created May 24, 2019 12:03
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/b7e68619137a1ba2679e6ab4f0a94e5b to your computer and use it in GitHub Desktop.
Save dylanberry/b7e68619137a1ba2679e6ab4f0a94e5b to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Http;
namespace VersionCheck.API.VersionCheck
{
public interface IVersionCheckService
{
void PerformVersionCheck(HttpRequest request);
string MinimumClientVersion { get; }
}
}
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using VersionCheck.API.Config;
using VersionCheck.Common;
namespace VersionCheck.API.VersionCheck
{
public class VersionCheckService : IVersionCheckService
{
public VersionCheckService(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
public static readonly string ApiVersionString = Assembly.GetExecutingAssembly().GetName().Version.ToString();
private readonly AppSettings _appSettings;
public string MinimumClientVersion => _appSettings.MinimumSupportedClientVersion;
public void PerformVersionCheck(HttpRequest request)
{
var minimumSupportedClientVersionString = _appSettings.MinimumSupportedClientVersion;
if (request.Headers.TryGetValue(Common.HeaderKeys.ClientVersion, out StringValues clientVersionString) &&
!string.IsNullOrWhiteSpace(minimumSupportedClientVersionString))
{
var clientVersion = new Version(clientVersionString.SingleOrDefault());
var minimumSupportedAppVersion = new Version(minimumSupportedClientVersionString);
if (clientVersion.CompareTo(minimumSupportedAppVersion) < 0)
{
// min supported version is > client version
throw new ClientVersionNotSupportedException(minimumSupportedClientVersionString, ApiVersionString,
clientVersionString);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment