【发布时间】:2015-05-02 17:02:28
【问题描述】:
我在 Asp.Net MVC 项目中使用 Ninject 已经有一段时间了,而且工作起来非常有魅力。但是现在,当我想通过安装 Ninject.Extensions.Wcf 和 Ninject.Web.Common 而不是 Ninject.MVC3 将其与 WCF 服务库一起使用时,添加服务引用时出现以下错误。
System.InvalidOperationException: The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.
at System.ServiceModel.Dispatcher.InstanceBehavior..ctor(DispatchRuntime dispatch, ImmutableDispatchRuntime immutableRuntime)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime..ctor(DispatchRuntime dispatch)
at System.ServiceModel.Dispatcher.DispatchRuntime.GetRuntimeCore()
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpened()
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
这是我的 WCF 类构造函数 -
private TestDBContext _dbContext;
public TestService(TestDBContext ctx)
{
_dbContext = ctx;
}
这是我在 NinjectWebCommon 中添加依赖项的方法 -
kernel.Bind<TestDBContext>().To<TestDBContext>().InRequestScope();
这里还需要什么?如何解决?
谢谢
编辑:整个 NinjectWebCommon
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<TestDBContext>().To<TestDBContext>().InRequestScope();
}
App.config
【问题讨论】:
-
这看起来很简单,但仍然没有描述 WCF 服务是如何引导的,例如 *.svc 文件和/或 web.config。
-
它是一个 wcf 库,所以不包含 .svc。它是使用 App.config 引导的。让我知道您的调试需要其中的哪一部分。
-
好。你能把 app.config 添加到这里吗?我认为这是工厂的问题。
-
这对github.com/ninject/Ninject.Extensions.Wcf/wiki/…有什么帮助吗?想要在跳转之前确认。
-
添加这个,我最好的猜测是它应该可以工作。我将更新我的答案以澄清一下。
标签: c# asp.net-mvc wcf ninject ninject-extensions