C# Unity Resolve中的堆栈溢出异常

C# Unity Resolve中的堆栈溢出异常,c#,unity-container,C#,Unity Container,当我从unity容器解析类型时,StackOverflowException发生在: public class UserValidator : Validator<User> { readonly IBaseService<User> _service; public UserValidator() { _service = ApplicationResolver.Instance.Res

当我从unity容器解析类型时,StackOverflowException发生在:

   public class UserValidator : Validator<User>
   {
        readonly IBaseService<User> _service;
        public UserValidator()
        {
            _service = ApplicationResolver.Instance.Resolve<IBaseService<User>>();
            RuleFor(user => user.Email).EmailAddress();
        }

    }
寄存器

container.RegisterType<IValidator<User>, UserValidator>();
container.RegisterType();

我应该改变模式吗?

我有
BaseService
取决于
Validator
Validator
取决于
BaseService


Repository
使用存储/检索数据存储中的实体。

您有BaseService(取决于验证器)和Validator(取决于BaseService)。。。这会导致循环依赖并导致stackoverflow异常。您不应该有依赖于服务的验证器。服务应该使用验证器来验证实体,并使用存储库来存储/检索数据存储中的实体。@ChetanRanpariya我需要BaseService中的验证器来验证实体,如何在这里使用验证器?这就是我说的。。。。您应该在服务中使用验证器,但不应该在验证器中使用服务@阿米诺鲁兹普尔
container.RegisterType<IValidator<User>, UserValidator>();