我正在尝试使用 .NET 4.0 创建一个简单的 REST 服务,该服务使用 ninject 为服务注入任何依赖项。
这是我的服务目前的样子:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ReportService
{
private readonly ReportManager _reportManager;
public ReportService(ReportManager reportManager)
{
_reportManager = reportManager;
}
[WebInvoke(UriTemplate = "GetReports", Method = "GET")]
public List<ReportDTO> GetReports()
{
var reports = _reportManager.GetReports();
return reports.ToList();
}
}
这是我的 global.asax:
public class Global : NinjectHttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("ReportService", new NinjectWebServiceHostFactory(), typeof(ReportService)));
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
}
还有我的服务模块:
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IFRSDbContext>().To<FRSDbContext>().InRequestScope();
Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();
Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
Bind<IReportRepository>().To<ReportRepository>();
Bind<ReportManager>().ToSelf();
Bind<ReportService>().ToSelf();
}
}
当我尝试运行我的服务时,我不断收到以下异常:
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +222
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253
[HttpException (0x80004005): Object reference not set to an instance of an object.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9079228
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256
编辑
我已经更新到我的服务的最新版本。我从这里使用 Ninject 2.3 和 Ninject.Extensions.Wcf 2.3,但仍然没有运气..我做错了什么?我遵循了 WcfTimeService 示例中的所有内容,除了我使用的是 REST 服务而不是 .svc Wcf 服务......