如何修复"Exception of type 'Unity.Exceptions.InvalidRegistrationException' was thrown"



我试图登录我的应用程序,但它抛出了这个异常。我把它留给了正确运行的应用程序。两天后,我开始重新开始工作,所以现在它给出了这个例外。

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

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);
}

这是类别存储库类

我只是想知道为什么它给出了一个例外,我应该怎么做。

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

相关内容

最新更新