コード例 #1
0
        /// <summary>
        /// Represents the worker method to send the callback registration request.
        /// </summary>
        /// <param name="windowsPhoneCallbackRegistrationRequest">The callback registration request that associates a callback URI and message with a subscription.</param>
        /// <returns>The response of the Windows Phone Callback Registration Request.</returns>
        public static WindowsPhoneCallbackRegistrationResponse RegisterCallback(WindowsPhoneCallbackRegistrationRequest windowsPhoneCallbackRegistrationRequest)
        {
            var windowsPhoneCallbackRegistrationResponse = new WindowsPhoneCallbackRegistrationResponse()
            {
                OperationStartUtcDateTime = DateTime.UtcNow,
            };

            try
            {
                windowsPhoneCallbackRegistrationResponse.ValidationResults = Validator.Validate(windowsPhoneCallbackRegistrationRequest);

                if (windowsPhoneCallbackRegistrationResponse.ValidationResults.IsValidWithWarnings)
                {
                    var windowsPhoneCallbackRegistrationWebRequest = (HttpWebRequest)WebRequest.Create(windowsPhoneCallbackRegistrationRequest.NotificationUri);

                    windowsPhoneCallbackRegistrationWebRequest.Headers.Add("X-CallbackURI", windowsPhoneCallbackRegistrationRequest.CallbackUri);
                    windowsPhoneCallbackRegistrationWebRequest.Method = "POST";

                    windowsPhoneCallbackRegistrationWebRequest.ContentType   = "application/*";
                    windowsPhoneCallbackRegistrationWebRequest.ContentLength = windowsPhoneCallbackRegistrationRequest.CallbackMessage.Length;

                    using (var notificationRequestStream = windowsPhoneCallbackRegistrationWebRequest.GetRequestStream())
                    {
                        notificationRequestStream.Write(windowsPhoneCallbackRegistrationRequest.CallbackMessage, 0, windowsPhoneCallbackRegistrationRequest.CallbackMessage.Length);
                    }

                    using (var windowsPhoneCallbackRegistrationWebResponse = (HttpWebResponse)windowsPhoneCallbackRegistrationWebRequest.GetResponse())
                    {
                        PushNotifier.SetCallbackRequestWebResponseValuesToResult(windowsPhoneCallbackRegistrationResponse, windowsPhoneCallbackRegistrationWebResponse);
                    }
                }
            }
            catch (Exception exception)
            {
                windowsPhoneCallbackRegistrationResponse.OperationResult = ResultType.Failed;
                windowsPhoneCallbackRegistrationResponse.RawException    = exception;

                var webException = exception as WebException;

                if (webException != null && webException.Response != null)
                {
                    var httpWebResponse = webException.Response as HttpWebResponse;

                    if (httpWebResponse != null)
                    {
                        PushNotifier.SetCallbackRequestWebResponseValuesToResult(windowsPhoneCallbackRegistrationResponse, httpWebResponse);
                    }
                }
            }
            finally
            {
                windowsPhoneCallbackRegistrationResponse.OperationEndUtcDateTime = DateTime.UtcNow;
            }

            return(windowsPhoneCallbackRegistrationResponse);
        }
        private static void AssertOperationResultValidationResultsMessageCodeOnWindowsPhoneCallbackRegistrationResponse(WindowsPhoneCallbackRegistrationResponse response, ResultType resultType, bool completelyValid, bool validWithWarnings, string code = null)
        {
            Assert.AreEqual(response.OperationResult, resultType);
            Assert.AreEqual(response.ValidationResults.IsCompletelyValid, completelyValid);
            Assert.AreEqual(response.ValidationResults.IsValidWithWarnings, validWithWarnings);

            if (!String.IsNullOrWhiteSpace(code))
            {
                Assert.IsTrue(response.ValidationResults.Any(vr => vr.MessageCode == code));
            }
        }
コード例 #3
0
        /// <summary>
        /// Sets the <see cref="HttpWebResponse"/> values of callback request into our <see cref="WindowsPhoneCallbackRegistrationResponse"/> object.
        /// </summary>
        /// <param name="windowsPhoneCallbackRegistrationResponse">The result of the Callback Request operation.</param>
        /// <param name="httpWebResponse">The web response of the Callback Request operation.</param>
        private static void SetCallbackRequestWebResponseValuesToResult(WindowsPhoneCallbackRegistrationResponse windowsPhoneCallbackRegistrationResponse, HttpWebResponse httpWebResponse)
        {
            windowsPhoneCallbackRegistrationResponse.HttpStatusCode = httpWebResponse.StatusCode;

            var subscriptionStatus     = httpWebResponse.Headers["X-SubscriptionStatus"];
            var deviceConnectionStatus = httpWebResponse.Headers["X-DeviceConnectionStatus"];

            if (!String.IsNullOrWhiteSpace(subscriptionStatus))
            {
                windowsPhoneCallbackRegistrationResponse.CallbackSubscriptionStatus = subscriptionStatus.ToEnum <SubscriptionStatus>();
            }

            if (!String.IsNullOrWhiteSpace(deviceConnectionStatus))
            {
                windowsPhoneCallbackRegistrationResponse.DeviceConnectionStatus = deviceConnectionStatus.ToEnum <DeviceConnectionStatus>();
            }
        }