microsoft / service-fabric

Service Fabric is a distributed systems platform for packaging, deploying, and managing stateless and stateful distributed applications and containers at large scale.
https://docs.microsoft.com/en-us/azure/service-fabric/
MIT License
3.03k stars 401 forks source link

Enumerating headers #821

Open aloneguid opened 6 years ago

aloneguid commented 6 years ago

I'm not quite sure why this interface doesn't expose a way to enumerate headers, this is something obvious I need to do:

namespace Microsoft.ServiceFabric.Services.Remoting.V2
{
   public interface IServiceRemotingRequestMessageHeader
   {
      int MethodId { get; set; }
      int InterfaceId { get; set; }
      [DataMember(Name = "InvocationId", IsRequired = false, Order = 3, EmitDefaultValue = false)]
      string InvocationId { get; set; }

      void AddHeader(string headerName, byte[] headerValue);
      bool TryGetHeaderValue(string headerName, out byte[] headerValue);
   }
}

Service Fabric default implementation uses a dictionary internally, therefore we should be able to expose it? Decompiled code:

namespace Microsoft.ServiceFabric.Services.Remoting.V2
{
  [DataContract(Name = "ServiceMessageHeaders", Namespace = "urn:ServiceFabric.Communication")]
  internal class ServiceRemotingRequestMessageHeader : IServiceRemotingRequestMessageHeader
  {
    internal const string CancellationHeaderName = "CancellationHeader";
    [DataMember(IsRequired = true, Name = "Headers", Order = 2)]
    private Dictionary<string, byte[]> headers;
Expecho commented 6 years ago

I am interested as well. I want to write a generic piece of code for adding custom headers and with this feature I can just iterate the custom headers. Any news on this? Should we create a PR?

mikkelhegn commented 6 years ago

@amanbha

suchiagicha commented 6 years ago

@aloneguid Could you explain more on your usecase for enumerating headers instead of using GetHeader Api?

@Expecho Here is sample on how to add custom headers . https://github.com/amanbha/servicefabric/blob/master/samples/ServiceCustomMessageHandler/Stateless1/CustomMessageHandler.cs

Expecho commented 6 years ago

@suchiagicha I know how to add them, that is not the problem.

But at runtime headers are added, and I need to read them. It is not a fixed set of headers and not known on beforehand how they are named. See here for an example of how headers are added and here how I now have to read them.

Or take a look at the docs.

suchiagicha commented 6 years ago

@Expecho I glanced at your code, you should not create ServiceProxyFactory for every proxy. You should cache it. Its an expensive object.

Second, you could make your CustomHeader class serializable and add that in SF headers. Header can be anything . It doesnt needs to be a string. Since it accept value as byte[]. This way you don't need to rely on our internal headers implementation.

Expecho commented 6 years ago

@suchiagicha thank you for your time and effort. I suppose using a serializable answer is the best solution in this case.

andrejbratukhin commented 5 years ago

So, there is no way to iterate headers yet?