خرید بک لینک

Vote count: 0

Hello I am having a hard time passing an HTTP scoped object into a fluent validation validator. Everything works until I try to inject a service and then it throws the following error

No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component

It comes down to the below snippet I think and I don't know how to mimic it in webapi

 public Func<T> HttpRequestScopedFactoryFor<T>(){
       retu () => system.Web.Mvc.DependencyResolver.Current.GetService<T>();
  }

IN MVC my autofac code looked like so

    public class FluentValidationModule : Module
    {
                public Func<T> HttpRequestScopedFactoryFor<T>()
                {
                    retu () => System.Web.Mvc.DependencyResolver.Current.GetService<T>();
                }

                protected override void Load(ContainerBuilder builder)
                {
                    base.Load(builder);
                    builder.RegisterType<ApplicationDbContext>().AsSelf().InstancePerRequest();
                    builder.RegisterInstance(HttpRequestScopedFactoryFor<ApplicationDbContext>());
                }
        }
     }

My FluentValidation validator code looked like so

public class RoleDTOValidator : AbstractValidator<RoleDTO>
    {
        readonly Func<ApplicationDbContext> _dbFactory;
        public RoleDTOValidator(Func<ApplicationDbContext> dbFactory)
        {
            _dbFactory = dbFactory;
            RuleFor(m => m.Id).NotEmpty();
            RuleFor(m => m.Name).NotEmpty().Must(DoesNotExist).WithMessage("Another permission exists with that name."); ;
            RuleFor(m => m.Description).NotEmpty();
            RuleFor(m => m.Permissions).NotEmpty().SetCollectionValidator(new ApplicationRoleClaimValidator());
        }

        bool DoesNotExist(RoleDTO model, string name)
        {
            ApplicationDbContext db = _dbFactory();
            int count = db.Roles.Count(item => item.Name == name);
            retu count == 0;
        }
    }

Any help is much appreciated.

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 24 ارديبهشت 1395 ساعت: 3:29

صفحه بندی