2012-12-02 92 views
2

我有一個ASP.NET MVC應用程序,我正在使用MEF導入我的接口。問題是我的IVotesRepository接口在傳遞給構造函數時爲空。構造函數中的空接口

這裏是我的控制器代碼:

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) 
+0

歡迎來到本網站。如果您需要進行內聯突出顯示,請使用一對反引號(')。否則,使用代碼格式化按鈕(編輯器中的一對花括號)來獲取代碼塊,如類和接口。 –

+0

我在構造函數中看不到['ImportingConstructorAttribute'](http://msdn.microsoft.com/zh-cn/library/system.componentmodel.composition.importingconstructorattribute.aspx)。請參閱[這裏](http://stackoverflow.com/q/2008133/50776)關於如何正確執行此操作。 – casperOne

+0

感謝您的幫助。 –

回答

-1

似乎某處在我的應用我有未註冊該接口不知何故,所以我做了另外一個接口,現在它的工作,對不起,如果這是一個骯髒的修復,但我沒有時間做出適當的更正,感謝您的幫助。