web API .net - .net core 对比学习-依赖注入
今天我们来看一下 .net web api 和 .net core web api依赖注入机制的差异。
首先我们分别在.net web api 和 .net core web api新建文件夹Service并增加IValueService 接口和它的实现类ValueService.
然后我们在ValueController 里面分别添加Service 字段,并加上新的构造方法:
.net web api controller的代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using FreWebApi.Service; using System.Collections.Generic; using System.Web.Http; namespace FreWebApi.Controllers { public class ValuesController : ApiController { private IValueService valueService; public ValuesController(IValueService valueService) { this.valueService = valueService; } // GET api/values public IEnumerable<string> Get() { return valueService.GetValues(); } } }
.net core web api controller的代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System.Collections.Generic; using CoreWebApi.services; using Microsoft.AspNetCore.Mvc; namespace CoreWebApi.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private IValueService valueService; public ValuesController(IValueService valueService) { this.valueService = valueService; } // GET api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { return valueService.GetValues(); } } }
看到这里我们并没有看到有什么不同,下面我们将为controller里面的service 添加依赖注入。
在.net core中我们在Startup 类的ConfigureServices service方法里面添加依赖注入的配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void ConfigureServices(IServiceCollection services) { services.AddMvc( options => { options.Filters.Add(typeof(HandleExceptionAttribute), 1); // an instance } ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddTransient(typeof(IValueService), typeof(ValueService)); //services.AddTransient<IValueService>(); //services.AddTransient(typeof(ValueService)); //services.AddTransient<IValueService, ValueService>(); //services.AddTransient(typeof(IValueService), service => new ValueService()); }
对于 .net 我们使用autoFac 做依赖注入,首先我们打开项目的Nuget管理,添加autoFac的包:
然后我们在App_Start文件夹下添加类AutoFacConfig:
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Reflection; using System.Web.Http; using Autofac; using Autofac.Integration.WebApi; using FreWebApi.Service; namespace FreWebApi { public class AutoFacConfig { public static void Run() { SetAutoFacConfig(); } private static void SetAutoFacConfig() { ContainerBuilder builder = new ContainerBuilder(); HttpConfiguration config = GlobalConfiguration.Configuration; // Register API controllers using assembly scanning. builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<ValueService>().As<IValueService>() .SingleInstance(); var container = builder.Build(); // Set the WebApi dependency resolver. config.DependencyResolver = new AutofacWebApiDependencyResolver(container); } } }
这个时候我们的,net web的Api的配置也就完成了。
本文旨在比较.net web api 和 .net core web api 在依赖注入配置和写法上的差异,所以没有涉及到依赖注入对象生命周期的研究,有兴趣的同学可以查看其他原因的帖子加以了解。