Search code examples
c#exchangewebservices

EWS notifications - can only subscribe to two mailboxes... unless using Fiddler


I'm writing a C# console application to connect to several Exchange mailboxes - mainly Microsoft 365 but a couple of on-prem ones too - and receive notifications when new messages arrive. I'm using the EWS API to do this.

Everything is working fine: I create a EWS connection per mailbox

  _exchangeService= new ExchangeService(ExchangeVersion.Exchange2013_SP1)
  {

      Url = new Uri($"{server.ServerAddress}/EWS/Exchange.asmx")
  };

.. and then subscribe to NewMail notifications:

  _subscription = _exchangeService.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
  var connection = new StreamingSubscriptionConnection(_exchangeService, 25);
  connection.AddSubscription(_subscription);
  connection.OnNotificationEvent += Connection_OnNotificationEvent;
  connection.OnDisconnect += Connection_OnDisconnect;
  connection.OnSubscriptionError += Connection_OnSubscriptionError;

  connection.Open();

This works great - notifications are received, everything works as expected against a single test mailbox. However, if I put this code into something that iterates through ALL the mailboxes I need to monitor, I can only get two subscriptions before the code grinds to a halt.

There's no exception, no sign of errors, but the code stops at:

_subscription = _exchangeService.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);

I've left it for several minutes, but it doesn't seem to move on. It's not specific to any one particular mailbox or Exchange account, or onprem vs 365, no matter which order I connect in, I can only get two subscriptions. If I don't create subscriptions, but do other EWS API stuff instead (e.g. FindItems), those work fine - it's only subscriptions that appear to be affected.

This doesn't just affect my development environment (Windows 11/ARM64) - I've deployed it to a production server (Windows Server 2019, x64), and it does the same thing there. This isn't the Max Subscription Limit Reached error - that throws a distinct exception, whereas this issue causes the code to sit and wait for something.

In an attempt to see if the code was even making the EWS request, I ran the code with Fiddler running (in "Streaming" mode) - and, incredibly, everything works fine! The subscriptions are created, events are received, and everything works as expected.

I cannot understand where the limit on subscriptions is coming from, or why running Fiddler fixes the issue. I'm presuming that Fiddler is installing itself as a system proxy and that somehow is affecting things, but it's obviously not a permanent solution for a production server.


Solution

  • Check the .net concurrent connection limit https://learn.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.defaultconnectionlimit?view=net-8.0&redirectedfrom=MSDN#System_Net_ServicePointManager_DefaultConnectionLimit (this is the most common error) eg if you haven't set then you won't be able to open more then 2 connections and you code will just hang.

    ServicePointManager.DefaultConnectionLimit = 50;