autofac

Net core 3.0 with Autofac throw IServiceProvider isn't supported

烂漫一生 提交于 2019-12-01 08:35:48
问题 I have some trouble i try to resolve problem i use Autofac with .net core 3.0-6preview. I add new AutofacServiceProviderFactory() to CreateHostBuilder which is require in this .net core version framework. The code was working correctly in version 2.1 and lower but now application was crashed The exception: System.NotSupportedException: 'ConfigureServices returning an System.IServiceProvider isn't supported.' The Program class code: public class Program { public static void Main(string[] args)

Mock CloudBlobClient with AutoFac and AutoMock

為{幸葍}努か 提交于 2019-12-01 08:28:50
I'm trying to write unit tests for my AzureBlobRepository. The repository receives a CloubBlobClient in the constructor. I want to mock the client, but this gives an exception: using (var mock = AutoMock.GetLoose()) { var mockClient = mock.Mock<CloudBlobClient>(); } Cannot choose between multiple constructors with equal length 2 on type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered. Ofcourse, in my unit test I am not registering anything, so that message is not very

Mock CloudBlobClient with AutoFac and AutoMock

余生颓废 提交于 2019-12-01 07:30:18
问题 I'm trying to write unit tests for my AzureBlobRepository. The repository receives a CloubBlobClient in the constructor. I want to mock the client, but this gives an exception: using (var mock = AutoMock.GetLoose()) { var mockClient = mock.Mock<CloudBlobClient>(); } Cannot choose between multiple constructors with equal length 2 on type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component

How to use Autofac to inject specific implementation in constructor

做~自己de王妃 提交于 2019-12-01 05:17:10
I have two classes that take a ILastActivityUpdator as a constructor parameter: UserService and AnonymousUserService . public AnonymousUserService(ILastActivityUpdator lastActivityUpdator) { if (lastActivityUpdator == null) { throw new ArgumentNullException("lastActivityUpdator"); } this.lastActivityUpdator = lastActivityUpdator; } And similar as above for UserService : public UserService(ILastActivityUpdator lastActivityUpdator) { if (lastActivityUpdator == null) { throw new ArgumentNullException("lastActivityUpdator"); } this.lastActivityUpdator = lastActivityUpdator; } ILastActivityUpdator

Autofac passing parameter to nested types

家住魔仙堡 提交于 2019-12-01 04:10:26
I am using Autofac as my IoC in my WCF service. I have a situation where I want to pass an object to a nested type (ie a type that is not resolved directly, but when resolving another type). As far as I understood, passing this object as a constructor parameter is the preferred way in Autofac. Here is an example of such a situation. The nested type: public class EventLogger<T> : IEventLogger<T> { public EventLogger(IRepository<T> repository, User currentUser) { ... } } The type I am actually trying to resolve: public class SomeBusinessObject { public SomeBusinessObject(IEventLogger

Is order of dependencies guaranteed when injecting IEnumerable<T>

 ̄綄美尐妖づ 提交于 2019-12-01 03:08:08
I register in container services implementing IMyService. Do I have any guarantees about their order in container.Resolve<IEnumerable<IMyService>> ? Nicholas Blumhardt No, there's no ordering guaranteed here. We've considered extensions to enable it but for now it's something to handle manually. Just as extra help for people like me landing on this page... Here is an example how one could do it. public static class AutofacExtensions { private const string OrderString = "WithOrderTag"; private static int OrderCounter; public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle

Autofac attribute injection failing on attributes

痴心易碎 提交于 2019-12-01 02:45:39
I've found a few questions on this, but they tend to point to the exact documentation I'm following... but it's still not working. I'm building a fairly simple ASP.NET MVC 4 site, and the plan is to use ActionFilterAttribute -based logging. I have a DataAccessProvider class which opens transactions with the database and provides unit-of-work instances, and I'm trying to inject it into the filter attribute. The documentation says that it's enough to just call RegisterFilterProvider() , and ensure that the relevant types are registered. It specifically says that there is no need to register the

How to use Autofac to inject specific implementation in constructor

安稳与你 提交于 2019-12-01 02:09:05
问题 I have two classes that take a ILastActivityUpdator as a constructor parameter: UserService and AnonymousUserService . public AnonymousUserService(ILastActivityUpdator lastActivityUpdator) { if (lastActivityUpdator == null) { throw new ArgumentNullException("lastActivityUpdator"); } this.lastActivityUpdator = lastActivityUpdator; } And similar as above for UserService : public UserService(ILastActivityUpdator lastActivityUpdator) { if (lastActivityUpdator == null) { throw new

Solving Autofac issue Inheritance security rules violated while overriding member GetService

依然范特西╮ 提交于 2019-12-01 02:07:58
I've got an ASP.NET MVC application using Autofac. I've added the appropriate packages via: Install-Package Autofac Install-Package Autofac.Mvc4 When I ran the web application, this error was throw: Inheritance security rules violated while overriding member: 'Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(System.Type)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden. How can this be solved? p.campbell I had installed Autofac for MVC 4 when the project type was actually MVC 5. To solve this, I ran Uninstall

How to make AutoFac use same instance of nested dependency per top-level object? (SignalR dependency injection per hub)

六月ゝ 毕业季﹏ 提交于 2019-12-01 01:39:13
I am trying to set up my AutoFac registration in such a way that this test passes: [Test] public void Autofac_registration_test() { // Given var builder = new ContainerBuilder(); RegisterServices(builder); var container = builder.Build(); // When var firstHub = container.Resolve<Hub>(); var secondHub = container.Resolve<Hub>(); // Then firstHub.Should().NotBe(secondHub); firstHub.FooRepo.Context.Should().Be(firstHub.BarRepo.Context); firstHub.FooRepo.Context.Should().NotBe(secondHub.FooRepo.Context); } i.e. I want to use the same Context object all the way down within a single Hub , but use a