autofac

Autofac PerLifetimeScope vs PerRequest in web applications

杀马特。学长 韩版系。学妹 提交于 2019-12-24 15:22:48
问题 Using Autofac DI container- What is the difference between registering a unit of work in my web application as per request to registering it as PerLifetimeScope? Autofac creates a new scope for each request and by registering the unit of work as PerMatchingScope it will anyways be resolved from the scope created for the request. If I'm mistaken, please correct me, otherwise, what is the difference? Moreover, If I register the UoW as PerLifetimeScope, and have a console application that sends

How to wire up AutoFac to Common.Logging?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 15:22:00
问题 I have a class like so: public class LoggedFoo { private readonly ILog _logger; public LoggedFoo(ILog logger) { this._logger = logger; } public DoStuff() { this._logger.Info(i => i("Doing stuff...")); } } One of the business requirements is that logs are being generated for certain functions, so naturally I want to mock out the ILog to verify. However, Common.Logging library supports type-based loggers, along the lines of: var logger = LogManager.GetLogger<LoggedFoo>(); ...or: var logger =

Autofac shared objects require different registrations per controller but InstancePerApiControllerType won't work

 ̄綄美尐妖づ 提交于 2019-12-24 13:33:57
问题 As detailed in InstancePerApiControllerType not working, I am unable to use the InstancePerApiControllerType to configure my solution. The answer provided there works so long as I am directly injecting a ConnectionContext into the controller, or otherwise know that a class is only used by a specific controller. Unfortunately that is not the case in my situation: ControllerA -> EngineA -> RepositoryA -> GenericEntityAccessor ControllerB -> EngineB -> RepositoryB -> GenericEntityAccessor The

Autofac delegate factories, and passing around container

拜拜、爱过 提交于 2019-12-24 12:01:07
问题 I have a question about delegate factories: autofac docs I understand how they set up the factories but I do not get the resolving part: var shareholdingFactory = container.Resolve<Shareholding.Factory>(); var shareholding = shareholdingFactory.Invoke("ABC", 1234); It looks like you have to pass around the container in order to resolve. Maybe I have to Invoke something with parameters I only know at runtime. How do I do that without passing the container to for example a service method?

How to load records from Setting table of NopCommerce v3.5

China☆狼群 提交于 2019-12-24 11:56:24
问题 I'm trying to use NopCommerce v3.5 and trying to replace Autofac with Ninject v3.2. I've been searching how NopCommerce load records from Setting table, it's still not clear. And if ever, I want to load the settings using Ninject. Thank you for any help. Here's a sample code in Nop.web.Framework.DependencyRegistrar.cs builder.RegisterType<SettingService>().As<ISettingService>() .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")) .InstancePerLifetimeScope(); builder

Autofac and MVC integration: Register Type per API Controller

血红的双手。 提交于 2019-12-24 10:59:09
问题 I'm working with .Net 4.5, ASP.NET MVC 5, and the NuGet packages: Autofac 3.5.2 Autofac ASP.NET Web Api 5 Integration 3.0.0-rc1 I have 2 different implementations of an interface: public class MemoryStreamService : IStreamService { ... } public class HttpStreamService : IStreamService { ... } Also another concrete class that uses the interface by constructor injection: public class MainService : IMainService { public MainService(IStreamService streamService) { ... } } And I have 2 ASP.NET Web

Overriding IOC Registration for use with Integration Testing

前提是你 提交于 2019-12-24 10:27:56
问题 so I think I'm perhaps not fully understanding how you would use an IOC container for doing Integration tests. Let's assume I have a couple of classes: public class EmailComposer : IComposer { public EmailComposer(IEmailFormatter formatter) { ... } ... public string Write(string message) { ... return _formatter.Format(message); } } OK so for use during the real application (I'm using autofac here) I'd create a module and do something like: protected override void Load(ContainerBuilder

How to use AutoFac in Web API to resolve a service at runtime?

强颜欢笑 提交于 2019-12-24 10:25:22
问题 I have an API ( eg: ItemController.cs ) which would obtain the Authorization Token from the Request Header at run time. With the Token, then only I pass into my Service Class ( eg: ServiceItem.cs ). Here's how I did. At the Startup.cs, I register my ServiceItem var builder = new ContainerBuilder(); builder.RegisterType<ServiceItem>(); container = builder.Build(); //Note that, my container is a static variable In my API, I resolve it in this way: [Authorize] [Route("GetData")] [HttpGet] public

Common code for dependency injection for web api, admin panel and mvc controller

此生再无相见时 提交于 2019-12-24 10:08:52
问题 Currently, I have below 4 projects in my solution file : API (Web API) Web (MVC) Admin (MVC) Service Layer (C# Library) The service layer is being used by all the 3 web projects. The service is injected using Autofac container. The services are registered in each of the web projects during startup which is causing duplication of the code. Is there a simpler way where I can register all the dependencies in one place so that it can be reused by all the projects? Any help is highly appreciated.

Autofac MultiTenant with ASP.NET MVC3

心已入冬 提交于 2019-12-24 09:07:43
问题 Which version of Autofac supports MultiTenant for ASP.NET MVC 3? I could find this for ASP.NET MVC 4 回答1: The last version of Autofac released that supported MVC3 was 2.6.3. Not all packages were released on NuGet at that time; that happened starting with 3.0. The last time the Contrib packages (which contain the multitenant library) were released on the 2.6 line was 2.6.1. If you need an MVC3 stack, you'll need to go to the Autofac download list, switch the search filter to "Deprecated