我有一個 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 關注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消