Unity: The type LogWriter cannot be constructed

后端 未结 5 624
时光取名叫无心
时光取名叫无心 2020-12-20 20:06

For the following \"project\" I am getting a very annoying and inexplicable error when resolving Unity for DI.

InvalidOperationException - The type Lo

相关标签:
5条回答
  • 2020-12-20 20:49

    Activation error occured while trying to get instance of type LogWriter, key ""

    check your config file it must have correct default 'loggingConfiguration' section

    0 讨论(0)
  • 2020-12-20 20:50

    Try this:

    protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                try
                {
                    IUnityContainer container = new UnityContainer();
                    container.RegisterType<ILogWriter, LogWriter>();
                    container.RegisterType<ExceptionManager>();
                    container.RegisterType<Performance>(new InjectionConstructor(typeof(ILogWriter), typeof(ExceptionManager));                    
                    Performance p = container.Resolve<Performance>();
                }
                catch (Exception ex)
                {
    
                }
            }
    
    0 讨论(0)
  • 2020-12-20 20:50

    LogWriter does not have an empty constructor or a constructor with all concrete types parameters: LogWriter Constructor.

    So Unity fails in building it and as it says, it will need your help by configuring the container to provide an implementation.

    As a confirmation, ExceptionManager will probably be resolved ok, since it has only one constructor, parameterless as well :)

    0 讨论(0)
  • 2020-12-20 20:53

    In order for Unity to construct your Performance class, it needs to know how to construct the implementation of ILogWriter.

    I cannot see anywhere in your code where you tell Unity what class to create for the ILogWriter interface, so I suspect you may need to add this.

    0 讨论(0)
  • 2020-12-20 21:10

    You need to add the Enterprise Library extension to your container. Without it, the container doesn't read the config file and therefore doesn't know how to create Entlib objects, like the LogWriter.

    0 讨论(0)
提交回复
热议问题