依赖注入:SimpleTraceWriter类型必须从ITraceWriter派生

时间:2015-07-07 09:09:29

标签: c# dependency-injection ninject asp.net-web-api2

我使用Ninject进行DI。其中一行给了我一个奇怪的错误:

The type SimpleTraceWriter must derive from ITraceWriter.
Parameter name: service

{"The type SimpleTraceWriter must derive from ITraceWriter.\r\nParameter name: service"}

给出错误的代码行:

config.Services.Replace(typeof(ITraceWriter), new SimpleTraceWriter(WebContainerManager.Get<ILogManager>()));

但是SimpleTraceWriter实现了ITraceWriter:

SimpleTraceWriter代码:

 public class SimpleTraceWriter : ITraceWriter
    {
        private readonly ILog _log;

        public SimpleTraceWriter(ILogManager logManager)
        {
            _log = logManager.GetLog(typeof(SimpleTraceWriter));
        }

        public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
        {
            var rec = new TraceRecord(request, category, level);
            traceAction(rec); WriteTrace(rec);
        }

...

ITraceWriter代码:

public interface ITraceWriter
{   
    void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction);
}

1 个答案:

答案 0 :(得分:4)

System.Web.Http.Tracing命名空间中还有另一个ITraceWriter接口。尝试在行中指定名称空间,这会导致错误:

config.Services.Replace(typeof(MyNamespace.ITraceWriter), new SimpleTraceWriter(WebContainerManager.Get<ILogManager>()));