Fixing the "The operation took too long" Error in O365

I was attempting to deploy a provider-hosted application to my O365 instance and it was failing with an extremely helpful message reading “The operation took too long”.  In the past, we’ve seen situations where a remote event receiver took too long to execute, but the message that comes back says something about the remote event receiver not returning a successful status.  Plus, my remote event receiver was never getting hit, so the error was occurring somewhere during the package deployment process, long before ever trying to call out to the remote event receivers.

In an attempt to figure out what was wrong, I took out the remote event receiver calls from the app manifest to verify those were not the issue.  When that solved nothing I started removing features from the package until I was left with, essentially, an empty app package.  Still didn’t work.

So I decided to clear my site collection (it’s a dev environment) and start from scratch.  One thing I like to do is delete the app principal entirely from O365 to make sure it’s fresh, but when I went to remove the app principal it was nowhere to be seen. 

Now, the thing that makes this especially odd is that I distinctly remember heading on over to the AppInv.aspx page, typing in my app’s client ID, and it successfully looked up that app principal.  And to confirm, I headed back over to that AppInv.aspx page and tried again.  Sure enough, even though there was no App Principal defined according to O365, that page pulled up my App Principal registration information without any problem.

One of the annoying things is that if this scenario played out in an on-premise SharePoint environment, the error that comes back is actually fairly helpful.  It essentially outright informs you that your app principal does not exist, but in O365 all you get is “The operation took too long”.  Technically, the error is accurate because it takes forever before it actually fails, but it’s far from helpful.

So the fix, at least in this specific scenario, is to register your App Principal again using AppRegNew.aspx, and then deploy the application again.