如何的WebAPI 2 Spring.Net整合

编程入门 行业动态 更新时间:2024-10-27 16:36:05
本文介绍了如何的WebAPI 2 Spring.Net整合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试着为的WebAPI 2 是 Spring.Net 集成了没有成功。这是我曾尝试:

Im trying to integrate WebApi 2 with Spring.Net with not success. This is what i have tried:

public class MvcApplication : SpringMvcApplication { protected void Application_Start() { XmlConfigurator.Configure(); AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } }

在创建该 SpringWebApiDependencyResolver 例如,我得到这个异​​常:

When is creating the SpringWebApiDependencyResolver instance, i'm getting this exception:

错误创建上下文spring.root':请求不可用在这种情况下

public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.DependencyResolver = new SpringWebApiDependencyResolver(ContextRegistry.GetContext()); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }

即时通讯使用Spring.Net 2.0.1

Im using Spring.Net 2.0.1

推荐答案

现在正在工作,并不需要以下行:

Now is working, the following line was not needed:

config.DependencyResolver = new SpringWebApiDependencyResolver(ContextRegistry.GetContext());

这是基于Spring.Net 例如的:

This is my implementation based on Spring.Net example:

public class MvcApplication : SpringMvcApplication { protected void Application_Start() { // More config... GlobalConfiguration.Configure(WebApiConfig.Register); // More config... } protected override System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver() { var resolver = base.BuildWebApiDependencyResolver(); var springResolver = resolver as SpringWebApiDependencyResolver; if (springResolver != null) { var resource = new AssemblyResource( "assembly://assemblyName/namespace/ChildControllers.xml"); springResolver.AddChildApplicationContextConfigurationResource(resource); } return resolver; } }

我prefered保持共同的WebAPI配置:

I prefered to keep the common WebApi configuration:

public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }

有关在IoC容器控制器的注册是一样的MVC集成:

For registration of controllers in the IoC container is the same as MVC integration:

<object type="namespace.PrefixController, assemblyName" singleton="false" > <property name="Dependency1" ref="Dependency1" /> </object>

更多推荐

如何的WebAPI 2 Spring.Net整合

本文发布于:2023-06-04 22:09:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WebAPI   Spring   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!