Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Classic OWIN StartUp ConfigureServices not called

Tags:

asp.net

owin

I am writing a ASP.NET Classic WebAPI application. I have implemented the OWIN StartUp class, and the Caonfiguration method is executed, however, the ConfigureServices method does not get executed. I know this works for 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
        }


    }
}

Is this only a feature of DotNetCore or do I need to include another library in the set up?

like image 655
franzke Avatar asked Jul 20 '17 18:07

franzke


People also ask

Where is ConfigureServices called?

At run time, the ConfigureServices method is called before the Configure method. This is so that you can register your custom service with the IoC container which you may use in the Configure method.

What is ConfigureServices in startup CS?

The ConfigureServices method is a public method on your Startup class that takes an IServiceCollection instance as a parameter and optionally returns an IServiceProvider . The ConfigureServices method is called before Configure .


1 Answers

I'd like to use the dependency injection. Specifically the AddDbContext and AddScoped

Build-in dependency injection is only available in ASP.NET Core. You will need to use third party IoC container such as -

  • Autofac for Web API
  • Ninject
like image 119
Win Avatar answered Oct 13 '22 21:10

Win