asp.net web api - Using Autofac's resolver on demand in WebAPI perRequest to handle Circular errors -
I use autofac at the basic level to handle dependency injection. Currently my config is simple:
Builder. Registration Type & lt; Testdiccontax & gt; (). & Lt; DBCTNext, TestDB Contex & gt; (). InstanceRequest (); Builder.RegisterType & lt; UserRepository & gt; () In form of. & Lt; IUserRepository & gt; () InstancePerRequest () .; Builder.RegisterType & lt; TestRepository & gt; () In form of. & Lt; ITestRepository & gt; () InstancePerRequest () .;
is related to my problem:
I do not want to receive the Kirkular component dependency error, so I IUserRepository
in ITestRepository
Constructor (This is the other way round)
I wanted to use the second suggestion from the answer to the above question. How do I code my TestRepository
to use UserRepository
in on-demand? I have tried to use BeginLifetimeScope with the following attempts:
Public Class Test Rezistory: ITestRepository {Public Test Repository () {} Public Zero Test () {(var scope = AutofacConfig. Container.BeginLifetimeScope)) {var scopeUserRepo = scope.Resolve & lt; IUserRepository & gt; (); }}}
But I get the following exception: Any autofak web request that matches the tag
is not visible from that radius In which the instance was requested. 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
You can not just start a lifetime scope in the web API. / Strong> The request has been pasted with the request message to the scope of the lifetime. By manually making a request lifetime period like this, you are not receiving the results you are seeing - especially if you have a lifetime-liability dependent per-request because The realization that the scope you create will not be the same as in the lifetime. You will not be able to meet the lifelong partnership that you should get.
It should help you get it on your way. You may also be interested.
Comments
Post a Comment