最近公司在做一个前后分离的项目,后端运用.net core做Web API接口,之前也有做过,不过都是框架已经搭好了直接运用的,这次完全是自己从头开始做
1. .net core依注入
简单来说就是通过类的接口去调用具体实例的方法,好处在于降低耦合性,具体介绍
asp.net core 依赖注入
第一次使用不知道具体的配置,还以为直接在Controller构造函数传入调用就可以了。
public class AdminController : ControllerBase
private readonly IAccountBLL accountBLL;
public AdminController(IAccountBLL account)
this.accountBLL = account;
然鹅调用Controller里的方法就会类型一下的错误
ystem.InvalidOperationException: Unable to resolve service for type 'OrderSys.BLL.IAccountBLL' while attempting to activate 'OrderSysAdminApi.Controllers.AdminController'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
原因在于没有在 Startup.cs 中进行依赖注入的设置
在项目找到 Startup.cs文件 ---》ConfigureServices方法,进行配置,保存在启动就可以正常调用了
因为在DAL中也是运用依赖注入,所以DAL一起进行设置就可以了
services.AddScoped<OrderSys.DAL.IAccountDAL, OrderSys.DAL.AccountDAL>();
services.AddScoped<OrderSys.BLL.IAccountBLL, OrderSys.BLL.AccountBLL>();
WPF 依赖注入之 Microsoft.Extensions.DependencyInjection独立观察员 2023 年 1 月 8 日NuGet 包:添加业务需要的接口及实现类:在 App 中声明服务提供者对象,以及添加服务等:核心代码:/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial cla...
System.InvalidOperationException: Unable to resolve service for type 'lz.TXK.IServices.INewsarticlesService' while attempting to activate 'lz.TXK.Api.Controllers.NewsarticleController'.
系统无效操作异常:当试图激...
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.BLL;
using WebNetCore5_Img_St
System.NullReferenceException: Object reference not set to an instance of an object.at ZoomLaCMS.Controllers.FrontController.Content() in E:\Code2020\ZoomlaCMS-Net5@v8\ZoomLaCMS\Controllers\FrontContr...
InvalidOperationException: Unable to resolve service for type 'WebApplication3.Models.backtestContext' while attempting to activate 'WebApplication3.Controllers.SysUsersController'.
Microsoft.Extensi...
写这篇文章的心情:激动
Microsoft.Extensions.DependencyInjection在github上同样是开源的,它在dotnetcore里被广泛的使用,比起之前的autofac,unity来说,它可以说是个包裹,或者叫适配器,它自己提供了默认的DI实现,同时也支持第三方的IOC容器,在这段时间里使用了它,就想,这东西为什么被在dotnetcore里大放异彩?为什...
{"success":false,"code":500,"count":0,"data":null,"msg":"System.NullReferenceException:
Object reference not set to an instance of an object.\r\n
at Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()\r\n
at Micro
1、Unabletoresolveservicefortype :没有将服务加入依赖注入容器,将需要的服务注入容器即可,需要注意的是扩展IServiceCollection别忘记加入容器
2、Cannot consume scoped service 'xxx' from singleton 'yyy':发生此错误是因为加入服务时scoped类型与singletion的类型服务混在一起
项目使用了 Microsoft.Extensions.DependencyInjection 2.x 版本,遇到第2次请求时非常高的内存占用情况,于是作了调查,本文对 3.0 版本仍然适用。
先说结论,可以转到ServiceProvider章节,为了在性能与开销中获取平衡,Microsoft.Extensions.DependencyInjection在初次请求时使用反射实例化目标服务,再次请求...