2012-11-28

Nuget package: integrated MEF in Asp.Net MVC 4 and WebApi

,
Today i wanted to create my first Nuget package and i already knew what i would publish to the world. I wanted to make the code that is used to use MEF in Asp.Net MVC 4 and the WebApi (view this post) easy to reuse in other projects. I rewrote the code in a class libary.
After a little google-ing i found a video that explained how to create a Nuget package using the Nuget Package Explorer.  After that i created a user at the Nuget gallery so i got my API Key. With this, i had all the information i needed to published my first Nuget package and i have a feeling that it will not be my last :-)
The package can be installed from the Package Manager Console by typing: Install-Package MEF.MVC4

18 comments:

  1. Hi Kenny,

    I just grabbed your Nuget package (thank you), but am getting an exception when I attempt to hit a non-existent controller - eg http://localhost/site/foo.

    If the foo controller does not exist then I see the following error: Value cannot be null. Parameter name: type.

    Here's the stack trace:

    [ArgumentNullException: Value cannot be null.
    Parameter name: type]
    System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality) +263919
    System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(Type type, Type metadataViewType, String contractName) +41
    MEF.MVC4.MefControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +28
    System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +66
    System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +191
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155



    Hoping you can give me a hint as to what I've done wrong.

    Many thanks,
    Chris

    ReplyDelete
  2. You need to add the attribute Export at your controller. Otherwise the MefControllerFactory isn't able to resolve the controller that is requested.

    ReplyDelete
    Replies
    1. Hi Kenny, Nice article. I have one question though. If I create a BaseController for all my controllers, then do I need to associate the [Export] attribute to my BaseController as well the child controllers ? please see below, my Homecontroller is not importing any types, so I guess the below example is working fine although I have not associated any [Export] attribute to my home controller

      [Authorize]
      [Export]
      public class BaseController : Controller
      {
      [Import]
      private IViewModelProvider _viewProvider;
      protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {
      if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
      return;

      if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
      return;

      filterContext.Controller.ViewData["BrandsViewModel"] = _viewProvider.GetBrands();
      }
      }

      My Home Controller is as below

      public class HomeController : BaseController
      {
      public ActionResult Index()
      {
      return View();
      }

      }

      Delete
  3. Basicly when your application is requested for non-existent controller MefControllerFactory.GetControllerInstance is called with controllerType=null
    immedeately at GetControllerInstance start ExportProvider.GetExports is called with type parameter of null. That is the source of exception, because GetExports requires not null type parameter.

    It would be great if Kenny can fix this issue in future releases.

    ReplyDelete
  4. Thanks for this, its great! If I wanted to build controllers in a seperate dll, in which folder would I put it?

    ReplyDelete
  5. Hi Kenny, Nice article. I have one question though. If I create a BaseController for all my controllers, then do I need to associate the [Export] attribute to my BaseController as well the child controllers ? please see below, my Homecontroller is not importing any types, so I guess the below example is working fine although I have not associated any [Export] attribute to my home controller

    [Authorize]
    [Export]
    public class BaseController : Controller
    {
    [Import]
    private IViewModelProvider _viewProvider;
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
    return;

    if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
    return;

    filterContext.Controller.ViewData["BrandsViewModel"] = _viewProvider.GetBrands();
    }
    }

    My Home Controller is as below

    public class HomeController : BaseController
    {
    public ActionResult Index()
    {
    return View();
    }

    }

    ReplyDelete
  6. I look forward in reading more of your work. Also, I made sure to bookmark your website so I can come back later. I enjoyed every moment of reading it.SEO Liverpool

    ReplyDelete
  7. This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much. Keep up the good works.tartanarmymessageboard |

    mikestoanelighting |

    workplace-wellness |

    thumbswood |

    oldfieldrealty |

    utilitysearches |

    bizzybooker |

    restaurantrecruit |

    vetequip |

    cafe-vn |

    ReplyDelete
  8. Do you want to market a service or product but lack the knowledge to start? If this sounds like you, Facebook is an easy way to get started. You have the ability to reach millions of people. Keep reading to find out more.
    Lainey
    Layton
    Leonidas
    Lilliana
    London
    Lydia
    Madilynn
    Malcolm
    Marie
    Mason

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    Marleigh
    Maverick
    Melody
    Milana
    Monica
    Natalie
    Niko
    Olivia
    Paul
    Porter
    Randall
    Reese
    Riley
    Rory
    Ryann
    Samiyah
    Scarlet
    Shiloh
    Sonny
    Talia
    Terry
    Tori
    Tyrone
    Vincenzo
    Wilson
    Yusuf
    Zeke
    Abby
    Adeline
    Alaina

    ReplyDelete
  11. Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.
    Gracelynn
    Hadley
    Harry
    Holden
    Isabela
    Jackson
    Jamal
    Jasper
    Jayleen
    Jeremy
    Johan
    Joselyn
    Juliana
    Kaelyn
    Kamden
    Karter
    Kaylen
    Kenia
    Kimberly
    Krish
    Lachlan
    Layla
    Leonard
    Lilianna
    Logan
    Luke
    Madelyn
    Malakai
    Mariam
    Marvin

    ReplyDelete
  12. I think if we put all the logic in the client side, as well as the validation our server will accept all data and it will be so easy to crack the application. How do we handle this without putting validation on the server?
    Morgan
    Natasha
    Nina
    Orion
    Paxton
    Prince
    Raphael
    Reginald
    Rivka
    Rosalie
    Rylan
    Sandra
    Scott
    Siena
    Soren
    Tamia
    Thaddeus
    Trent
    Uriah
    Vivian
    Wren
    Zackary
    Zoe
    Addilyn
    Aiden
    Alec
    Aliana
    Alonzo
    Amira
    Angel

    ReplyDelete