- Home
- .NET tutorials
- An efficient way of handling multiple actions through one route
An efficient way of handling multiple actions through one route
Published: Thursday 13 June 2019
The solution
// BackgroundService.cs
using System.Collections.Concurrent;
namespace MyProgram.Routes
{
public partial class BackgroundService : IBackgroundService
{
protected ConcurrentDictionary<int, TypeRoute> _types;
public BackgroundService()
{
UpdateURLs();
}
public virtual TypeRoute GetTypeRoute(string url)
{
// Use the URL of the page to get the appropriate "type" record
return _types.ContainsKey(url.GetHashCode()) ? _types[url.GetHashCode()] : null;
}
public virtual void UpdateURLs()
{
lock (this)
{
// Lock so no other thread can access whilst updating the URL's
// Use your DB query to get the different types
// Create new variable of "types" and store it against the class specific variable once all the data has been added.
var types = new ConcurrentDictionary<int, TypeRoute>();
types.AddOrUpdate(("type1").GetHashCode(), new TypeRoute { Url = "type1", Controller = "Type", Action = "Type1" }, (k2, l) => new TypeRoute { Url = "type1", Controller = "Type", Action = "Type1" });
_types = types;
}
}
}
public partial interface IBackgroundService
{
TypeRoute GetTypeRoute(string url);
void UpdateURLs();
}
public partial class TypeRoute
{
public virtual string Url { get; set; }
public virtual string Controller { get; set; }
public virtual string Action { get; set; }
}
}
// Startup.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace MyProgram
{
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().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Add singleton service to dependency injection
services.AddSingleton<IBackgroundService, BackgroundService>();
}
}
}
// TypeInsertUpdate.cs
using System.Threading;
using System.Threading.Tasks;
namespace MyProgram.Routes
{
public class TypeInsertUpdate
{
protected readonly IBackgroundService _backgroundService;
public TypeInsertUpdate(IBackgroundService backgroundService)
{
_backgroundService = backgroundService;
}
public virtual void Update()
{
// Do update task
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
Task.Factory.StartNew(() =>
{
// Async thread meaning the user does not need to hang around to finish.
_backgroundService.UpdateURLs();
}, cancellationToken);
}
}
}
Give us your anonymous feedback regarding this page, or any other areas of the website.
Watch .NET tutorials
Subscribe to our YouTube channel
Related tutorials
Common errors to avoid in ASP.NET Core dependency injection
Looking at how you can avoid common errors in ASP.NET Core dependency injection like not registering a service and circular dependency.