Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:01:18+00:00 2026-05-28T03:01:18+00:00

I am working on code for an ASP.NET MVC application that will do the

  • 0

I am working on code for an ASP.NET MVC application that will do the following when the application is started:

  • Load all assemblies in the application bin directory
  • Get all types from each assembly that are derived from an interface (ITask)
  • Invoke the Execute() method on each type

Here is the current idea I came up with. This method will get called in OnApplicationStarted():

    private void ExecuteTasks()
    {
        List<ITask> startupTasks = new List<ITask>();
        Assembly asm = this.ExecutingAssembly;

        // get path of executing (bin) folder 
        string codeBase = this.ExecutingAssembly.CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        string bin = Path.GetDirectoryName(path);
        string[] assemblies = Directory.GetFiles(bin, "*.dll");

        foreach (String file in assemblies)
        {
            try
            {
                if (File.Exists(file))
                {
                    // load the assembly
                    asm = Assembly.LoadFrom(file);

                    // get all types from the assembly that inherit ITask
                    var query = from t in asm.GetTypes()
                                where t.IsClass &&
t.GetInterface(typeof(ITask).FullName) != null
                                select t;

                    // add types to list of startup tasks
                    foreach (Type type in query)
                    {
                        startupTasks.Add((ITask)Activator.CreateInstance(type));
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
            }
        }

        // execute each startup task
        foreach (ITask task in startupTasks)
        {
            task.Execute();
        }
    }

My question: is there a better way to do any of these steps? The method to get the bin directory was taken from this answer: https://stackoverflow.com/a/283917/213159. It seems like a lot of work to do something simple, but I couldn’t figure out an easier approach.

Also, is using System.Activator to create instances and then subsequently invoke the Execute() method on each instance the most efficient way to perform that step?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:01:19+00:00Added an answer on May 28, 2026 at 3:01 am

    You may be able to clean the code up, but without any extension libraries the code doesn’t get all that much shorter.

    About performance, I’d not worry too much about optimizing OnApplicationStarted tasks in particular, it’s hopefully not called all that often and shouldn’t impact your site once it’s up and running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in ASP.NET MVC 1.0 using MvcContrib FluentHtml version 1.0. The following code
I'm working on a typical CRUD application in ASP.NET MVC where there will be
I'm working on an asp.net MVC application. I have a class that wraps a
I am working on an asp.net MVC 3 web application, i have the following
I am working on an ASP.NET page that we, in code impersonate the requesting
I have an ASP.NET MVC 2 form that is working perfectly, doing client side
I'm working on an ASP.NET MVC application, but I'm trying to remove everything but
I am working in an ASP.NET MVC Application. I have a view model as
I am working with the sample ASP.NET MVC 3 application. I am working on
I am working on a asp.net mvc application. I have a situation where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.