Ninject web api - System.TypeLoadException - PullRequest
1 голос
/ 18 марта 2020

Я получил эту ошибку при использовании Ninject с Web API.

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        try
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel1);
        }
        catch (Exception ex)
        {
            WriteLog(ex.Message +"-------------------------------"+ ex.Source +"---" +ex);
            throw;
        }
    }

    /// <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 CreateKernel1()
    {
        WriteLog("CreateKernel1 -- 5");
        var kernel = new StandardKernel();
        try
        {
            WriteLog("CreateKernel1 -- 6");
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            NinjectServiceRegister ser = new NinjectServiceRegister(kernel);
            ser.RegisterServices();
            AutoMapperConfig autoMapperConfig = new AutoMapperConfig(kernel);
            autoMapperConfig.RegisterServices();
            WriteLog("CreateKernel1 -- 7");
            kernel.BindHttpFilter<ApiAuthneticatonFilter>(System.Web.Http.Filters.FilterScope.Global).WhenControllerHas<CollectorsAuthenticationAttribute>();
            kernel.BindHttpFilter<CollectorsAuthorizationFilter>(System.Web.Http.Filters.FilterScope.Global).WhenControllerHas<CollectorsAuthroizationAttribute>();
            WriteLog("CreateKernel1 -- 8");
            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);
            return kernel;
        }
        catch(Exception ex)
        {
            WriteLog("Authenticate -- 9" + ex.Message);
            kernel.Dispose();
            throw;
        }
    }

сообщение об ошибке:

-CollectorsApp.MobileWebAPI --- System.TypeLoadException: метод 'ExecuteAuthorizationFilterAsyn c' в типе 'CollectorsApp. MobileWebAPI.InfraStructure.Authorization.CollectorsAuthorizationFilter 'из сборки' CollectorsApp.MobileWebAPI, версия = 1.0.0.0, Culture = нейтральный, PublicKeyToken = null 'не имеет реализации. в CollectorsApp.MobileWebAPI.App_Start.NinjectWebCommon.CreateKernel1 () в Ninject.Web.Common.Bootstrapper.Initialize (Func`1 createKernelCallback) в CollectorsApp.MobileWebAPI.App_Start.Nive.InW: \ IGWW InvogateWeb \ CollectorsApp.MobileWebAPI \ App_Start \ NinjectWebCommon.cs: строка 34

...