2

I have ASP MVC Core application.

After application started i've generate dynamic assembly with some controllers. The problem is that RouteMiddleware does not process requests to generated controllers.

The problem is solved if i call

services.AddMvc()
        .AddApplicationPart(...)

and pass generated assembly to ApplicationPartManager in Startup.ConfigureServices method, but i need to generate Assembly after application started.

Maxim
  • 1,413
  • 2
  • 10
  • 15
  • You can't and you shouldn't. Route tables are created at application start time and are not changed anymore. There was a post somewhere on SO or GitHub issue where the ASP.NET Core clearly advised against attempting to change routes after application startup – Tseng Jan 29 '18 at 12:08
  • [this](https://stackoverflow.com/a/32586837/455493) may be helpful hint in the right direction. Its pretty old (from beta times) , still referencing ASP.NET MVC6 (now called ASP.NET Core) and the names of the methods, interfaces etc. may have changed by now. But implementing `IRouter` yourself and registering it seems the way to go – Tseng Jan 29 '18 at 12:18

1 Answers1

2

I add my own IActionDescriptorChangeProvider

internal class ModuleActionDescriptorChangeProvider : IActionDescriptorChangeProvider
{
    internal static ModuleActionDescriptorChangeProvider Instance { get; } = new ModuleActionDescriptorChangeProvider();

    internal CancellationTokenSource TokenSource { get; private set; }

    public IChangeToken GetChangeToken()
    {
        TokenSource = new CancellationTokenSource();
        return new CancellationChangeToken(TokenSource.Token);
    }
}

register in service collection

services.AddSingleton<IActionDescriptorChangeProvider>(ModuleActionDescriptorChangeProvider.Instance);

and then after assembly generated call

ModuleActionDescriptorChangeProvider.Instance.TokenSource.Cancel();

to signal invalidation of the cached collection of ActionDescriptor

Maxim
  • 1,413
  • 2
  • 10
  • 15
  • Hi @Maxim, I am trying to do something like you. I am getting controller code from user by using a textarea, I am compiling them, and I want to add this compiled code as an applicationPart. Can you help me to find right resources about this topic. I mean, any article, tutorial, or link would be appropriate. Thanks. – AliRıza Adıyahşi Apr 10 '23 at 19:13