相关疑难解决方法(0)

如何在AutoFac中使用Property Injection?

在Console应用程序中,我正在使用Log4Net,在Main方法中我得到了logger对象.现在,我想通过让所有类继承自具有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<ILog>();

           // tried below but didn't inject ILog object into the Product
           Container.Resolve<Product>();

           RunTest();

            Console.ReadLine();
        }

        private static void RunTest()
        {
            var product = new Product();
            product.Do();
        }

        private static void InitializeAutoFac()
        {
            var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net log4net inversion-of-control autofac

27
推荐指数
3
解决办法
3万
查看次数

标签 统计

asp.net ×1

autofac ×1

c# ×1

inversion-of-control ×1

log4net ×1