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的代码如下:
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的代码如下:
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方法里面添加依赖注入的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(
options =>
{
options.Filters.Add(typeof(HandleExceptionAttribute), ); // 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:
代码如下:
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 在依赖注入配置和写法上的差异,所以没有涉及到依赖注入对象生命周期的研究,有兴趣的同学可以查看其他原因的帖子加以了解。
web API .net - .net core 对比学习-依赖注入的更多相关文章
- web API .net - .net core 对比学习-使用Swagger
根据前两篇的介绍,我们知道.net web api 和 .net core web api在配置方面的不同如下: 1. .net web api的配置是在 App_Stat文件夹里面添加对应的配置类, ...
- web API .net - .net core 对比学习-文件目录概述
个人正在学习.net web Api的相关知识,因此用这一系列博客做一记录. 1. 首先我们分别创建 .net web api 项目和 .net core web api 项目. 2. 我们首先比较一 ...
- ASP.NET Web API 中的返回数据格式以及依赖注入
本篇涉及ASP.NET Web API中的返回数据合适和依赖注入. 获取数据 public IEnumerable<Food> Get() { var results = reop.Get ...
- Net core学习系列(三)——Net Core中的依赖注入
本文通过一个维修工与工具库的例子形象的描述一下为什么要用依赖注入.它的工作原理是什么样的, 然后根据这个类比一下ASP.NET Core 中的依赖注入, 从而深刻了解它的使用方法.注意事项以及回收机制 ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
- 深入理解net core中的依赖注入、Singleton、Scoped、Transient(一)
相关文章: 深入理解net core中的依赖注入.Singleton.Scoped.Transient(一) 深入理解net core中的依赖注入.Singleton.Scoped.Transient ...
- ASPNETCOREAPI 跨域处理 SQL 语句拼接 多条件分页查询 ASPNET CORE 核心 通过依赖注入(注入服务)
ASPNETCOREAPI 跨域处理 AspNetCoreApi 跨域处理 如果咱们有处理过MV5 跨域问题这个问题也不大. (1)为什么会出现跨域问题: 浏览器安全限制了前端脚本跨站点的访问资源, ...
- ASP.NET Core 中的 依赖注入介绍
ASP.NET Core 依赖注入 HomeController public class HomeController : Controller { private IStudentReposito ...
- ASP.NET Core中的依赖注入(5): ServiceProvider实现揭秘 【总体设计 】
本系列前面的文章我们主要以编程的角度对ASP.NET Core的依赖注入系统进行了详细的介绍,如果读者朋友们对这些内容具有深刻的理解,我相信你们已经可以正确是使用这些与依赖注入相关的API了.如果你还 ...
随机推荐
- 证书转化 .cer .crt .jks
cer格式——>JKS (keytool 为java JDK自带的,可以在bin目录下找到) keytool -import -alias mycert -file d:\def.cer -ke ...
- intellij ide调用一个对象所有的set方法
1.下载地址:https://github.com/yoke233/genSets/releases/download/1.1/genSets.jar 2.plugin 从本地磁盘安装找到jar,并重 ...
- ukulele弹奏模拟器v1.0(待完善)
写在前面 最近听beyond乐队的<灰色轨迹>听上瘾了,300多遍,震惊!!尤其喜欢最后一分半钟的吉他solo,真可谓吉他没有酒,依然让我醉如老狗.. 翻了翻网上的视频,瞬间觉得单身20年 ...
- 大div套多个小div,怎样设置外div的高度自适应?
在最后一个div 后面加上 overflow:hidden;如下: <div style="width:580px; height:auto; margin:0 auto; <d ...
- 使用Spring容器最简单的代码姿势
如果仅仅是为了测试简单使用一下Spring的IOC容器,或者研究一下Spring的容器的源码实现,那么在搭建Spring工程的时候,不需要复杂的xml配置.Spring3.0之后提供了Java注解配置 ...
- (1)PyCharm开发工具安装Flask并创建helloworld程序
一.环境描述 1.操作系统:windows7 2.编程语言:python3.6 下载地址:https://www.python.org/downloads/windows/ 3.虚拟化环境:virtu ...
- MSYS2 更新源
博客转载自:https://blog.csdn.net/puputaoexin/article/details/81780492 在使用msys2下载文件的时候回出现下载速度奇慢,且经常因为各种原因报 ...
- Ehcache API的使用和注意点
目录 创建CacheManager CacheManager常用的API 创建Cache Cache常用的API 创建Element Element常用的API 配置文件 配置文件名为ehcache. ...
- Bladex使用代码生成器操作步骤
一.从私服上下载BladeX和Saber 二.运行BladeX所有服务 三.运行Saber 四.数据库创建自己需要使用的表(建议表名和字段名为:bldex_xxxx,xxx_xxxx,不要使用驼峰命名 ...
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...