Search code examples
c#office365exchangewebservicesmicrosoft365

EXO: Using EWS for streaming subscriptions stucks at third connection


I'm currently modifying a solution that uses streaming subscriptions to three room calendars implemented via PowerShell to work with Exchange Online. I reimplemented it in C# as it didn't seem to work in PowerShell (no notifications and no error at all).

I am using an app registration with impersonation permissions to access the calendars and create a FreeBusy-Changed streaming notification with one ExchangeService connection.

For the subscriptions I am using three different subscription connections (due to the fact that there are three different mailboxes). This is the code to setup each connection:

var folderIds = new List<FolderId>() { new FolderId(WellKnownFolderName.Calendar, mailbox) };
var subscriptionConnection = new StreamingSubscriptionConnection(exService, 10);
subscriptionConnection.OnNotificationEvent += SubscriptionConnection_OnNotificationEvent;
subscriptionConnection.OnSubscriptionError += SubscriptionConnection_OnSubscriptionError;
subscriptionConnection.OnDisconnect += SubscriptionConnection_OnDisconnect;
var subscription = exService.SubscribeToStreamingNotifications(folderIds, EventType.FreeBusyChanged);
subscriptionConnection.AddSubscription(subscription);
subscriptionConnection.Open();
_subscriptions.Add(subscriptionConnection);

This works for the first two subscriptions, but the third one just hangs there on method SubscribeToStreamingNotifications until the timeout (10mins) is reached.

It's not a problem of the mailbox itself, because if I changed the order I open the connections, it's still the third one.

I know that there is a limit of I think 20 concurrent connections, but these are only three, so what's the problem here? Any ideas?


Solution

  • The problem is that in .net by default its limited to 2 concurrent connections see https://learn.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.defaultconnectionlimit

    So you need to set the defaultconnectionlimit to value up to 27 concurrent connections (which is the EWS default concurrency limit in Office365). eg

    // Set the maximum number of connections per server to 27.  
    ServicePointManager.DefaultConnectionLimit = 27;