OwinStartup not firing
Asked Answered
M

22

402

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.

Magdeburg answered 25/11, 2013 at 21:45 Comment(6)
Do you have Microsoft.Owin.Host.Systemweb package installed in this application. Make sure this dll is also part of the bin folder?Approximate
That was it. Thanks so much. I think what happened is that I used Resharper to remove unused references at some point and it doesn't think that one is needed. If you want to put this as an answer I'll definitely mark it as the solution. Thanks for the help.Magdeburg
Jeff, ReSharper removed "unused" reference to Microsoft.Owin.Host.Systemweb - are you talking about "using" statement? In which file, Startup.cs?Dumpling
I was talking about the actual reference within the project itself. So the DLL got removed from the project essentially and I had to add it back. Expand the References folder in the project and see if Microsoft.Owin.Host.SystemWeb is listed or not.Magdeburg
I had exactly the same problem, resharper removed the reference to Microsoft.Owin.Host.SystemWeb. I fixed it by opening the Package Manager Console Window in Visual Studio and running the following command PM> Update-Package -reinstall Microsoft.Owin.Host.SystemWebFluoro
As an update to @JoeKing's coment above. PM Console wouldn't update as "Not found". I had to go for a reinstall. PM> Install-Package Microsoft.Owin.Host.SystemWebMonsour
A
869

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.

Approximate answered 25/11, 2013 at 22:39 Comment(8)
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)?Ambassadress
Trying to create an application from scratch, without using the MVC template. This ended 2 hours despair.Keirakeiser
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.Sheepfold
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!Hurling
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.Drome
Why is it that none of the OWIN NuGet packages require this one?Dendro
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.Traditional
I added the owin startup using the option in the context menu - and still have to do this? >:-(Froma
H
86

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.

Headlight answered 14/5, 2015 at 0:16 Comment(1)
Had the same issue when implementing an Owin startup class in an old mvc project. Switching the value did the trick!Cespitose
D
55

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.

Dartmoor answered 31/3, 2014 at 21:41 Comment(7)
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.Dumpling
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?Proudfoot
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.Dumpling
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.Every
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.Furze
regarding @Proudfoot 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.Furze
Another option is to restart the IIS. To restart IIS on command line, open terminal and type: iisresetNikolaos
C
27

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.

Crematorium answered 2/10, 2014 at 11:39 Comment(6)
this is exactly what's happening. Is there a solution to get Startup class to fire after debugger has attached?Wes
May i know how to throw exception in Configuration method?Jataka
Added this line to Startup.cs to prove that it was executed : System.IO.File.WriteAllText(@"c:\temp\startup.txt", "Started");Anear
@Jataka - Add this line after of before (doesn't matter) the ConifureAuth(app); line - throw new Exception("Hello");Calctufa
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)Saccharin
You can debug in any middleware, simply add System.Diagnostics.Debugger.Launch();Pamphylia
M
23

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
Murmur answered 11/5, 2016 at 8:37 Comment(4)
toggling the setting optimizeCompilations="false" works for meAksel
You save my day! optimizeCompilations="false" work for me.Comptom
optimizeCompilations saved my day. Thanks :)Their
#5-7 saved my careerKroeger
E
15

I had a similar issue to this and clearing Temporary ASP.NET Files fixed it. Hope this helps someone.

Electrolyse answered 8/12, 2014 at 18:11 Comment(4)
Just for reference, here's another answer that provides a little more detail about where these files are stored: #16137957Prent
I running IIS EXPRESS and window 8: delete in here: C:\Users\Your User Name\AppData\Local\Temp\Temporary ASP.NET Files\vsFurry
Thanks, I noticed this answer because I had an error before this one, about something locking a file in the Temporary ASP.NET Files folderPsoas
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.Williamson
C
12

I had the same problem. Microsoft.Owin.Host.SystemWeb package was installed but during the installation NuGet was not able to add the dll as a reference for some reason. Make sure your project has that reference. If not you can try to reinstall:

update-package Microsoft.Owin.Host.SystemWeb -reinstall

I had an error like below on reinstall but somehow it worked:

System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED))

Crossman answered 24/8, 2015 at 8:36 Comment(2)
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.Yonatan
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.Favian
S
5

I had same problem when I added Owin to an existing web project. I eventually found the problem was due to the following in the web.config file.

<assemblies>
  <remove assembly="*" />
  <add assembly="System.Web.Mvc" />
  <add assembly="System.Web.WebPages" />

   ...

</assemblies>

The remove assembly="*" was causing the problem. When I remove this line the Owin startup code ran. I eventually change it to the following and it worked perfectly

<assemblies>
  <remove assembly="*" />
  <add assembly="Microsoft.Owin.Host.SystemWeb" />
  <add assembly="System.Web.Mvc" />
  <add assembly="System.Web.WebPages" />
  <add assembly="System.Web.Helpers" />
...
</assemblies>
Singles answered 19/1, 2017 at 1:10 Comment(3)
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.Rattlepate
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.Metre
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>Serous
T
4

In my case this Microsoft.Owin.Host.SystemWeb package is present in the project.

But below two tags are not present in web.config.

<add key="owin:AutomaticAppStartup" value="true" />
<add key="owin:appStartup" value="namespace.className.methodName" />

after adding them it works smoothly.

Theiss answered 29/6, 2017 at 10:27 Comment(0)
V
3

I found the following article to be very helpful:

https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application#MinimalCodeSummary

In my case, I had to set the following before Owin authentication would work instead of windows authentication:

<system.web>   
    <authentication mode="None" />   
<system.web>
Vandalism answered 20/11, 2019 at 8:49 Comment(0)
F
2

In my case, the IIS application pool was not set to v4. It was v2.

Changed the AppPool to v4 and everything was okay.

Furry answered 6/11, 2015 at 3:14 Comment(0)
M
2

In my case, my web.config had

<authorization>
  <allow users="?" />
</authorization>

To force it to fall back to Owin, I needed it to be

<authorization>
  <deny users="*" />
</authorization>
Moselle answered 31/5, 2018 at 14:46 Comment(1)
For me there wasn't any allow users, but setting to deny fixed the problemaDogma
W
2

This worked for me:

add authentication mode="None"

<system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
      <authentication mode="None" /><!--Use OWIN-->
  </system.web>
Winterkill answered 28/1, 2020 at 15:57 Comment(0)
O
1

In my case, my website's output path is changed by somebody, the IIS Express even not load OWIN, and the setup class will not be hit of course. After I set the output path as "bin\", it works well.

Other answered 9/12, 2016 at 6:35 Comment(0)
E
1

I think what some people were trying to get to above is that if you want to programatically make your OWIN server "come to life", you'd be calling something like this:

using Microsoft.Owin.Hosting;

    IDisposable _server = WebApp.Start<StartupMethod>("http://+:5000"); 
              // Start Accepting HTTP via all interfaces on port 5000

Once you make this call, you will see the call to StartupMethod() fire in the debugger

Effable answered 11/8, 2018 at 8:16 Comment(0)
C
0

I am not sure if this will still help someone, but I've done all of the solutions above (and from some other posts) to no avail.

What fixed the issue on my end was to put a backslash to the end of RedirectUri value in the web.config (crazy, I know!). RedirectUri is a parameter in UseOpenIdConnectAuthentication.

So, instead of:

<add key="ida:RedirectUri" value="https://www.bogussite.com/home" />

Do this:

<add key="ida:RedirectUri" value="https://www.bogussite.com/home/" />

And updated the Reply URL on the Azure App Settings as well.

That somehow made the Startup to run as expected (probably cleared some cache), and the breakpoints are now firing.

FYI. I was modelling my code from here: https://github.com/microsoftgraph/aspnet-connect-sample

Clemenciaclemency answered 6/3, 2018 at 23:3 Comment(0)
P
0

After converting a class library to a Web Application Project, I ran into this and became stubborn. Turned out, in my .csProj file, I had this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <DebugType>pdbonly</DebugType>
  <Optimize>true</Optimize>
  <OutputPath>bin\Release\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>
  • thus building the various dll's into a subfolder of the bin-folder (which ifc. won't work). Solution was to change both text-contents for OutputPath to just bin\.
Penuche answered 9/4, 2018 at 9:11 Comment(0)
S
0

For me it was because they are not in the same namespace. After I remove my AppStart from "project.Startup.AppStart" and let they both Startup.cs and Startup.Auth.cs with "project.Startup" namespace, everything was back to work perfectly.

I hope it help!

Stuffed answered 17/4, 2018 at 2:52 Comment(0)
M
0

If you are seeing this issue with IIS hosting, but not when F5 debugging, try creating a new application in IIS.

This fixed it for me. (windows 10) In the end i deleted the "bad" IIS application and re-created an identical one with the same name.

Melisent answered 8/6, 2018 at 11:58 Comment(0)
M
0

In case you have multiple hosts using the same namespace in your solution, be sure to have them on a separate IISExpress port (and delete the .vs folder and restart vs).

Mannos answered 24/2, 2021 at 11:24 Comment(0)
R
-1

I messed around with a lot of the suggestions on this post.

I had the following but still could not land on a break point. Throwing an exception proved the code was being entered.

<appSettings>
...
  <add key="owin:AutomaticAppStartup" value="true" />
  <add key="owin:appStartup" value="SSOResource.Startup, SSOResource" />
...
</appSettings>

Finally out of desperation I looked at project->properties, and then under the WEB section I also checked the NATIVE CODE checkbox (ASP.NET should be already checked).

That finally fixed it for me.

Note : I am using Visual Studio 2017 Professional.

Ranunculaceous answered 30/3, 2017 at 17:16 Comment(0)
L
-3

First add the OWIN Auth Class and then enable OWIN:AutomaticAppStartup key in your web.config like Now it will fire

Lozada answered 25/2, 2016 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.