2

I have a ASP.NET MVC application and I'm using MEF to import my interfaces. The problem is that my IVotesRepository interface is null when it's passed to the constructor.

Here is the code from my controller:

public class VotesController : BaseController
{
    //
    // GET: /Votes/
    IVotesRepository VotesRepository;
    IAccountRepository accountRepository;

    public VotesController(IVotesRepository votesRepo, IAccountRepository accountRepo)
    {
        VotesRepository = votesRepo;
        accountRepository = accountRepo;
    }

And here is the interface and the repository itself:

public interface IVotesRepository
{
    void SaveVotes(int TeamId, int GameId, int UserId);

    bool CheckIfUserHaveVoted(int UserId, int GameId);
}

[Export(typeof(IVotesRepository))]
public class VotesRepository : IVotesRepository
{
   ...
}

Why is the IVotesRepository instance null?

EDIT:

Stack trace:

Object reference not set to an instance of an object at SocialSport.Controllers.VotesController.SaveVotes(Nullable`1 TeamId, Nullable`1 GameId) in C:\...\Visual Studio 2010\Projects\SocialSport\Implementation\Source\SocialSportWeb\Controllers\VotesController.cs:line 29
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
krock
  • 28,904
  • 13
  • 79
  • 85
  • Welcome to the site. If you need to do inline highlighting, use a pair of backticks (`). Otherwise, use the code formatting button (the pair of curly braces in the editor) for blocks of code, like your classes and interfaces. – Mike Bailey Dec 02 '12 at 02:21
  • I don't see the [`ImportingConstructorAttribute`](http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.importingconstructorattribute.aspx) on the constructor. See [here](http://stackoverflow.com/q/2008133/50776) on how to do it correctly. – casperOne Dec 02 '12 at 02:22
  • @casperOne I've already tried the `[ImportConstructor]` attribute and still doesn't work, the funny thing is that it was working before, and in my other controllers I have the same code but with other interfaces and they work, this one is the only one that doesn't work. – Rene Durazo Dec 02 '12 at 02:27

1 Answers1

-1

it seems that somewhere in my app I've unregistered that interface somehow, so I've made another interface and now it's working, sorry if it's a dirty fix, but I didn't had the time to make the proper corrections, thanks for your help.