在以前的文章中,我们给出了在现在的系统中使用ABP模块的方法。对于新的使用ABP开发的系统,我们需要在新的系统中也可以访问现有的Web Api,我们希望将现有的Web Api部署到ABP的环境中,这样可以避免跨域访问,安全认证等问题。实现起来不是很复杂,只要如下两步就可以了:
- 在ABP系统的Global.asax中增加注册现有web api的代码
- 将现有web api的相关动态库拷贝到ABP的bin目录下
相关示例代码如下:
using System.Threading;
using Abp.Castle.Logging.Log4Net;
using Abp.Web;
using Castle.Facilities.Logging;
namespace jiagoushi_cn.MyPlat.Web
{
public class MvcApplication : AbpWebApplication<MyPlatWebModule>
{
protected override void Application_Start(object sender, EventArgs e)
{
AbpBootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseAbpLog4Net().WithConfig(Server.MapPath("log4net.config"))
);
var tp = Type.GetType(string.Concat("WebApplication1.Controllers.PlatBackController", ", ", "WebApplication1"));
AbpBootstrapper.IocManager.Register(tp, Abp.Dependency.DependencyLifeStyle.Transient);
base.Application_Start(sender, e);
}
}
}
这里我们通过Web Api控制器的名称和程序集的名称动态获取需要的类型,然后使用AbpBootstrapper.IocManager.Register进行注册,请注意,注册的类型必须是Abp.Dependency.DependencyLifeStyle.Transient。
这样在ABP系统中就集成了遗留系统的Web Api。