autofac

What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependencyResolver in WebAPI

眉间皱痕 提交于 2019-11-26 15:34:58
问题 I have existing project, which uses AutoFac as IoC. In the registration code i have these lines: var resolver = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(resolver)); config.DependencyResolver = new AutofacWebApiDependencyResolver(resolver); So my question is what is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependecyResolver ? Why should I assign both of them? 回答1: Prevent mixing MVC and Web API in the same project.

Autofac - InstancePerHttpRequest vs InstancePerLifetimeScope

不羁的心 提交于 2019-11-26 15:17:35
问题 What are the differences between the two scopes? I am building Module (s) in each layer (Repository, Service, MVC App), but in order to have InstancePerHttpRequest you need the Autofac.Mvc assembly. Which scope should I be using in my Repository and Service layer? 回答1: InstancePerHttpRequest and InstancePerApiRequest essentially do the same thing - you get a single instance of your service for each discrete web request. I'll use InstancePerHttpRequest for the rest of the answer, but keep in

Resolve IContainer

戏子无情 提交于 2019-11-26 12:05:07
问题 What is the suggested method of getting the Autofac container from inside a class in the application? Does Autofac provide for resolving an IContainer property on a class or do I need to store the container globally once I\'ve build it? 回答1: You'll need IComponentContext or ILifetimeScope for most purposes. These are automatically provided by Autofac, so all you need to do is take a (constructor) dependency on one of them and the current instance will be injected. Both interfaces provide the

How to set ViewBag properties for all Views without using a base class for Controllers?

谁说我不能喝 提交于 2019-11-26 06:56:38
问题 In the past I\'ve stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller. This allowed my to use IoC on the base controller and not just reach out into global shared for such data. I\'m wondering if there is an alternate way of inserting this kind of code into the MVC pipeline? 回答1: Un-tried by me, but you might look at registering your views and then setting the view data during the

Does Ninject support Func (auto generated factory)?

允我心安 提交于 2019-11-26 05:29:55
问题 Autofac automatically generates factories for Func<T> ; I can even pass parameters. public class MyClass { public MyClass(Func<A> a, Func<int, B> b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If not, what workaround can I apply? Thanks. Update : Just found this post, seems the answer is no: How do I handle classes with static methods with Ninject? 回答1: NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https:/