public static void Publish(this HttpActionContext httpContext, IPublication publication) { using var _ = publication.Use(); var publicationState = publication.State; var computed = publicationState.Computed; var isConsistent = computed.IsConsistent(); // If exception occurred, response is empty, and we can not assign publication header. // If JsonifyAttribute is used, it will do it. var responseHeaders = httpContext.Response?.Headers; if (responseHeaders != null && responseHeaders.Contains(FusionHeaders.Publication)) { throw Errors.AlreadyPublished(); } var psi = new PublicationStateInfo(publication.Ref, computed.Version, isConsistent); var items = httpContext.GetItems(); items[typeof(IPublicationState)] = publicationState; items[typeof(PublicationStateInfo)] = psi; if (responseHeaders != null) { responseHeaders.AddPublicationStateInfoHeader(psi); } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); var headers = response.Headers; if (headers.TryGetValues(FusionHeaders.Publication, out var values)) { var header = values.FirstOrDefault(); if (!string.IsNullOrEmpty(header)) { var psi = JsonConvert.DeserializeObject <PublicationStateInfo>(header); if (response.StatusCode == HttpStatusCode.InternalServerError) { // Providing extended error info var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var error = new ApiException(request, response, content); psi = new PublicationStateInfo <object>(psi, Result.Error <object>(error)); } PublicationStateInfoCapture.TryCapture(psi); } } return(response); }
public static void Capture(PublicationStateInfo publicationStateInfo) { var current = CurrentLocal.Value; if (current != null) { current.Captured = publicationStateInfo; } }
public IReplica <T> GetOrAdd <T>(PublicationStateInfo <T> publicationStateInfo, bool requestUpdate = false) { var(replica, isNew) = ReplicaRegistry.Instance.GetOrRegister(publicationStateInfo.PublicationRef, () => new Replica <T>(this, publicationStateInfo, requestUpdate)); if (isNew) { Subscribe(replica); } return((IReplica <T>)replica); }
public static void Publish(this HttpContext httpContext, IPublication publication) { using var _ = publication.Use(); var state = publication.State; var computed = state.Computed; var isConsistent = computed.IsConsistent; var headers = httpContext.Response.Headers; if (headers.ContainsKey(FusionHeaders.Publication)) { throw Errors.AlreadyShared(); } var psi = new PublicationStateInfo(publication.Ref, computed.Version, isConsistent); headers[FusionHeaders.Publication] = JsonConvert.SerializeObject(psi); }
public static void Publish(this HttpContext httpContext, IPublication publication) { using var _ = publication.Use(); var publicationState = publication.State; var computed = publicationState.Computed; var isConsistent = computed.IsConsistent(); var responseHeaders = httpContext.Response.Headers; if (responseHeaders.ContainsKey(FusionHeaders.Publication)) { throw Errors.AlreadyPublished(); } var psi = new PublicationStateInfo(publication.Ref, computed.Version, isConsistent); httpContext.Items[typeof(IPublicationState)] = publicationState; httpContext.Items[typeof(PublicationStateInfo)] = psi; responseHeaders[FusionHeaders.Publication] = JsonConvert.SerializeObject(psi); }
// Private & internal methods internal static bool AddPublicationStateInfoHeader(this HttpResponseHeaders responseHeaders, PublicationStateInfo psi) { if (responseHeaders.Contains(FusionHeaders.Publication)) { return(false); } responseHeaders.Add(FusionHeaders.Publication, JsonConvert.SerializeObject(psi)); return(true); }
public void Capture(PublicationStateInfo publicationStateInfo) => Captured = publicationStateInfo;