我有一个 MVC3 应用程序,它应该通过 WCF 公开一些功能。其中一些函数具有依赖关系,我想使用 Ninject 注入它们,但我必须在这里遗漏一些明显的东西,因为这不起作用。谁能给个提示?这是我所做的:
1) 在我的 MVC3 应用程序中,我通过 nuget 添加了Ninject Wcf 扩展。
2)我添加一个.svc 文件,如:
<%@ ServiceHost Service="MyWeb.LookupService"
Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"%>
3)我添加了相应的代码(这只是一个测试,所以我在这里使用了一个虚拟接口):
[ServiceContract]
public interface ILookupService
{
[OperationContract]
int GetProjectIdByCode(string sCode);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LookupService : ILookupService
{
private readonly ISomeRepository _repository;
public LookupService(ISomeRepository repository)
{
_repository = repository;
}
public int GetProjectIdByCode(string sCode)
{
//... use _repository
}
}
4) 在 App_Start/ NinjectMVC3.cs 中:
private static void RegisterServices(IKernel kernel)
{
string sConnection = ConfigurationManager.ConnectionStrings["SomeEntities"].ConnectionString;
// WCF
kernel.Bind<ServiceHost>().To<NinjectServiceHost>();
kernel.Bind<ISomeRepository>()
.To<AConcreteRepository>()
.WithConstructorArgument("sConnection", sConnection);
}
现在,当我访问 svc 时,我从 Ninject 得到以下异常:
System.ArgumentNullException 未被用户代码处理
Message=Cannot null 参数名称:root Source=Ninject
ParamName=root StackTrace:在 C:\Projects\Ninject\Maintenance2 中的 Ninject.Infrastructure.Ensure.ArgumentNotNull(对象参数,字符串名称)。 2\ninject\src\Ninject\Infrastructure\Ensure.cs:第 20 行 Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot 根,类型服务,Func2 constraint, IEnumerable
1 个参数,Boolean isOptional,Boolean isUnique) 在 c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 258 at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[ ] 参数)在 c:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37 at Ninject.Extensions.Wcf.NinjectServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) in c :\Projects\Ninject\Maintenance2.2\ninject.extensions.wcf\src\Ninject.Extensions.Wcf\NinjectServiceHostFactory.cs:System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) 处的第 48 行System.ServiceModel.ServiceHostingEnvironment.HostingManager。CreateService(String normalizedVirtualPath) 在 System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(String normalizedVirtualPath) 在 System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)