我有一个 Web api,我必须使用 Ninject 来进行 DI。我按照以下教程中的步骤进行操作,但仍然无法使其正常工作。我找到的大多数解决方案都是针对旧的 ASP.Net,在 ASP.Net Core 中不起作用。我知道 MVC 项目和 Web-API 项目之间存在差异。启动public class Startup { private readonly AsyncLocal<Scope> scopeProvider = new AsyncLocal<Scope>(); public IKernel Kernel { get; set; } private object Resolve(Type type) => Kernel.Get(type); private object RequestScope(IContext context) => scopeProvider.Value; public Startup(Microsoft.Extensions.Configuration.IConfiguration configuration) { Configuration = configuration; } public Microsoft.Extensions.Configuration.IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddRequestScopingMiddleware(() => scopeProvider.Value = new Scope()); services.AddCustomControllerActivation(Resolve); services.AddCustomViewComponentActivation(Resolve); services.AddMvc(); }
1 回答
吃鸡游戏
TA贡献1829条经验 获得超7个赞
根据文档, Ninject 不再支持字段注入。使用公共设置器将您的字段转换为财产,您应该可以开始了
[Inject] public IGPSProcessor Processor { private get; set; }
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报
0/150
提交
取消