public void Connect(Action<PushRegistrationResponse> callback) { EventHandler<PushContextErrorEventArgs> errorHandler = null; errorHandler = (s, e) => { PushContext.Current.Error -= errorHandler; this.DispatchCallback(callback, new PushRegistrationResponse(false, e.Exception.Message)); }; PushContext.Current.Error += errorHandler; PushContext.Current.Connect( c => { PushContext.Current.Error -= errorHandler; var endpoint = new Endpoint { ApplicationId = this.applicationId, DeviceId = this.deviceId, ChannelUri = c.ChannelUri.ToString() }; this.PutEndpoint(endpoint, callback); }); }
private void PutEndpoint(Uri tileNavigationUri, Action<PushRegistrationResponse> callback) { var endpointIdentifier = tileNavigationUri ?? primaryTileEnpointIdentifier; if (this.notificationEndpoints.Contains(endpointIdentifier)) throw new InvalidOperationException("Navigation Uri already registered."); var endpoint = new Endpoint { ApplicationId = this.applicationId, ClientId = this.clientId, DeviceType = this.deviceType, ChannelUri = this.channelUri, TileId = tileNavigationUri != null ? tileNavigationUri.ToString() : string.Empty }; var request = this.ResolveRequest(this.endpointsServiceUri); request.Method = "PUT"; request.ContentType = "application/json"; request.Accept = "application/json"; byte[] body; using (var stream = new MemoryStream()) { var serializer = new DataContractJsonSerializer(typeof(Endpoint)); serializer.WriteObject(stream, endpoint); body = stream.ToArray(); } try { this.signRequestDelegate(request); request.BeginGetRequestStream( ar => { var postStream = request.EndGetRequestStream(ar); postStream.Write(body, 0, body.Length); postStream.Close(); request.BeginGetResponse( asyncResult => { try { var response = request.EndGetResponse(asyncResult) as HttpWebResponse; if ((response != null) && (response.StatusCode == HttpStatusCode.Accepted)) { this.DispatchCallback(callback, new PushRegistrationResponse(true, string.Empty)); this.notificationEndpoints.Add(endpointIdentifier); } else { this.DispatchCallback(callback, new PushRegistrationResponse(false, "The specified endpoint could not be registered in the Endpoints service.")); } } catch (WebException webException) { this.DispatchCallback(callback, new PushRegistrationResponse(false, Helpers.ParseWebException(webException))); } }, null); }, request); } catch (ArgumentNullException exception) { this.DispatchCallback(callback, new PushRegistrationResponse(false, exception.Message)); } catch (MessageSecurityException exception) { this.DispatchCallback(callback, new PushRegistrationResponse(false, exception.Message)); } }
private void PutEndpoint(Endpoint endpoint, Action<PushRegistrationResponse> callback) { var request = this.ResolveRequest(this.endpointsServiceUri); request.Method = "PUT"; request.ContentType = "application/json"; request.Accept = "application/json"; byte[] body; using (var stream = new MemoryStream()) { var serializer = new DataContractJsonSerializer(typeof(Endpoint)); serializer.WriteObject(stream, endpoint); body = stream.ToArray(); } try { this.signRequestDelegate(request); request.BeginGetRequestStream( ar => { var postStream = request.EndGetRequestStream(ar); postStream.Write(body, 0, body.Length); postStream.Close(); request.BeginGetResponse( asyncResult => { try { var response = request.EndGetResponse(asyncResult) as HttpWebResponse; if ((response != null) && (response.StatusCode == HttpStatusCode.Accepted)) { this.DispatchCallback(callback, new PushRegistrationResponse(true, string.Empty)); } else { this.DispatchCallback(callback, new PushRegistrationResponse(false, "The push notification channel coud not be registered in the Endpoints service.")); } } catch (WebException webException) { this.DispatchCallback(callback, new PushRegistrationResponse(false, Helpers.ParseWebException(webException))); } }, null); }, request); } catch (ArgumentNullException exception) { this.DispatchCallback(callback, new PushRegistrationResponse(false, exception.Message)); } catch (MessageSecurityException exception) { this.DispatchCallback(callback, new PushRegistrationResponse(false, exception.Message)); } }