【问题标题】:ASP.NET Classic OWIN StartUp ConfigureServices not called未调用 ASP.NET 经典 OWIN 启动 ConfigureServices
【发布时间】:2019-11-08 00:48:08
【问题描述】:

我正在编写一个 ASP.NET Classic WebAPI 应用程序。我已经实现了 OWIN StartUp 类,并且执行了 CaConfiguration 方法,但是没有执行 ConfigureServices 方法。我知道这适用于 DotNetCore。

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;


[assembly: OwinStartup(typeof(ClassicWebApi.Startup))]

namespace ClassicWebApi
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //CODE IS NOT EXECUTED
        }
        public void Configuration(IAppBuilder app)
        {
            //CODE IS EXECUTED
        }


    }
}

这只是 DotNetCore 的一个功能还是我需要在设置中包含另一个库?

【问题讨论】:

  • ConfigureServices(IServiceCollection services) 方法仅在 ASP.NET Core 中可用。为什么需要 ASP.NET Web API?
  • 我想使用依赖注入。特别是 AddDbContext 和 AddScoped

标签: asp.net owin


【解决方案1】:

我想使用依赖注入。具体来说 AddDbContext 和 AddScoped

内置依赖注入仅在 ASP.NET Core 中可用。您将需要使用第三方 IoC 容器,例如 -

【讨论】:

  • 我想这可能就是答案。我希望能够使用 Microsoft.Extensions.DependencyInjection 库。对于这个,我将继续使用 Autofac。感谢您的回复
  • 这不一定是真的。您可以将 Microsoft 依赖注入包与 .NET 框架一起使用
【解决方案2】:

您可以将Microsoft.Extensions.DependencyInjection 用于非核心项目。您只需从 Global.asax Application_Start 方法中调用 ConfigureServices(IServiceCollection services)

【讨论】:

  • 怎么样? Application_Start 没有对 IServiceCollection 或 Startup 实例的引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多