如何的WebAPI 2 Spring.Net整合 [英] How to integrate WebApi 2 with Spring.Net

查看:769
本文介绍了如何的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 <我的一个实施href=\"https://github.com/spring-projects/spring-net/tree/master/examples/Spring/Spring.Mvc5QuickStart/Spring.Mvc5QuickStart\"相对=nofollow>例如的:

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整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆