what is the global.asax Application_Start equivalent when using WAS in IIS7

27,784

Solution 1

I believe AppInitialize() is the method you're looking for. Here's an article on using it to initialise Castle Windsor in a WAS hosted WCF service:

Castle Windsor and non-HTTP Protocol WCF Services

The essence of the article is, instead of using Application_Start() which won't get called in WAS:

protected void Application_Start(object sender, EventArgs e)
{
   var container = new WindsorContainer("ioc.config");
   DefaultServiceHostFactory.RegisterContainer(container.Kernel);
}

Use:

public class InitialiseService
{
   /// <summary>
   /// Application initialisation method where we register our IOC container.
   /// </summary>
   public static void AppInitialize()
   {
      var container = new WindsorContainer("ioc.config");
      DefaultServiceHostFactory.RegisterContainer(container.Kernel);
   }
}

To quote Matt:

I confess I spent a while looking at the Host Factory in more detail, looking to wrap the DefaultServiceHostFactory. However, there appears to be a far simpler solution and that is to make use of the little documented AppInitialize method. If you create a class (any class), put it into the ASP.NET App_Code folder in your project and give it a method signature as defined below, this little baby will get fired exactly when you want it to. You can then initialise your IoC container in there.

Solution 2

AppInitialize is a valid method of initializing your service. But there are some other methods that might work better for you and they are described in this article: How to Initialize Hosted WCF Services

NOTE: the original link went away. The link above is to a copy on archive.org.

Share:
27,784
Mike Tours
Author by

Mike Tours

Updated on July 08, 2020

Comments

  • Mike Tours
    Mike Tours almost 4 years

    I'd like to use the netTcpBinding for my WCF application which is currently hosted in IIS7, which means configuring it to use WAS instead. This is fairly straight forward however, my application previously made use of the Application_Start event in the global.asax file. I do not require access to the httpContext(which I understand access has been removed in IIS7), however I would still like to hook into the start or init methods?

    Does an equivalent exist when hosting an application in WAS as apposed to IIS7?

    Using classic mode is not an option(again I'm not interested in the httpcontext and this only appears to work if using an http binding) - and I've seen an example of putting a static class instide the app_code folder which looks like a horrible hack.

  • Mike Tours
    Mike Tours about 14 years
    Thanks for the respone. I had come across that, but it looks rather "hacky" to put it lightly ... I really didn't think I'd have to rely on the app_code folder .... is there really not other suitable replacement?
  • Mike Tours
    Mike Tours over 12 years
    Thanks Graham - I've not hada chance to look at this piece of code to validate it, but the article does look like a better option than having to rely in the other hacky solutions.
  • abatishchev
    abatishchev over 11 years
    But seems it's being ignored by VS Web Server (aka Casini, ASP.NET Dev Server)
  • Kev
    Kev over 11 years
    @abatishchev - What about with IIS Express?
  • abatishchev
    abatishchev over 11 years
    The same. I have to develop my own WCF extension to make it working regardless the host a service is running under. Weird. Wondering why there is no such built-in functionality.
  • Nelson Rothermel
    Nelson Rothermel over 10 years
    Application_Start runs one for the whole application. Using a service factory like your link mentions would run once per service, so you would need additional logic to prevent your code from running twice.
  • Ashish Shukla
    Ashish Shukla almost 4 years
    @grahamesd link mentioned in this answer is not found 404 page is coming
  • Chuck Lu
    Chuck Lu over 3 years
    The article can also be found here