本文介绍了如何的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整合
发布评论