public Task <byte[]> RequestResponseAsync(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody) { // add custom header messageHeaders.AddHeader("TestHeader", Encoding.UTF8.GetBytes("TestHeaderValue")); return(_inner.RequestResponseAsync(messageHeaders, requestBody)); }
public static void AddHeaders(this ServiceRemotingMessageHeaders messageHeaders, IEnumerable <ServiceRequestHeader> headers) { foreach (var header in headers) { messageHeaders.AddHeader(header); } }
public void SendOneWay(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody) { // add custom header messageHeaders.AddHeader("TestHeader", Encoding.UTF8.GetBytes("TestHeaderValue")); _inner.SendOneWay(messageHeaders, requestBody); }
#pragma warning restore 0649 public ServiceRemotingMessageHeaders ToServiceMessageHeaders() { var remotingMessageHeaders = new ServiceRemotingMessageHeaders(); remotingMessageHeaders.AddHeader(ActorMessageHeaderName, this.Serialize()); return(remotingMessageHeaders); }
private static void AddHeaders(ServiceRemotingMessageHeaders headers) { byte[] value; if (!headers.TryGetHeaderValue(ServiceRemotingHeaders.Identity, out value)) { headers.AddHeader(ServiceRemotingHeaders.Identity, SerializationHelper.SerializeToBytes(ClaimsPrincipal.Current)); } }
private CustomServiceRequestHeader UpdateAndGetMessageHeaders(ServiceRemotingMessageHeaders messageHeaders) { var customHeader = ServiceRequestContext.Current?.GetCustomHeader(); if (customHeader != null) { messageHeaders.AddHeader(customHeader); } return(customHeader); }
public ServiceRemotingMessageHeaders ToServiceMessageHeaders() { if (_needsPackaging) { Pack(); } var remotingMessageHeaders = new ServiceRemotingMessageHeaders(); remotingMessageHeaders.AddHeader(CustomServiceRequestHeaderName, _bytes); return(remotingMessageHeaders); }
public static bool SetContextDto(this ServiceRemotingMessageHeaders headers, CustomContextDataDto contextData) { try { if (contextData != null) { string str = JsonConvert.SerializeObject(contextData); headers.AddHeader("contextdata-header", Encoding.UTF8.GetBytes(str)); return(true); } return(false); } catch (Exception ex) { return(false); } }
private CustomServiceRequestHeader UpdateAndGetMessageHeaders(ServiceRemotingMessageHeaders messageHeaders) { if ((ServiceRequestContext.Current != null) && (ServiceRequestContext.Current?.Headers?.Any() ?? false)) { messageHeaders.AddHeaders(ServiceRequestContext.Current.Headers); } else if (ServiceRequestContext.Current?[ServiceRequestContextKeys.CorrelationId] != null || ServiceRequestContext.Current?[ServiceRequestContextKeys.UserId] != null) { var header = new CustomServiceRequestHeader() .AddHeader(ServiceRequestContextKeys.CorrelationId, ServiceRequestContext.Current?[ServiceRequestContextKeys.CorrelationId]) .AddHeader(ServiceRequestContextKeys.UserId, ServiceRequestContext.Current?[ServiceRequestContextKeys.UserId]); messageHeaders.AddHeader(header); } var customServiceRequestHeader = messageHeaders.GetCustomServiceRequestHeader(Logger) ?? new CustomServiceRequestHeader(); return(customServiceRequestHeader); }
public static void Add(T value, ServiceRemotingMessageHeaders headers) { Debug.Assert(value != default(T)); Debug.Assert(headers != null); byte[] bytes = null; if (headers.TryGetHeaderValue(TypeName, out bytes)) { throw new FabricElementAlreadyExistsException("A header with name " + TypeName + " and namespace " + TypeNamespace + " already exists in the message."); } MemoryStream writer = new MemoryStream(); DataContractSerializer serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(writer, value); writer.Close(); bytes = writer.ToArray(); headers.AddHeader(TypeName, bytes); }
internal override void OnAddHeaders(MessageHeaders messageHeaders) { if (AddHeaders != null) { ServiceRemotingMessageHeaders headers = Header <ServiceRemotingMessageHeaders> .Get(messageHeaders); AddHeaders(new HeaderAccessor(headers)); Header <ServiceRemotingMessageHeaders> .Replace(headers, messageHeaders); } else { if (Accessor != null) { ServiceRemotingMessageHeaders headers = Header <ServiceRemotingMessageHeaders> .Get(messageHeaders); foreach (KeyValuePair <string, byte[]> header in (Accessor as HeaderAccessor).Headers.Headers()) { headers.AddHeader(header.Key, header.Value); } Header <ServiceRemotingMessageHeaders> .Replace(headers, messageHeaders); } } }
public static void AddHeader(this ServiceRemotingMessageHeaders messageHeaders, ServiceRequestHeader header) { messageHeaders.AddHeader(header.HeaderName, header.GetValue()); }
public Task <byte[]> RequestResponseAsync(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody) { messageHeaders.AddHeader(HeaderIdentifiers.TraceId, Encoding.ASCII.GetBytes(traceId)); return(Client.RequestResponseAsync(messageHeaders, requestBody)); }
internal async Task <byte[]> InvokeAsync( ServiceRemotingMessageHeaders headers, byte[] requestMsgBody, CancellationToken cancellationToken) { if (!cancellationToken.CanBeCanceled) { return(await this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), cancellationToken)); } // // Remote calls that can be canceled need to be identifiable. So set the call context if // the higher layer hasn't already set one. // if (headers.InvocationId == null) { headers.InvocationId = Guid.NewGuid().ToString(); } // // Create a TaskCompletionSource that completes with false on cancellation. // var tcs = new TaskCompletionSource <bool>(); cancellationToken.Register(() => tcs.TrySetResult(false)); var innerTask = this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), cancellationToken); var completedTask = await Task.WhenAny(innerTask, tcs.Task); if (completedTask != innerTask) { // Task has been canceled. if (cancellationToken.IsCancellationRequested) { // // Invoke the cancellation logic. // Adding a cancellation header indicates that the request that was sent with // for the interface, method and identified by the call-context should be canceled. // ServiceTrace.Source.WriteInfo( TraceType, "Cancellation requested for CallContext : {0}, MethodId : {1}, InterfaceId : {2}", headers.InvocationId, headers.MethodId, headers.InterfaceId); headers.AddHeader(ServiceRemotingMessageHeaders.CancellationHeaderName, new byte[0]); // Cancellation token is not sent in this call that means that cancellation *will* be // delivered. await this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), CancellationToken.None); ServiceTrace.Source.WriteInfo( TraceType, "Cancellation delivered for CallContext : {0}, MethodId : {1}, InterfaceId : {2}", headers.InvocationId, headers.MethodId, headers.InterfaceId); } } tcs.TrySetResult(true); return(await innerTask); }
public void SendOneWay(ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody) { messageHeaders.AddHeader(HeaderIdentifiers.TraceId, Encoding.ASCII.GetBytes(traceId)); Client.SendOneWay(messageHeaders, requestBody); }
/// <summary> /// Sets the user header /// </summary> /// <param name="messageHeaders"></param> /// <param name="user"></param> public static void SetUser(this ServiceRemotingMessageHeaders messageHeaders, string user) { byte[] userHeader = Encoding.UTF8.GetBytes(user); messageHeaders.AddHeader(UserHeaderName, userHeader); }
internal async Task <byte[]> InvokeAsync( ServiceRemotingMessageHeaders headers, byte[] requestMsgBody, CancellationToken cancellationToken) { if (!cancellationToken.CanBeCanceled) { return(await this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), cancellationToken)); } // // Remote calls that can be canceled need to be identifiable. So set the call context if // the higher layer hasn't already set one. // if (headers.InvocationId == null) { headers.InvocationId = Guid.NewGuid().ToString(); } // // Create a TaskCompletionSource that completes with false on cancellation. // var tcs = new TaskCompletionSource <bool>(); // Using statement will make sure that we dispose registerationtoken which in turn un-register cancellationtoken. using (cancellationToken.Register(() => tcs.TrySetResult(false))) { var innerTask = this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), cancellationToken); var completedTask = await Task.WhenAny(innerTask, tcs.Task); if (completedTask != innerTask) { // Task has been canceled. if (cancellationToken.IsCancellationRequested) { // // Invoke the cancellation logic. // Adding a cancellation header indicates that the request that was sent with // for the interface, method and identified by the call-context should be canceled. // ServiceTrace.Source.WriteInfo( TraceType, "Cancellation requested for CallContext : {0}, MethodId : {1}, InterfaceId : {2}", headers.InvocationId, headers.MethodId, headers.InterfaceId); headers.AddHeader(ServiceRemotingMessageHeaders.CancellationHeaderName, new byte[0]); // // Under normal service operation, we want to make sure that cancellation message is // delivered to destination service. However if the destination service undergoes failover // while cancellation request is in progress, we would not want to retry the cancellation // after resolving to new primary as it is not valid for the new primary that will take over. // // Have a cancellation token that can used to cancel the cancellation task if needed. // var remoteCancellationTaskCts = new CancellationTokenSource(); var remoteCancellationTask = this.InvokeWithRetryAsync( client => client.RequestResponseAsync(headers, requestMsgBody), remoteCancellationTaskCts.Token); // // During failover, the actual remote request task retries to resolve to new primary and // before each retry, checks if the user has requested cancellation. // // To handle both normal and long/stuck failover scenario, wait for either of cancellation // task or actual request task to finish. // var finishedTask = await Task.WhenAny(innerTask, remoteCancellationTask); if (finishedTask != innerTask) { ServiceTrace.Source.WriteInfo( TraceType, "Cancellation delivered for CallContext : {0}, MethodId : {1}, InterfaceId : {2}", headers.InvocationId, headers.MethodId, headers.InterfaceId); } else { // // Actual task finished before cancellation task. // Cancel the cancellation task and observe exception if any. // remoteCancellationTaskCts.Cancel(); try { await remoteCancellationTask; } catch (Exception) { // Ignore. } } } } tcs.TrySetResult(true); return(await innerTask); } }
/// <summary> /// Sets the correlationId header /// </summary> /// <param name="messageHeaders"></param> /// <param name="correlationId"></param> public static void SetCorrelationId(this ServiceRemotingMessageHeaders messageHeaders, Guid correlationId) { byte[] correlationIdHeader = correlationId.ToByteArray(); messageHeaders.AddHeader(CorrelationIdHeaderName, correlationIdHeader); }
public static void SetActivityIdHeader(ServiceRemotingMessageHeaders headers) { string activityId = GetOrCreateActivityId(); headers.AddHeader(_activityIdKeyName, Encoding.UTF8.GetBytes(activityId)); }