0

Sorry about the trouble guys, I have rephrased it as a new question here: MVC: Run a method on Application startup without calling it from Application_Start


This may be a very simple question but what's the best way to have an interface with a method OnStartup. When this interface is implemented, the OnStartup() code should run on application_start.

EDIT: I am actuall trying to create a HttpModule or something so that When I implement a particular class of this module, this class would have a method and by overriding this method, I will be able to run its code on application startup.

My only problem is that I don't want to call any code directly from application_start method.

P.S. If there is a nuget package available for this, please tell me the name.

3
  • @Inanikian I am doing it this way for a particular reason. Commented Apr 26, 2014 at 12:17
  • You will see how to create a custom method to start on Application_Start Commented Apr 26, 2014 at 12:21
  • please send me a link? Commented Apr 26, 2014 at 12:27

4 Answers 4

1

Define your interface and class:

interface IOnStartup
{
    void OnStartup();
}

class YourClass : IOnStartup
{
    public void OnStartup()
    {
    }
}

Add in Global.asax

public void Application_OnStart() 
{
    var cls = new YourClass();
    cls.OnStartup();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please see the edited question, actually i am not allowed to call it directly from Application_OnStart.
1

You cant run any code by implementing an interface you can just force your class to implement the code for OnStartup method

interface IOnStartup
{
    void OnStartup();
}

class MyClass : IOnStartup
{
    public void OnStartup()
    {
       // your code for OnStartUp method
       // you are forced to implement the code for this method because your class implement IOnStartup
    }
}

Or you can define an abstract class that implement this code and to inherit all classes that should have that method from this abstract class.

abstract class OnStartUpClass
{
    public void OnStartup()
    {
        // the code for this method
    }
}

class MyClass : OnStartUpClass
{
    // implement code for this class
    // you already have this method in your class
}

I think what you want to do is not achievable by implementing an interface. You should choose another way to do that.

4 Comments

How do I register this code to run when application starts?
If i understood correctly you have some classes with OnStartup() method and you want run all that method when the application starts ?
still not clear what you want to achieve, try to read more about Global.asax and may be delegates
Sorry about the trouble. I have just asked this in clear words here. stackoverflow.com/questions/23311217/…
1

If I understand you correctly, if there is a class that implements a given interface, you want that class to be instantiated and have it's OnStartup method called. If that is what you are trying to do then you have to rely on reflection.

In Application_Start call a method that will load all types present in your assembly and check if any of them implements that interface. If found you can instantiate an instance of it and call the method on that class.

If you want to add the class dynamically without recompiling your own application then it gets more complicated and involves creating AppDomains but it is also feasible.

EDIT

This question on stackoverflow tells you how to get all classes that implement an interface:

Getting all types that implement an interface

1 Comment

Thanks for this, can you please point me to any articles or samples for this?
0

I would use an IOC container like Autofac, Ninject, Unity. Then you can register the interface with the framework and use the service locator to return all instances of classes that implement the interface.

//using Autofac
var builder = new ContainerBuilder();
containerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                            .AsImplementedInterfaces()
                            .InstancePerLifetimeScope();

var container = builder.Build();
var myItems = Container.Resolve<ISomeInterface>();

foreach(var item in myItems){
    item.DoSomething()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.