Controller in Dynamically Loaded Assembly

Last post 03-04-2008 2:11 AM by tehlike. 2 replies.

Sort Posts:

  • Controller in Dynamically Loaded Assembly

    03-03-2008, 3:41 PM
    • Member
      70 point Member
    • tehlike
    • Member since 11-19-2005, 3:21 AM
    • Posts 25

    Hello everybody,

    I am working on a blog application which is available at http://blogcontroller.googlecode.com/.

    I am now working on plugin architecture which will enable user to drop dll into plugins folder and then by invoking Start on the plugin object, it will do the necessary component registration, routing etc.

    The problem here is eventhough I use AppDomain.Current.Load() and do the routing, I can't make it work. This is the error that I get

    The controller for path '/Gallery/Index' could not be found or it does not implement the IController interface.

    But if I drop the dll into bin folder, everything works as expected.

     

    Thanks!

  • Re: Controller in Dynamically Loaded Assembly

    03-03-2008, 3:47 PM
    • Member
      70 point Member
    • tehlike
    • Member since 11-19-2005, 3:21 AM
    • Posts 25

    PS:
    When I look at assemblies loaded into AppDomain, I can see my plugin assembly.

  • Re: Controller in Dynamically Loaded Assembly

    03-04-2008, 2:11 AM
    Answer
    • Member
      70 point Member
    • tehlike
    • Member since 11-19-2005, 3:21 AM
    • Posts 25

    Yeap, there we go

     public class BlogRouteHandler : System.Web.Mvc.IRouteHandler
     {

      #region IRouteHandler Members

      public System.Web.IHttpHandler GetHttpHandler(System.Web.Mvc.RequestContext requestContext)
      {
       BlogMvcHandler blogHandler=new BlogMvcHandler();
       blogHandler.RequestContext = requestContext;
       return blogHandler;
      }

      #endregion
     }
     public class BlogMvcHandler:System.Web.Mvc.MvcHandler
     {
      protected override void ProcessRequest(IHttpContext httpContext)
      {
       if (this.RequestContext == null)
        throw new InvalidOperationException("No RequestContext");

       Type controllerType = this.RequestContext.RouteData.Values["controllertype"] as Type;


       IController controller = ControllerBuilder.Current.CreateController(this.RequestContext, controllerType);
       ControllerContext controllerContext = new ControllerContext(base.RequestContext, controller);
       controller.Execute(controllerContext);
      }


     }

     and I set the route like

    RouteTable.Routes.Add(new Route
    {
     Url = "Gallery/Index",
     Defaults = new { controller="gallery",controllertype = typeof(GalleryController), action = "Index" },
     RouteHandler = typeof(BlogRouteHandler)
    });

     

    A better way would be directly obtaining the controller type from the container but this works anyway.

    Not intended to use in production.

     

Page 1 of 1 (3 items)