我的项目中有一个Web服务,用于将json数据返回到ajax调用。
注入我的ClientService在常规页面上运行正常,但如何让它在Web服务中运行?
NinjectWebCommon.cs:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IClientService>().To<ClientService>();
}
Default.aspx.cs:有效!
public partial class _Default : System.Web.UI.Page
{
[Inject]
public IClientService clientService { get; set; }
MyWebservice.asmx:NullReferenceException(clientService为null)
public class MyWebService: System.Web.Services.WebService
{
[Inject]
public IClientService clientService { get; set; }
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public MyObject GetClients(int id)
{
var list = clientService.GetClients(id);
答案 0 :(得分:6)
我已经解决了这个问题。我将MyWebService类改为派生自Ninject.Web.WebServiceBase
:
public class MyWebService: WebServiceBase