OwinStartup not firing

114,646

Solution 1

Make sure you have installed Microsoft.Owin.Host.SystemWeb package in the project. This package is needed for startup detection in IIS hosted applications. For more information you can refer to this article.

Solution 2

If you've upgraded from an older MVC version make sure you don't have

  <add key="owin:AutomaticAppStartup" value="false" />

in your web.config. It will suppress calling the startup logic.

Instead change it to true

  <add key="owin:AutomaticAppStartup" value="true" />

I realize you already mentioned this but sometimes people (like me) don't read the whole question and just jump to the answers...

Somewhere along the line - when I upgraded to MVC 5 this got added and I never saw it until today.

Solution 3

Alternative answer to the original problem discussed - Owin "not firing." In my case I spent hours thinking it wasn't firing due to being unable to set a breakpoint in it.

When debugging OWIN startup in visual studio

  • IIS Express - Running "F5" will break on the OWIN startup code

  • IIS - Running "F5" will not break until after OWIN (and global.asax) code is loaded. If you attach to W3P.exe you will be able to step into it.

Solution 4

If you are having trouble debugging the code in the Startup class, I have also had this problem - or I thought I did. The code was firing but I believe it happens before the debugger has attached so you cannot set breakpoints on the code and see what is happening.

You can prove this by throwing an exception in the Configuration method of the Startup class.

Solution 5

DEBUGGING TIPS

If debugging does not work try using IIS Express or try the method below for local IIS

Using local IIS

For some reason this method enables debugging of this method:

  1. Request a webpage
  2. Attach to w3wp.exe process
  3. Touch the web.config file
  4. Request a webpage

Extra tip

Maybe doing this will flush a cache:

  1. In web.config add the optimizeCompilations attribute with a false value

    <compilation debug="true" ... optimizeCompilations="false">

  2. Run site

  3. Undo the change in web.config
Share:
114,646
Jeff Treuting
Author by

Jeff Treuting

Long time developer/consultant and co-creator of SharpRepository. Currently working at one of the best places for developers to call home, Fairway Technologies.

Updated on February 25, 2021

Comments

  • Jeff Treuting
    Jeff Treuting about 3 years

    I had the OwinStartup configuration code working perfectly and then it stopped working. Unfortunately I'm not sure exactly what I did to get it to stop working and am having a really hard time figuring it out.

    To make sure I have the basics covered, I doubled checked to make sure the I have the

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

    attribute assigned properly and made sure that I don't have an appSetting for owin:AutomaticAppStartup that is set to false so I made one set to true to be safe as there was nothing there before.

    <add key="owin:AutomaticAppStartup" value="true" />
    

    I also tried specifically calling out the appSetting:

    <add key="owin:appStartup" value="WebApplication.Startup" />
    

    Before it stopped working I upgraded the Microsoft.Owin.Security NuGet packages to 2.0.2, so I tried reverting them to 2.0.1 (that was a pain) but it didn't change anything. I have WebActivator installed on the project and am using that to bootstrap other things but I've tested that on a fresh WebApplication template and it works there so I don't think that is the culprit.

    I also tried removing my Startup class and using Visual Studio to add a new one using the OWIN Startup Class type in Add New Item and that isn't getting called either. Next I tried adding a second Startup class since I know it will throw an exception if there is more than one OwinStartup attributes defined, but it isn't throwing any exception there.

    Not sure what else to try. Any thoughts?

    Update

    Turns out that Resharper removed the reference to Microsoft.Owin.Host.SystemWeb when I used it to remove unused references.

  • vkelman
    vkelman over 9 years
    you are right! It's just Visual Studio 2013 debugger not stopping on a break point inside Startup class - while running under local IIS. Weird.
  • Sean
    Sean over 9 years
    Can you elaborate on the 'attach to W3P.exe'? I have the same issue of the breakpoint not being hit with IIS but is hit with IISExpress. I have stopped the application, attached to w3wp.exe and then browsed to my localhost but still it's not being hit. Have I missed something?
  • vkelman
    vkelman over 9 years
    I cannot attach to a running instance of my app either, although I believe I was able to do so before. I tried to open an app in a browser, then in VS2013 to use "Debug-> Attach to Process", and then to reload a page in a browser. VS didn't stop. I know that OWIN Startup is executing: I put some logging into it for debug purposes. It's like back in a dark age of intermediate debugging prints.
  • Jason Kleban
    Jason Kleban over 9 years
    How might someone kick off the Owin Startup Detection programmatically within a consumed/owin-encapsulating library, assuming we have a place to call it (PreApplicationStartMethod), without requiring these direct references of the Microsoft.Owin stuff? How do we specifically provoke Katana to call Startup.Configuration(IAppBuilder)?
  • mkvlrn
    mkvlrn over 9 years
    Trying to create an application from scratch, without using the MVC template. This ended 2 hours despair.
  • Sam Storie
    Sam Storie almost 9 years
    Just for reference, here's another answer that provides a little more detail about where these files are stored: stackoverflow.com/questions/16137457/…
  • Frank Myat Thu
    Frank Myat Thu over 8 years
    @Praburaj, Perfect answer. Thank
  • Ivan G.
    Ivan G. over 8 years
    Just when I was shouting resharper is the biggest productivity killer I had this problem and couldn't figure out for a week why web api stopped working. Another proof resharper is killing my productivity.
  • Grey Wolf
    Grey Wolf over 8 years
    I running IIS EXPRESS and window 8: delete in here: C:\Users\Your User Name\AppData\Local\Temp\Temporary ASP.NET Files\vs
  • Tom Schreck
    Tom Schreck about 8 years
    this is exactly what's happening. Is there a solution to get Startup class to fire after debugger has attached?
  • birken25
    birken25 about 8 years
    In my case, TFS decided this package wasn't important enough to remember. So when I tried rolling back, this package was mysteriously missing. Thanks for the answer!
  • Karthikeyan
    Karthikeyan almost 8 years
    May i know how to throw exception in Configuration method?
  • Elger Mensonides
    Elger Mensonides almost 8 years
    Thanks, I noticed this answer because I had an error before this one, about something locking a file in the Temporary ASP.NET Files folder
  • ps_ttf
    ps_ttf almost 8 years
    Requiring to reference a library that is not really used during compilation is a bad design by Microsoft! Resharper is doing his best but there is no weapon against such bad decisions.
  • RaoulRubin
    RaoulRubin almost 8 years
    Added this line to Startup.cs to prove that it was executed : System.IO.File.WriteAllText(@"c:\temp\startup.txt", "Started");
  • Darxtar
    Darxtar over 7 years
    Had the same issue when implementing an Owin startup class in an old mvc project. Switching the value did the trick!
  • Francis Rodgers
    Francis Rodgers over 7 years
    @Karthikeyan - Add this line after of before (doesn't matter) the ConifureAuth(app); line - throw new Exception("Hello");
  • Zackary Geers
    Zackary Geers over 7 years
    I'd been fighting this for a while, and this is what fixed my problem. I had the nuget reference in packages.config, but my csproj didn't have the reference.
  • Ryan
    Ryan over 7 years
    Wow. Thanks microsoft for telling us this in the documentation..........
  • Johann Marx
    Johann Marx over 7 years
    Lifesaver! I've been struggling with this for two days! Thank you x 1000!
  • dwbartz
    dwbartz over 7 years
    Why is it that none of the OWIN NuGet packages require this one?
  • barsh
    barsh about 7 years
    toggling the setting optimizeCompilations="false" works for me
  • Tobias
    Tobias about 7 years
    Amazing. Every time I add an OWIN startup file I have this problem. And every time I forget why, and end up at this answer.
  • Matt Bodily
    Matt Bodily about 7 years
    a co worker just showed me that in iis if you double click on the default app pool and change the managed pipeline mode to classic the debugger will break in startup. I ran after that and got an error saying the application had to run in Integrated so I had to change it back but was at least able to see that it was breaking there.
  • Prabo
    Prabo over 6 years
    Saved my day. Thanks
  • James Gray
    James Gray over 6 years
    Make the first line System.Threading.Sleep(10_000) so it will wait long enough for the debugger to attach before continuing (adjust as needed)
  • Vostrugin
    Vostrugin over 6 years
    You save my day! optimizeCompilations="false" work for me.
  • Bogdan Stojanovic
    Bogdan Stojanovic about 6 years
    optimizeCompilations saved my day. Thanks :)
  • burki
    burki almost 6 years
    I had a sitecore project and in my web.config file, there was no <remove assembly=*"/>tag. But adding <add assembly="Microsoft.Owin.Host.SystemWeb" /> fixed my problem.
  • Brett Caswell
    Brett Caswell over 5 years
    There are a couple of things here. "w3wp.exe" is based on application pool. you should be able to debug global.asax and owin if your application is not the first application you request on that starts up that process. i.e. request a different application in the application pool, "Attach to w3wp.exe", and then request the application your attempting to debug.
  • Brett Caswell
    Brett Caswell over 5 years
    regarding @Sean comment - I suspect you didn't actually stop the application, because I think your scenario "w3wp.exe" ought not have been running in order for you to attach to it. You would Recycle IIS for this scenario. The caveat to this is what I mentioned in the comment above: it is based on Application Pools.
  • J. Horn
    J. Horn over 5 years
    In my case, I didn't have the package installed at all. Running install-package Microsoft.Owin.Host.SystemWeb solved my issue. Thanks for the hint.
  • David
    David about 5 years
    Thanks @burki, because of your comment, I forced myself to try this solution and it did worked for us too! The Owin startup class wasn't firing since the update of VS to 15.9.9.
  • trykyn
    trykyn almost 5 years
    thank you so much! this fixed it, i had to add the follwing: <assemblies> <remove assembly="*" /> <add assembly="myapplication" /> <add assembly="Microsoft.Owin.Host.SystemWeb" /> <add assembly="Microsoft.Owin.Security" /> <add assembly="System.Web.Mvc" /> <add assembly="System.Web.WebPages" /> <add assembly="System.Web.Helpers" /> </assemblies>
  • Harsh Baid
    Harsh Baid almost 4 years
    I was struggling with this for 1 whole day and finally this worked for me. After clearing the Temporary ASP.NET Files folder content and Owin startup got invoked.
  • Eric Herlitz
    Eric Herlitz almost 3 years
    You can debug in any middleware, simply add System.Diagnostics.Debugger.Launch();
  • gschuster
    gschuster almost 3 years
    Another option is to restart the IIS. To restart IIS on command line, open terminal and type: iisreset
  • Sumer
    Sumer over 2 years
    For me there wasn't any allow users, but setting to deny fixed the problema