autofac

Using Autofac and Moqs with Delegate Factories

一笑奈何 提交于 2019-12-12 12:28:38
问题 I am trying to unit test a class that uses factory injection. I have a class that instantiates two copies of the same object (with different config) to control hardware. I'm trying to test the behaviour of the classes while simulating the hardware calls. I've injected a set of factory delegates into the constructors so that the class can instantiate the hardware classes as required. However I just can't work out how to control or create factory methods within the Autofac.Extras.Moq package.

Mocking and resolving Autofac dependency in integration test in AspNetCore with TestServer

谁说胖子不能爱 提交于 2019-12-12 12:19:17
问题 I'm using AspNetCore 2.2 Following (moreless) the docs here: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2 I am using Autofac, my Startup class has the following methods: public void ConfigureServices(IServiceCollection services) public void ConfigureContainer(ContainerBuilder containerBuilder) //this is where things can be registered directly with autofac and runs after ConfigureServices public void Configure(...) //the method called by runtime The

Cannot choose between multiple constructors with equal length 1 on type 'System.String'

﹥>﹥吖頭↗ 提交于 2019-12-12 11:32:13
问题 I am getting the following error when trying to resolve a type: Cannot choose between multiple constructors with equal length 1 on type 'System.String'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered. The type has 1 constructor that takes an IRepository and an ILog so I don't really know where the System.String is coming in to the picture. I'm perplexed. Does anyone have any idea what the problem is? Here is the stack

Register Autofac decorator for only one generic command handler

时光怂恿深爱的人放手 提交于 2019-12-12 09:39:23
问题 We have a lot of generic command handlers that are registered by Autofac in open-generic fashion. We have couple decorators that decorate all handles. Now I need to register a decorator for only one command handler and not affect all other command handlers. Here is my attempt on that, but I don't seem to get the registration right. Here is simple test code that is similar to our code: We have hundreds of commands working like this: class NormalCommand : ICommand { } // This command handler

A .NET Unit Test without a parameterless constructor, to facilitate dependency injection

末鹿安然 提交于 2019-12-12 08:23:08
问题 I'm trying to have the unit tests not rely on calling container.Resolve<T>() for their dependencies. I'm currently using AutoFac 2.2.4, and tried xUnit.NET and NUnit , but both have this issue : No parameterless constructor defined for this object How do I get past this issue? Is it a particular unit testing framework that will support this, or just how said framework is configured? Should I not be doing this? Or can I set up the test class to work with the constructor that has it's only

What does AsSelf do in autofac? [duplicate]

谁说我不能喝 提交于 2019-12-12 07:41:16
问题 This question already has an answer here : What is self-binding in an IoC container? (1 answer) Closed 2 years ago . What is AsSelf() in autofac? I am new to autofac, what exactly is AsSelf and what are the difference between the two below? builder.RegisterType<SomeType>().AsSelf().As<IService>(); builder.RegisterType<SomeType>().As<IService>(); Thank you! 回答1: Typically you would want to inject interfaces, rather than implementations into your classes. But let's assume you have: interface

AutoFac does not register api controller

限于喜欢 提交于 2019-12-12 06:15:34
问题 I am using AutoFac to registered api controller but don't know whats the problem Getting error Type 'myApp.WebAPI.Controllers.myController' does not have a default constructor I added IocHelper class as below public class IocHelper { public static IContainer CreateContainer() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); var container = builder.Build(); DependencyResolver

WCF service versioning with single SVC

南楼画角 提交于 2019-12-12 05:30:02
问题 Due to some certain requirement, I've got to use a single svc for multiple service versions. I've separated the interface contract for each version using different namespaces. I have only one class (partial) implementing all the service versions. My code is as below: namespace Application.V1 { [ServiceContract(Namespace = "http://google.com/ApplicationService/v1.0", Name = "IMathService")] public interface IMathService } namespace Application.V2 { [ServiceContract(Namespace = "http://google

Resolve Autofac dependencies in OWIN Startup class

时光毁灭记忆、已成空白 提交于 2019-12-12 03:58:24
问题 I have a multi-tenant MVC5 webapp, which is using Autofac v3.5.2 and Autofac.Mvc5 v3.3.4. My Autofac DI wiring takes place in a class within my MVC project. For authentication, we are using OWIN OpenId middleware to integrate with Azure B2C. In the OWIN Startup class I need a dependency to set tenantId / clientId using information from the current request. I try to grab the dependency via : DependencyResolver.Current.GetService<...>(); However, this always throws an ObjectDisposedException

Cannot pass parameters to controller constructor - Autofac MVC 5

早过忘川 提交于 2019-12-12 03:33:25
问题 I'm trying from 16 hours to fix one issue in my application. I've searched internet but couldn't find any solution. I'm using Onion Architecture with ASP.NET MVC. I cannot inject service into my controller. Controller: public class TestController : Controller { private readonly IService<Customer> _service; public TestController(IService<Customer> service) { _service = service; } public async Task<ActionResult> Index() { var list = await _service.GetAllAsync(); return View(list); } } IOC: