asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-web-api – 仅在第一个Web请求(WebAPI 2,OWIN 3,Ninject 3)上抛出Ninject.ActivationException大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个使用OWIN中间件的“准系统”Web API项目,Ninject Depencency注入,并最终托管在IIS中。我遵循一篇文章“ASP.NET Web.API2,OWIN和Ninject”,这里: http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html,除了我使用一个空的Web应用程序项目,并没有勾选“包括Web API参和文件夹”的文章创建项目。

当我启动新的API并提出一个页面请求时,我得到以下Ninject.ActivationException抛出:

当我刷新浏览器时,这个例外消失了。我真的不明白是什么导致这个例外。从我的角度来看,在OWIN上下文和Ninject中,WebAPI几乎没有经验,很难知道问题所在。是否与Ninject …?是在OWIN吗?是否在WebAPI …?

这是我的Startup.cs文件的内容:

using System.Web.http;
using LHD.API_2;
using Microsoft.owin;
using Microsoft.owin.Security.oAuth;
using Ninject.Web.Common.owinHost;
using Ninject.Web.WebApi.owinHost;
using Owin;

[assembly: OwinStartup(typeof(Startup))]

namespace LHD.API_2
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            httpConfiguration config = new httpConfiguration();

            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationTypE));

            // Web API routes
            config.MaphttpAttributeRoutes();
            config.Routes.MaphttpRoute("API Default","api/1/{Controller}/{action}/{iD}",new { id = RouteParameter.optional });

            app.UseNinjectMiddleware(() => NinjectConfig.CreateKernel.value);
            app.UseNinjectWebApi(config);
        }
    }
}

这是我的NinjectConfig.cs文件的内容:

using System;
using System.Reflection;
using Ninject;

namespace LHD.API_2
{
    public static class NinjectConfig
    {
        public static Lazy<IKernel> CreateKernel = new Lazy<IKernel>(() =>
        {
            StandardKernel kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecuTingAssembly());

            Registerservices(kernel);

            return kernel;
        });

        private static void Registerservices(KernelBase kernel)
        {
            // TODO - put in registrations here...

            //kernel.bind<IFakeservice>().To<Fakeservice>();
        }
    }
}

这是我的packages.config的内容:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.owin" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net451" />
  <package id="Microsoft.owin" version="3.0.1" targetFramework="net451" />
  <package id="Microsoft.owin.Host.SystemWeb" version="3.0.1" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net451" />
  <package id="Ninject" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Extensions.ContextPreservation" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Extensions.NamedScope" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.Common.owinHost" version="3.2.3.0" targetFramework="net451" />
  <package id="Ninject.Web.WebApi" version="3.2.0.0" targetFramework="net451" />
  <package id="Ninject.Web.WebApi.owinHost" version="3.2.4.0" targetFramework="net451" />
  <package id="Owin" version="1.0" targetFramework="net451" />
</packages>

…为了完整起见,我的web.config的内容同样是不明确的:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application,please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
    </system.web>

    <runtime>
        <assemblyBinding xmlns="urn:scheR_338_11845@as-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.http.owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Net.http.FormatTing" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferrequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

任何人都可以指出我在做错什么,或者更重要的是,我可以改变什么来使事情工作,而不会被抛出异常?

解决方法

您使用的是什么版本的Ninject.Web.WebApi库?看起来像是在两周之前,改变了一些事情。在此更改之前(v3.2.3及更早版本),httpConfiguration绑定存在于Ninject.Web.WebApi.owinHost.owinWebApiModule和Ninject.Web.WebApi.WebApiModule中的两个不同模块中。此更改后(新版本为3.2.4),此绑定只发生一次。

大佬总结

以上是大佬教程为你收集整理的asp.net-web-api – 仅在第一个Web请求(WebAPI 2,OWIN 3,Ninject 3)上抛出Ninject.ActivationException全部内容,希望文章能够帮你解决asp.net-web-api – 仅在第一个Web请求(WebAPI 2,OWIN 3,Ninject 3)上抛出Ninject.ActivationException所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。