Имам ASP.NET MVC приложение и използвам MEF за импортиране на моите интерфейси. Проблемът е, че моят IVotesRepository
интерфейс е null, когато се предава на конструктора.
Ето кода от моя контролер:
public class VotesController : BaseController
{
//
// GET: /Votes/
IVotesRepository VotesRepository;
IAccountRepository accountRepository;
public VotesController(IVotesRepository votesRepo, IAccountRepository accountRepo)
{
VotesRepository = votesRepo;
accountRepository = accountRepo;
}
И ето интерфейса и самото хранилище:
public interface IVotesRepository
{
void SaveVotes(int TeamId, int GameId, int UserId);
bool CheckIfUserHaveVoted(int UserId, int GameId);
}
[Export(typeof(IVotesRepository))]
public class VotesRepository : IVotesRepository
{
...
}
Защо екземплярът IVotesRepository
е нулев?
РЕДАКТИРАНЕ:
Проследяване на стека:
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)
ImportingConstructorAttribute
на конструктора. Вижте тук как да го направите правилно. - person casperOne   schedule 02.12.2012[ImportConstructor]
и все още не работи, смешното е, че работеше преди и в другите ми контролери имам същия код, но с други интерфейси и те работят, този е единственият такъв, който не работи. - person Rene Durazo   schedule 02.12.2012