I use autofac and can pass parameters to my resolve method.
How can I do this using microsofts DependencyResolver interface?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The IDependencyResolver does not support passing parameters directly, as I’m sure you have noticed. However, since you have Autofac under the hood, you’re able to resolve a factory delegate that enables you to pass on parameters to the underlying service:
Note: you can either use
Funcdelegates or explicitly defined factory delegates. More on this here.Regarding lifetime scopes: factory delegates must be resolved from a scope where the requested service can be “reached”. Consider this setup simulating how MVC or WebApi would look like:
With this setup, our
Xservice will only be available in the http scope. Trying to resolveXfromapplicationscope will fail with this message:Resolving from the
requestscope will work as expected: