Ninject无法在Web Api 2中创建控制器错误

时间:2015-01-08 16:32:32

标签: c# ninject asp.net-web-api2

我正在尝试在我的WebApi中使用Ninject作为我的DI容器。

我使用了以下代码,这些代码是我在SO here中从同一个问题中找到的,但它又给了我同样的错误

  

尝试创建类型的控制器时发生错误   ' SendOrderToInspectorController&#39 ;.确保控制器有一个   无参数公共

     

的InnerException:类型   ' SampleAspNetWebApi.Controllers.SendOrderToInspectorController'不   没有默认构造函数

namespace SampleAspNetWebApi
    {
        public class Startup
        {

            private readonly Lazy<IKernel> _kernel = new Lazy<IKernel>(() =>
            {
                var kernel = new StandardKernel();

                kernel.Load(Assembly.GetExecutingAssembly());

                // here for brevity, move this to a RegisterServices or similar method,
                // 
                kernel.Bind<ISaveOrderForInspector>().To<SaveOrderForInspector>();
                return kernel;
            });


public void Configuration(IAppBuilder app)
            {  

                app.UseNinjectMiddleware(() => _kernel.Value);
                JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

                app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                    {
                        Authority = "https://localhost:44333/core",
                        RequiredScopes = new[] { "write", "read" }
                    });

                app.UseResourceAuthorization(new AuthorizationManager());
                app.UseWebApi(WebApiConfig.Register());
            }
        }
    }

这是我的控制者:

   public class SendOrderToInspectorController : ApiController
    {
        public ISaveOrderForInspector SaveOrderForInspector { get; set; }


        public SendOrderToInspectorController(ISaveOrderForInspector saveOrderForInspector)
        {
            SaveOrderForInspector = saveOrderForInspector;
        }
}

更新

这也是我的SaveOrderForInspector类:

public class SaveOrderForInspector : ISaveOrderForInspector
    {
        public InspectorOrder InspectorOrder
        {
            get { return _inspectorOrder; }
            set { _inspectorOrder = value; }
        }

        public Inspector Inspector
        {
            get { return _inspector; }
            set { _inspector = value; }
        }

        private readonly FairShareContext _ctx = new FairShareContext();
        private InspectorOrder _inspectorOrder=new InspectorOrder();
        private Inspector _inspector=new Inspector();

        public SaveOrderForInspector()
        {

        }}

0 个答案:

没有答案