inversion of control - Autofac - how to resolve Func for ISomething from Singleton where ISomething is InstancePerHttpRequest -


I am trying to use autofac to inject dependency in FluentValidation in an MVC 4 app. I feel that the strategy has been made, but I am stuck with my request to separate a singleton from a single one.

Here's the scenario: I've got an accredator that flirt validation's anabrator. I have read that FluentValidation validators perform best as singletons, then my constructor expects a funk and store which is factory to use later. When the verifier is used, then it should ask the stored factory for an IDataStore, get an example created for that request and use it. This is the principle that I want to give credit, which helped me organize this solution. Here is the author ... Public category SiteAdminViewModelValidator: Abbbetvilidate & lt; SiteAdminViewModel & gt; {Private Readonly Funk & lt; IDataStore & gt; _dbFactory; Public SiteAdminViewModelValidator (Func & lt; IDataStore & gt; dbFactory) {_dbFactory = dbfactory; RuleFor (model => SiteCode). Lambic (1, 40). BeSpecial; } Public Hair BeSpecial (string site code) {var db = _dbFactory (); & Lt; Content & gt; Accessories = db All & lt; Content & gt; (). ToList (); Back true; }}

If someone is trying to work at me, it would be good, but I would like to know the solution of this particular piece of Autofac tricksyness.

Here my validator registration is ...

  Public class FluentValidator module: module {secure override zero load (containerbuilder builder) {base.Load (builder); Builder.RegisterType & lt; AutofacValidatorFactory & gt; () In form of. & Lt; IValidatorFactory & gt; () SingleInstance () .; Var Validated = Assembly Schnaer.Fidvalidators in Asappad (System. Reflections.exceptions. Gate Accelerating Assembled ()); Validators.ToList (). ForEach (v = & gt; Builder registration type (v.ValidatorType). (V.InterfaceType). Single instance ()); }}   

Here is my registration for the IDataStore Factory ...

  builder.RegisterType & lt; SuperDB & gt; (). & Lt; IDataStore & gt; (). InstancePerHttpRequest (); Manufacturer & lt; Func & lt; IDataStore & gt; & Gt; (C = & gt; {Reference Reference = C. Resolve & lt; IComponentContext & gt; (); Return Reference; Reload & lt; IDataStore & gt;;});   

I get this error when my verifyer asks an IDataStore on line - var db = _dbFactory ();

Tag matching appears on the 'AutoFac Web Requirement' radius, in which the example request was made. It usually indicates that the request is being requested by a single interaction () component (or a similar scenario) registered as a per-HTTP request. Under web integration always requests reliance dependency dependency. Current or iialim scoppriver Request Lifetime, Never by Container

... which I have actually found when I wrote it before writing my factory registration - Fanak Registration, by reading the various answers of similar questions, it seems that What should work because I thought that now I am solving Funk to get the current solutioner.

Any help would be greatly appreciated.

I agree that this should work - Func & lt; IDataStore & gt; Defining a factory to produce dependency in each method as per requirement.

The way I got this method, it is to use DependencyResolver.Current as an error message. The main reason is that I have already installed it using the Autofac.Mvc4 nuget package ... Dependency Resolver .settersolar (new AutofacDependencyResolver (container));

So to actually set up the method I have the following func

  Public Func & lt; T & gt; PerHttpSafeResolve & lt; T & gt; () {Return () = & gt; DependencyResolver.Current.GetService & LT; T & gt; (); }   

and when creating a container

  builder.RegisterType & lt; SuperDB & gt; (). & Lt; IDataStore & gt; (). InstancePerHttpRequest (); Builder.RegisterInstance (PerHttpSafeResolve & lt; IDataStore & gt; ());   

Edit: Entering the example is saying the second row - If you see funk & lt; IDataStore & gt; , then use the value given in the method PerhttpSafeResolve & lt; IDataStore & gt; The result of is only a function, so it can remain as an example

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -