如何修复“引发了类型为'Unity.Exceptions.InvalidRegistrationException'的异常”

时间:2019-10-28 06:46:35

标签: asp.net-mvc

我尝试登录我的应用程序,但它抛出此异常。我把它留给了正确运行的应用程序。 2天后,我开始重新处理此问题,因此现在出现了此异常。

我已经在两天前申请了工作单元。现在,今天在检查应用程序之前,我尝试通过添加“项目已存在”的操作方法来添加验证。然后我删除了此代码,仍然给出了此异常。没有得到。帐户控制器存在问题,因为它不允许登录。

 public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<ICategoryRepository, CategoryRepository>();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

它是UnityConfig。

 public class CategoryViewModel
{
    public int CategoryId { get; set; }

    [Required]
    [Remote("IsCategoryExists", "Category", HttpMethod = "POST", ErrorMessage = "This Category already exist.", AdditionalFields = "CategoryName,CategoryId")]
    [Display(Name ="Category Name")]
    public string CategoryName { get; set; }

    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }
}

这是类别的模型

public class CategoryController : AdminBaseController
{
    LaundryManagementSystemEntities db = new LaundryManagementSystemEntities();
    //private ICategoryRepository interfaceobj;

    private UnitOfWork unitOfWork;

    public CategoryController()
    {
       // this.interfaceobj = iCategory;
        //For Repositorypatterns
        //this.interfaceobj = new CategoryRepository(new LaundryManagementSystemEntities());

        //For Unit Of Work 
       this.unitOfWork = new UnitOfWork(new LaundryManagementSystemEntities());
    }

    // GET: Category        
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Create()
    {
        CategoryViewModel categoryViewModel = new CategoryViewModel();
        return PartialView("Create",categoryViewModel);
    }

    [HttpPost]
    public ActionResult Create(CategoryViewModel category)
    {
        if (ModelState.IsValid)
        { 
            unitOfWork.CategoryRepository.CreateCategory(category);
           // interfaceobj.CreateCategory(catogery);
        }
        return PartialView("Create",category);
    }

    //[HttpPost]
    //public JsonResult IsCategoryExists(string CategoryName, int CategoryId)
    //{
    //    bool found = false;

    //    if (CategoryId == 0)
    //    {
    //        found = db.Categories.Any(na => na.CategoryName == CategoryName);
    //    }
    //    else
    //    {

    //        found = db.Categories.Any(na => na.CategoryName == CategoryName && na.CategoryID != CategoryId);
    //    }
    //    if (!found)
    //    {
    //        return Json(true, JsonRequestBehavior.AllowGet);
    //    }
    //    return Json(false, JsonRequestBehavior.AllowGet);
    //}

这是我添加了IsCategoryExists方法的控制器,该方法暂时会被注释。

 ublic class UnitOfWork: IDisposable
{
    private CategoryRepository categoryRepository;
    private LaundryManagementSystemEntities _entities;

    public UnitOfWork(LaundryManagementSystemEntities entities)
    {
        this._entities = entities;
    }

    public CategoryRepository CategoryRepository
    {
        get
        {
            if (categoryRepository == null)
            {
                categoryRepository = new CategoryRepository(_entities);
            }
            return categoryRepository;
        }
    }

    public void Dispose()
    {
        _entities.Dispose();
    }

    public void Save()
    {
        _entities.SaveChanges();
    }
}

工作单元类别

  public class CategoryRepository : ICategoryRepository
{
    private LaundryManagementSystemEntities context;

    public CategoryRepository(LaundryManagementSystemEntities db)
    {
        this.context = db;
    }

    public void CreateCategory(CategoryViewModel categoryViewModel)
    {
        var category = new Category();
        category.CategoryName = categoryViewModel.CategoryName;
        category.IsActive = categoryViewModel.IsActive;
        context.Categories.Add(category);
        context.SaveChanges();
    }

    public void DeleteCategory(int categoryId)
    {

        Category category = context.Categories.Find(categoryId);
        if (category != null)
        {
            context.Categories.Remove(category);
            context.SaveChanges();
        }
    }

    public void DeleteProductOfCategory(int productId)
    {
         var listProducts = context.Products.Where(x => x.CategoryID == productId).ToList();
         foreach (var p in listProducts)
         {
             context.Entry(p).State = EntityState.Deleted;
         }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public CategoryViewModel GetEditCategory(int? categoryId)
    {
        Category category = context.Categories.Find(categoryId);
        var categoryViewModel = new CategoryViewModel();
        categoryViewModel.CategoryId = category.CategoryID;
        categoryViewModel.CategoryName = category.CategoryName;
        categoryViewModel.IsActive = category.IsActive;
        return categoryViewModel;          
    }

    public void PostEditCategory(CategoryViewModel model)
    {
        Category category = context.Categories.Find(model.CategoryId);
        category.CategoryID = model.CategoryId;
        category.CategoryName = model.CategoryName;
        category.IsActive = model.IsActive;
        context.SaveChanges();
    }

    public List<Category> GetCategories()
    {
        context.Configuration.ProxyCreationEnabled = false;
        return context.Categories.OrderBy(a => a.CategoryName).ToList();
    }

    public Category GetCategoryById(int? categoryId)
    {
        return context.Categories.Find(categoryId);
    }

这是类别存储库类

我只想知道为什么给了例外,我该怎么办。

1 个答案:

答案 0 :(得分:0)

这是我尝试过的答案,它正在起作用。 https://stackoverflow.com/a/45052060/12061392 最好的解释是最好的答案