autofac

Registering implementations of base class with Autofac to pass in via IEnumerable

放肆的年华 提交于 2020-01-02 01:10:19
问题 I have a base class, and a series of other classes inheriting from this: (Please excuse the over-used animal analogy) public abstract class Animal { } public class Dog : Animal { } public class Cat : Animal { } I then have a class that has a dependancy on an IEnumerable<Animal> public class AnimalFeeder { private readonly IEnumerable<Animal> _animals; public AnimalFeeder(IEnumerable<Animal> animals ) { _animals = animals; } } If I manually do something like this: var animals = typeof(Animal)

RegisterAssemblyTypes with UsingConstructor

只谈情不闲聊 提交于 2020-01-01 15:36:15
问题 I want to register assembly types which has multiple constructors Autowiring chooses the wrong constructor and want to specify it as I do in RegisterType builder.RegisterType(typeof(IController)) .UsingConstructor(typeof(IUnitOfWork)); But when I do this builder.RegisterAssemblyTypes(typeof(IController).Assembly) .UsingConstructor(typeof(IUnitOfWork)); I get "No matching constructor exists on type 'System.Object'." I think this is due to the fact that assembly type is a bit more complex than

RegisterAssemblyTypes with UsingConstructor

柔情痞子 提交于 2020-01-01 15:36:09
问题 I want to register assembly types which has multiple constructors Autowiring chooses the wrong constructor and want to specify it as I do in RegisterType builder.RegisterType(typeof(IController)) .UsingConstructor(typeof(IUnitOfWork)); But when I do this builder.RegisterAssemblyTypes(typeof(IController).Assembly) .UsingConstructor(typeof(IUnitOfWork)); I get "No matching constructor exists on type 'System.Object'." I think this is due to the fact that assembly type is a bit more complex than

Autofac named registration constructor injection

那年仲夏 提交于 2020-01-01 08:49:31
问题 Does Autofac support specifying the registration name in the components' constructors? Example: Ninject's NamedAttribute . 回答1: You need to use the Autofac.Extras.Attributed package on top to achieve this So let's say you have one interface and two classes: public interface IHello { string SayHello(); } public class EnglishHello : IHello { public string SayHello() { return "Hello"; } } public class FrenchHello : IHello { public string SayHello() { return "Bonjour"; } } Then you have a

Autofac PropertiesAutowired - Is it possible to ignore a one or more properties?

只谈情不闲聊 提交于 2020-01-01 07:15:27
问题 Despite the advice to pass dependencies through the constructor I've found that the development cost of having parameterless constructors and then autowiring all of the properties on everything is significantly less and makes the application much easier to develop out and maintain. However sometimes (on a view model for example) I have a property that is registered with the container, but that I don't want to populate at construction (for example the selected item bound to a container). Is

How do I implement a delegate factory?

ε祈祈猫儿з 提交于 2020-01-01 05:17:05
问题 The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories. It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? 回答1: Well I've never used Unity so far, so my answer is quite vague. The principal is simple

Can I use my Ninject .NET project within Orchard CMS?

為{幸葍}努か 提交于 2020-01-01 03:22:05
问题 I am creating a website using Orchard CMS and I have an external .NET project written with Ninject for dependency injection which I would like to use together with a module within Orchard CMS. I know that Orchard uses Autofac for dependency injection and this is causing me problems since I never worked with DI before. I have created an Autofac module, UserModule , which registers the a source, UserRegistrationSource , like this: UserModule.cs public class UserModule : Module { protected

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

本秂侑毒 提交于 2020-01-01 02:39:31
问题 I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where

OWIN + SignalR + Autofac

雨燕双飞 提交于 2019-12-31 09:47:10
问题 Taken from: http://docs.autofac.org/en/latest/integration/signalr.html: "A common error in OWIN integration is use of the GlobalHost. In OWIN you create the configuration from scratch. You should not reference GlobalHost anywhere when using the OWIN integration." That sounds reasonable. However, how should one resolve IHubContext from an ApiController, like the usual (non-OWIN): GlobalHost.ConnectionManager.GetHubContext<MyHub>() ? I can't find a reference on this one anywhere, and the only

使用AspectCore动态代理

南楼画角 提交于 2019-12-31 02:53:08
前言 最近越来越多的同学关注到 AspectCore ,并且提出不少中肯的建议,其中最多的提议是希望能够看到更多的关于AspectCore使用方式的文章和Demo。那么在这篇文章里,我们就来聊聊AspectCore核心之一的动态代理。 动态代理 在.NET平台中,静态织入和动态代理是实现AOP的两种方式。其中静态织入在编译时通过在MSBiuld执行自定义的Build Task来拦截编译过程,在生成的程序集里插入自己的IL。而动态代理则是在运行时通过Emit技术生成动态程序集和动态代理类型从而对目标方法进行拦截。不管那种方式,都需要对MSIL有足够的理解,如果读者想要学习MSIL相关的技术,我在这里推荐 《NET探秘MSIL权威指南》 一书。 AspectCore使用了动态代理作为AOP的实现,而不使用理论上性能更优的静态织入实现,是由于个人觉得动态代理方式可以做到更好的IoC进行集成并且能够在切面中获取更多的运行时元数据信息,并且在经过不断优化后,AspectCore动态代理的性能已经不落后静态织入的实现方式。 如何使用 在之前的文章里,只是简单的介绍了AspectCore,导致了很多同学不知道如何在项目里使用AspectCore( ^~^,求轻拍~ )。下面就来讲一下如何在IoC环境和非IoC环境里使用AspectCore的AOP。 首先通过nuget获取AspectCore: