Skip to content

Instantly share code, notes, and snippets.

@michaldudak
Last active August 22, 2017 14:44
Show Gist options
  • Save michaldudak/4eb6b0b26405543cff4c4f01a51ea869 to your computer and use it in GitHub Desktop.
Save michaldudak/4eb6b0b26405543cff4c4f01a51ea869 to your computer and use it in GitHub Desktop.
Dynamic setup of file server
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
namespace DynamicMiddleware.Controllers
{
public class HomeController : Controller
{
private readonly MiddlewareInjectorOptions middlewareInjectorOptions;
public HomeController(MiddlewareInjectorOptions middlewareInjectorOptions)
{
this.middlewareInjectorOptions = middlewareInjectorOptions;
}
[HttpGet("")]
public ActionResult Index()
{
return Content("Hello!");
}
[HttpGet("enable-file-server")]
public ActionResult EnableFileServer()
{
middlewareInjectorOptions.InjectMiddleware(app =>
{
var cFileProvider = new PhysicalFileProvider("C:\\");
app.UseFileServer(new FileServerOptions
{
RequestPath = "/c",
FileProvider = cFileProvider,
EnableDirectoryBrowsing = true
});
var dFileProvider = new PhysicalFileProvider("D:\\");
app.UseFileServer(new FileServerOptions
{
RequestPath = "/d",
FileProvider = cFileProvider,
EnableDirectoryBrowsing = true
});
});
return Content("File server enabled");
}
}
}
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using System.Threading;
using Microsoft.AspNetCore.Http;
namespace DynamicMiddleware
{
public static class MiddlewareInjectorExtensions
{
public static IApplicationBuilder UseMiddlewareInjector(this IApplicationBuilder builder, MiddlewareInjectorOptions options)
{
return builder.UseMiddleware<MiddlewareInjectorMiddleware>(builder.New(), options);
}
}
public class MiddlewareInjectorMiddleware
{
private readonly RequestDelegate _next;
private readonly IApplicationBuilder _builder;
private readonly MiddlewareInjectorOptions _options;
private RequestDelegate _subPipeline;
public MiddlewareInjectorMiddleware(RequestDelegate next, IApplicationBuilder builder, MiddlewareInjectorOptions options)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_builder = builder ?? throw new ArgumentNullException(nameof(builder));
_options = options ?? throw new ArgumentNullException(nameof(options));
}
public Task Invoke(HttpContext httpContext)
{
var injector = _options.GetInjector();
if (injector != null)
{
var builder = _builder.New();
injector(builder);
builder.Run(_next);
_subPipeline = builder.Build();
}
if (_subPipeline != null)
{
return _subPipeline(httpContext);
}
return _next(httpContext);
}
}
public class MiddlewareInjectorOptions
{
private Action<IApplicationBuilder> _injector;
public void InjectMiddleware(Action<IApplicationBuilder> builder)
{
Interlocked.Exchange(ref _injector, builder);
}
internal Action<IApplicationBuilder> GetInjector()
{
return Interlocked.Exchange(ref _injector, null);
}
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace DynamicMiddleware
{
public class Startup
{
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<MiddlewareInjectorOptions>();
serviceCollection.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
var injectorOptions = app.ApplicationServices.GetService<MiddlewareInjectorOptions>();
app.UseMiddlewareInjector(injectorOptions);
app.UseMvc();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment