Tag: autofac

如何根据传递到的服务来解析接口

我有一个界面。 public interface ISomeInterface {…} 和两个实现(SomeImpl1和SomeImpl2): public class SomeImpl1 : ISomeInterface {…} public class SomeImpl2 : ISomeInterface {…} 我也有两个服务,我注入ISomeInterface(通过contructor): public class Service1 : IService1 { public Service1(ISomeInterface someInterface) { } … } 和 public class Service2 : IService2 { public Service2(ISomeInterface someInterface) { } … } 我正在使用Autofac作为我的IoC工具。 这个问题。 如何配置Autofac注册,以便SomeImpl1自动注入Service1,SomeImpl2将自动注入Service2。 谢谢!

DI的一个接口的多个实现

现在我正在尝试使用Autofac的IOC容器自学习dependency injection模式。 我想出了一个非常简单的例子,如下所示。 虽然示例很简单,但我无法正常工作。 这是我的类/接口: 两个怪物,都实现了IMonster接口: interface IMonster { void IntroduceYourself(); } class Vampire : IMonster { public delegate Vampire Factory(int age); int mAge; public Vampire(int age) { mAge = age; } public void IntroduceYourself() { Console.WriteLine(“Hi, I’m a ” + mAge + ” years old vampire!”); } } class Zombie : IMonster { public […]

Autofac:注册组件并根据解析父级进行解析

我想要注册一个组件来解析基于它可能解析的类的参数。 (这听起来有点令人困惑,所以我会举一个例子)。 这是一个使用记录器的对象: class MyObject : IMyObject { public ILogger Logger; public MyObject(ILogger logger) { Logger = logger; } } 现在,COULD中传递的记录器在类与类之间是不同的。 所以我有一个相当修补的想法,如何在下面这样做: class MyLogger : ILogger { public string Name{get; protected set;} public static ILogger GetLogger(string className) { Name = className; MyLogger logger; // Do something to choose a logger for that specific class return […]

使用Autofac将依赖项注入控制台应用程序中的Main入口点

说我有一个简单的控制台应用程序: public static class Program { private static ILog Log { get; set; } public static void Main() { Log.Write(“Hello, world!”); } } 我可以使用Autofac注入Log实例并确保Log属性在运行时不为null的最简单方法是什么? 问题是我不能通过Main()传递它,我试图避免使用容器的服务位置(只是因为)。

Autofac – 无法创建请求生命周期范围,因为HttpContext不可用 – 由于异步代码?

简短问题: 与此未解决的问题相同 长问题: 我刚刚从MVC 4 + Web Api解决方案中移植了一些代码,该解决方案使用Autofac进入我的新解决方案,该解决方案也使用Autofac但仅使用Web Api 2(没有MVC 5.1项目,只有web api)。 在我以前的解决方案中,我有MVC4和Web Api所以我有2个Bootstrapper.cs文件,每个文件一个。 我复制了新项目的Web Api引导程序。 现在我在新解决方案中有2个其他需要依赖项目的项目。 让我们假设我必须使用DependencyResolver.Current.GetService()尽管它是一个反模式。 起初,在我将MVC依赖关系解析器设置为同一容器之前,这是行不通的: GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); //I had to pull in Autofac.Mvc and Mvc 5.1 integration but this line fixed it DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 奇怪的是,这样做只会修复其中一个项目! 这是情况: Solution.Web project Bootstrapper.cs that registers both dependency resolvers for web api and mvc. […]

如何解决Autofac InstancePerHttpRequest

我在Global.asax.cs中注册了这样的组件: ContainerBuilder builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType().As().InstancePerHttpRequest(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // This is where my error happens, not sure why? var workContext = container.Resolve(); WebWorkContext类: public class WebWorkContext : IWorkContext { // Left out other code } IWorkContext接口: public interface IWorkContext { // Left out other code } 我得到的错误是: 从请求实例的作用域中看不到具有匹配“httpRequest”的标记的作用域。 这通常表示注册为每HTTP请求的组件正被SingleInstance()组件(或类似场景)重新请求。在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖,永远不会从容器本身请求。 […]

如何在AutoFac中使用Property Injection?

在Console应用程序中,我正在使用Log4Net,在Main方法中我得到了logger对象。 现在,我想通过让所有类inheritance自具有ILog属性并且应该由Property Injection而不是Constructor Injection设置的BaseClass来使所有类中的日志对象可用。 我正在使用AutoFac IoC容器,如何将我的日志对象注入我的每个类的Log属性? 实现这一目标的最佳/最简单方法是什么? 有没有办法自动解决类型? 以下是我的测试应用程序: namespace ConsoleApplication1 { class Program { static ILog Log; static IContainer Container; static void Main(string[] args) { InitializeLogger(); InitializeAutoFac(); // the below works but could it be done automatically (without specifying the name of each class)? Product.Log = Container.Resolve(); // tried below but didn’t inject ILog […]