Why is UCMA telling me my session is not established when I add a contact?

Recently I found myself in the situation where I need to add a contact to a user using UCMA but did not care about what contacts currently existed. I did the standard plumbing to create a CollaborationPlatform and register the user, then added the contact with the following code.

userEndpoint.ContactGroupServices.EndAddContact(this.Subscriber.ContactGroupServices.BeginAddContact(contactUri, null, null));

To my surprise, this failed with the following error.

Failed to add contact: The operation can be performed only when the state of the session is established.

I spent some time scratching my head over this one, double checking that my session indeed was established, until I gave up and used my privileges at MSFT to debug into UCMA itself to see what was going on.

The issue is we do not support adding a contact without a subscription. At first you may wonder why this is the case. “But I don’t care what the current contacts are!” you may say. That may be true, but in order to publish a contact add request, we need a delta number so the server can sort out multiple requests. In order to receive this delta number you must subscribe. Fortunately, subscribing is quite easy – just use the BeginSubscribe and EndSubscribe methods on ContactGroupServices.

So if you happen to see this message, rest assured that there is nothing wrong with the session for your endpoint; it is the subscription session we are complaining about.