skip to Main Content
public void Configuration(IAppBuilder app) 
{
    // code is executed 
}

public void ConfigureServices(IServiceCollection services) 
{
    // code is not executed  
}

3

Answers


  1. ConfigureServices(IServiceCollection services) method is only available in ASP.NET Core.
    this might help- ASP.NET Classic OWIN StartUp ConfigureServices not called

    Login or Signup to reply.
  2. Based on OWIN Startup Class Detection, you have to add the NuGet package Microsoft.Owin.Host.SystemWeb and then reference a OWIN Startup class from VisualStudio template, then in the following code you can access OWIN:

    [assembly: OwinStartup("ProductionConfiguration", typeof(StartupDemo.ProductionStartup2))]
    
    namespace StartupDemo
    {
        public class ProductionStartup
        {
            public void Configuration(IAppBuilder app)
            {
                app.Run(context =>
                {
                    string t = DateTime.Now.Millisecond.ToString();
                    return context.Response.WriteAsync(t + " Production OWIN App");
                });
            }
        }
        public class ProductionStartup2
        {
            public void Configuration(IAppBuilder app)
            {
                app.Run(context =>
                {
                    string t = DateTime.Now.Millisecond.ToString();
                    return context.Response.WriteAsync(t + " 2nd Production OWIN App");
                });
            }
        }
    }
    
    Login or Signup to reply.
  3. Use this

       public void Configuration(IAppBuilder app)
       {
            TimedBackgroundService timedBackground = new TimedBackgroundService();
            timedBackground.StartAsync(CancellationToken.None);
       }
    
    public class TimedBackgroundService : BackgroundService
     {
      private async Task ExecuteTaskAsync()
      {
        //code Here
      }
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
     {
        try
        {
            TimeSpan interval = TimeSpan.FromMinutes(1);
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(interval, stoppingToken);
                await ExecuteTaskAsync();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search