【问题标题】:Ninject WCF ErrorNinject WCF 错误
【发布时间】:2011-09-26 14:22:37
【问题描述】:

好的,在过去两天追查此错误后,我束手无策:

Error activating IUserIssueRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IUserIssueRepository into parameter userIssueRepository of constructor of type IssueTrackerService
1) Request for IssueTrackerService

Suggestions:
1) Ensure that you have defined a binding for IUserIssueRepository.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.

我知道我的设置是正确的(绑定完好,等等)。以下是相关的 wcf 服务内容。是的,当然,程序集引用也是完整的。

Global.asax.cs 使用忍者; 使用 Ninject.Extensions.Wcf;

namespace NextGenIT.Web.Wcf
{
    public class Global : NinjectWcfApplication
    {
        protected override IKernel CreateKernel()
        {
            return new StandardKernel(new ServiceModule());
        }
    }
}

ServiceModule.cs

using Ninject.Modules;
using NextGenIT.Core.Domain;

namespace NextGenIT.Web.Wcf
{
    public class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            this.Bind<IUserIssueRepository>()
                .To<SqlUserIssueRepository>();
        }
    }
}

IssueTrackerService.svc

<%@
ServiceHost
Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"
Service="NextGenIT.Core.Services.IssueTrackerService"
%>

编辑 1: 整个堆栈跟踪:

    [FaultException`1: Error activating IUserIssueRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
  2) Injection of dependency IUserIssueRepository into parameter userIssueRepository of constructor of type IssueTrackerService
  1) Request for IssueTrackerService

Suggestions:
  1) Ensure that you have defined a binding for IUserIssueRepository.
  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3) Ensure you have not accidentally created more than one kernel.
  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
  5) If you are using automatic module loading, ensure the search path and filters are correct.
]
   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +4729827
   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +1725
   NextGenIT.Services.Contracts.IIssueTrackerService.GetUserIssues(String userId) +0
   NextGenIT.Services.Client.IssueTrackerClient.GetUserIssues(String userId) in D:\Projects\TFS\NextGenIssueTracker\branches\2011Updates\NextGenIT.Services.Client\Proxies\IssueTrackerClient.cs:46
   NextGenIT.Tests.Integration.WebClient._default.Page_Load(Object sender, EventArgs e) in D:\Projects\TFS\NextGenIssueTracker\branches\2011Updates\NextGenIT.Tests.Integration.WebClient\default.aspx.cs:26
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

编辑 2:接口和实现

IUserIssueRepository.cs

using System.Collections.Generic;
using NextGenIT.Services.Contracts;

namespace NextGenIT.Core.Domain
{
    public interface IUserIssueRepository
    {
        string CreateUser(IUser user);
        int CreateIssue(IIssue issue);
        int CreateComment(IComment comment);
        IUser GetUser(string userId);
        IIssue GetIssue(int issueId);
        IEnumerable<IIssue> GetIssues(string userId);
        IEnumerable<IComment> GetComments(string userId, int issueId);
    }
}

SqlUserIssueRepository.cs

using System.Collections.Generic;
using NextGenIT.Services.Contracts;

namespace NextGenIT.Core.Domain
{
    public class SqlUserIssueRepository : IUserIssueRepository
    {
        public string CreateUser(IUser user) { return "1234"; }
        public int CreateIssue(IIssue issue) { return 1; }
        public int CreateComment(IComment comment) { return 1; }
        public IUser GetUser(string userId) { return null; }
        public IIssue GetIssue(int issueId) { return null; }
        public IEnumerable<IIssue> GetIssues(string userId) { return null; }
        public IEnumerable<IComment> GetComments(string userId, int issueId) { return null; }
    }
}

【问题讨论】:

  • 两件事:1)当你放一个断点 int Load 时,它会触发吗? 2) 我们能看到整个堆栈跟踪吗?
  • Errrr,实际上我不确定它是否调用过......如果不是,可能是什么原因?

标签: .net wcf dependency-injection inversion-of-control ninject


【解决方案1】:

更新:

在某些时候,我已将托管 WCF 服务的项目重命名。我从没想过要检查 global.asax 的标记。

确实,它是从一个不再存在的文件继承而来的。项目编译没问题,这就是为什么我从来没想过在那里检查!

【讨论】:

    【解决方案2】:

    这可能是因为 SqlUserIssueRepository 没有实现 IUserIssueRepository。

    Ninject 正在尝试将类绑定到接口。

    【讨论】:

    • 不,它肯定实现了 IUserIssueRepository
    • 可以发一下界面吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2015-11-23
    • 2012-02-18
    相关资源
    最近更新 更多