Ejemplo n.º 1
0
        /// <summary>
        /// Start messaging
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="to"></param>
        /// <param name="callbackUrl"></param>
        /// <param name="localUserDisplayName"></param>
        /// <param name="localUserUri"></param>
        /// <returns></returns>
        private async Task <IMessagingInvitation> StartMessagingWithIdentityAsync(string subject, string to, string callbackContext, string callbackUrl, string href, string localUserDisplayName, string localUserUri, LoggingContext loggingContext = null)
        {
            (Parent as Application).GetCallbackUrlAndCallbackContext(ref callbackUrl, ref callbackContext);

            string operationId = Guid.NewGuid().ToString();
            var    tcs         = new TaskCompletionSource <IInvitation>();

            HandleNewInviteOperationKickedOff(operationId, tcs);
            IInvitation invite = null;
            var         input  = new MessagingInvitationInput
            {
                OperationContext = operationId,
                To                   = to,
                Subject              = subject,
                CallbackContext      = callbackContext,
                CallbackUrl          = callbackUrl,
                LocalUserDisplayName = localUserDisplayName,
                LocalUserUri         = localUserUri
            };

            var startMessagingUri = UriHelper.CreateAbsoluteUri(this.BaseUri, href);

            await this.PostRelatedPlatformResourceAsync(startMessagingUri, input, new ResourceJsonMediaTypeFormatter(), loggingContext).ConfigureAwait(false);

            try
            {
                invite = await tcs.Task.TimeoutAfterAsync(WaitForEvents).ConfigureAwait(false);
            }
            catch (TimeoutException)
            {
                throw new RemotePlatformServiceException("Timeout to get incoming messaging invitation started event from platformservice!");
            }

            //We are sure the invite sure be there now.
            var result = invite as MessagingInvitation;

            if (result == null)
            {
                throw new RemotePlatformServiceException("Platformservice do not deliver a messageInvitation resource with operationId " + operationId);
            }

            return(result);
        }
        /// <summary>
        /// Establishes a <see cref="IMessagingInvitation"/>> as an asynchronous operation.
        /// </summary>
        /// <param name="loggingContext">The logging context.</param>
        /// <returns>Task&lt;TInvitation&gt;.</returns>
        /// <exception cref="Microsoft.SfB.PlatformService.SDK.Common.CapabilityNotAvailableException">Link to establish messaging is not available.</exception>
        /// <exception cref="System.Exception">
        /// [Messaging] Failed to get Conversation from messaging base parent
        /// or
        /// [Messaging] Failed to get communication from conversation base parent
        /// </exception>
        /// <exception cref="Microsoft.SfB.PlatformService.SDK.Common.RemotePlatformServiceException">
        /// Timeout to get incoming messaging invitation started event from platformservice!
        /// or
        /// Platformservice do not deliver a messageInvitation resource with operationId " + operationId
        /// </exception>
        public override async Task <IMessagingInvitation> EstablishAsync(LoggingContext loggingContext = null)
        {
            string href = PlatformResource?.AddMessagingLink?.Href;

            if (string.IsNullOrWhiteSpace(href))
            {
                throw new CapabilityNotAvailableException("Link to establish messaging is not available.");
            }

            Logger.Instance.Information(string.Format("[Messaging] calling AddMessaging. LoggingContext: {0}",
                                                      loggingContext == null ? string.Empty : loggingContext.ToString()));

            var conversation = base.Parent as Conversation;

            if (conversation == null)
            {
                Logger.Instance.Error("[Messaging] Conversation from messaging base parent is numll");
                throw new Exception("[Messaging] Failed to get Conversation from messaging base parent");
            }
            var communication = conversation.Parent as Communication;

            if (communication == null)
            {
                Logger.Instance.Error("[Messaging] communication from  conversation base parent is numll");
                throw new Exception("[Messaging] Failed to get communication from conversation base parent");
            }

            string operationId = Guid.NewGuid().ToString();
            TaskCompletionSource <IInvitation> tcs = new TaskCompletionSource <IInvitation>();

            //Tracking the incoming invitation from communication resource
            communication.HandleNewInviteOperationKickedOff(operationId, tcs);

            IInvitation invite = null;
            //It seems that this is not the expected input, please clarify the right input resource here.
            var input = new MessagingInvitationInput
            {
                OperationContext = operationId,
            };

            Uri addMessagingUri = UriHelper.CreateAbsoluteUri(this.BaseUri, href);

            await this.PostRelatedPlatformResourceAsync(addMessagingUri, input, new ResourceJsonMediaTypeFormatter(), loggingContext).ConfigureAwait(false);

            try
            {
                invite = await tcs.Task.TimeoutAfterAsync(WaitForEvents).ConfigureAwait(false);
            }
            catch (TimeoutException)
            {
                throw new RemotePlatformServiceException("Timeout to get incoming messaging invitation started event from platformservice!");
            }

            //We are sure the invite sure be there now.
            var result = invite as IMessagingInvitation;

            if (result == null)
            {
                throw new RemotePlatformServiceException("Platformservice do not deliver a messageInvitation resource with operationId " + operationId);
            }

            return(result);
        }