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.

Community
  • 1
  • 1
user3219798
  • 333
  • 6
  • 14

4 Answers4

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.

Adrian
  • 693
  • 5
  • 19
  • How do I register this code to run when application starts? – user3219798 Apr 26 '14 at 12:24
  • If i understood correctly you have some classes with OnStartup() method and you want run all that method when the application starts ? – Adrian Apr 26 '14 at 12:29
  • still not clear what you want to achieve, try to read more about Global.asax and may be delegates – Adrian Apr 26 '14 at 12:49
  • Sorry about the trouble. I have just asked this in clear words here. http://stackoverflow.com/questions/23311217/mvc-run-a-method-on-application-startup-without-calling-it-from-application-sta – user3219798 Apr 26 '14 at 13:00
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();
}
leskovar
  • 661
  • 3
  • 8
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

Community
  • 1
  • 1
JTMon
  • 3,189
  • 22
  • 24
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()
}
Honorable Chow
  • 3,097
  • 3
  • 22
  • 22