简体   繁体   中英

vNext Application_Start() equivalent

I am trying to make this call one time during startup.

var assemblies = BuildManager.GetReferencedAssemblies();

This was brought over from a MVC 4 project.

Calling it from Startup.ConfigureServices or Startup.Configure throws an exception;

System.InvalidOperationException This method cannot be called during the application's pre-start initialization phase.

  1. Where in the vNext startup pipeline can I start making calls similar to this that do not conflict with pre-start initialization.

  2. Separate question, should I be making this explicit call BuildManager.GetReferencedAssemblies(); during startup. Reason for the call is to find all Assemblies for this app, find all areas in the app, find all controllers in the app, and impose filters on all of them. In the past I imposed Auth filters on every controller and did not rely on developers to put [Authorize] attributes on actions. It put a stop to surprise security holes showing up. Developers had to opt out via configuration from having this done to them, and that introduced a documented security audit trail.

I have researched the Middleware stuff but I like to do all my hookups up front and then let the app framework work as it likes.

Someone else suggested using OWIN, but I would like to follow vNext conventions if all possible and not do any mix and matching.

Another suggestion was putting Global.asax back in, etc.

Thanks for any direction that would be consistent to the vNext way.

You could listen on the ApplicationStarted event. Try this

public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime) {
    lifetime.ApplicationStarted.Register(() => {
        // Your code goes here
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM