title: Servicestack Multitenancy dynamic plugins
body: <p>We are moving from an on premise-like application to a multi tenant cloud application.</p>
  
  <p>for my web application we made a very simple interface based on IPlugin, to create a plugin architecture. (customers can have/install different plugins)</p>
  
  <pre><code>public interface IWebPlugin : IPlugin
  {
      string ContentBaseUrl { set; get; }
  }
  </code></pre>
  
  <p>We have some plugins that would normally be loaded in on startup. Now i'm migrating the code to load at the beginning of a request (the Register function is called on request start), and scope everything inside this request. 
  It's not ideal but it would bring the least impact on the plugin system for now.</p>
  
  <p>I could scope the Container by making an AppHost child container which would stick to the request:</p>
  
  <pre><code>    Container IHasContainer.Container
      {
          get
          {
              if (HasStarted)
                  return ChildContainer;
              return base.Container;
  
          } 
      }
      public Container ChildContainer
      {
          get { return HttpContext.Current.Items.GetOrAdd&lt;Container&gt;("ChildContainer", c =&gt; Container.CreateChildContainer()); }
      }
  </code></pre>
  
  <h2>problem case</h2>
  
  <p>Now im trying to make plugins work that actually add API services.</p>
  
  <pre><code>   appHost.Routes.Add&lt;GetTranslations&gt;("/Localizations/translations", ApplyTo.Get);
  </code></pre>
  
  <p>But this service is unreachable (and not visible in metadata). How do i make it reachable?</p>
  
  <p>I see you execute the following in ServiceController AfterInit. Re-executing this still wouldnt make it work.</p>
  
  <pre><code>    //Copied from servicestack repo
      public void AfterInit()
      {
          //Register any routes configured on Metadata.Routes
          foreach (var restPath in appHost.RestPaths)
          {
              RegisterRestPath(restPath);
  
              //Auto add Route Attributes so they're available in T.ToUrl() extension methods
              restPath.RequestType
                  .AddAttributes(new RouteAttribute(restPath.Path, restPath.AllowedVerbs)
                  {
                      Priority = restPath.Priority,
                      Summary = restPath.Summary,
                      Notes = restPath.Notes,
                  });
          }
  
          //Sync the RestPaths collections
          appHost.RestPaths.Clear();
          appHost.RestPaths.AddRange(RestPathMap.Values.SelectMany(x =&gt; x));
  
          appHost.Metadata.AfterInit();
      }
  </code></pre>
  
  <h2>solution directions</h2>
  
  <p>Is there a way i could override the route finding? like extending <code>RestHandler.FindMatchingRestPath(httpMethod, pathInfo, out contentType);</code></p>
  
  <p>Or could i restart the path compilation/caching? (would be enough for now that the service would be reachable tenant wide )</p>