Search code examples
c#wcfiiswcf-binding

Programmatically restarting IIS Hosted WCF service - change Bindings issue


I got into this not very good situation.. When web application starts - I set up different routes for my services so tenants of my multi-user app connect to:

private static void RegisterRoutes()
{
   // Setup URL's for each customer
   using (var cmc = new CoreModelContext())
   {
      foreach (var account in cmc.Accounts.Where(aa => aa.IsActive).ToList())
      {
         RouteTable.Routes.Add(
              new ServiceRoute(account.AccountId + "/mobile", 
                               new MyServiceHostFactory(), typeof(MobileService)));
      }
   }
}

So, when my site/service starts - it grabs all accounts from the database and sets up the routes.

This is a single point of failure right there. Sometimes servers rebooted in wrong order and if SQL Server not started - this service starts in "weird" mode.

Today web service stopped responding. I checked logs - IIS recycled pool as scheduled (default settings) and started different worked process. Something didn't click and boom - server stopped responding. Routes wasn't registered...

So. My question is.. How to fix it best way? I can put routes to config file, but that will mean I have to maintain those id's in 2 places. Probably not that bad but I'd rather do it differently if possible.

Is it possible to to programmatically try and restart pool? What happens when exception thrown in Application_Start ? Right now I'm not trapping it.


Solution

  • Not sure if this is a "fix" but when we've got similar dependency issues, we make sure the other dependencies cannot successfully start in "weird" mode. In this case, I would bring the app down hard if the sql server isn't avaliable, at least in production. Far better to have nothing being processed than have things being processed wrong.