asp.net-web-api – 仅在第一个Web请求(WebAPI 2,OWIN 3,Ninject
发布时间:2020-12-15 18:48:02 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试创建一个使用OWIN中间件的“准系统”Web API项目,Ninject Depencency注入,并最终托管在IIS中。我遵循一篇文章“ASP.NET Web.API2,OWIN和Ninject”,这里: http://www.alexzaitzev.pro/2014/11/webapi2-owin-and-ninject.html,除了我使用一个
我正在尝试创建一个使用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:schemas-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-mvc – 多个DateTime参数的属性路由
- asp.net-mvc – mvc asp.net中的自定义成员资格提供程序或配
- asp.net – Crystal Report Viewer按钮在Google Chrome和Fi
- 如何使用ASP.NET Core创建多部分HTTP响应
- asp.net – 将特定文件夹映射到web.config中的HttpHandler
- viewbag
- asp.net – K运行时使用的.Net完整框架和.Net Core Framewo
- asp.net-mvc – MVC4异步和并行执行
- 具有依赖关系的并行操作执行
- ASP.net缓存访问导致foreach循环中的Collection Modified异
推荐文章
站长推荐
热点阅读