1、新建一个静态类
public static class ServiceLocator { /// /// 服务提供程序,用于直接获取已注入的类 /// public static IServiceProvider Instance { get; set; } } 2、在Startup.cs中将生成器赋值给静态类属性 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { Models.ServiceLocator.Instance = app.ApplicationServices; } 3、操作类中通过静态类获取已注入的服务 var myRedis = ServiceLocator.Instance.GetService(typeof(MyRedis)) as MyRedis;