WCF MessageHeaders in OperationContext.Current

If I use this code [just below] to add message headers to my OperationContext, will all future outgoing messages contain this data for any new ClientProxy defined from the same β€œlaunch” of my application?

The goal is to pass a parameter or two to each OpeartionContract without logging in with the OperationContract signature, since the parameters passed will be consistent for all requests for this launch of my client application.

public void DoSomeStuff() { var proxy = new MyServiceClient(); Guid myToken = Guid.NewGuid(); MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken); MessageHeader untyped = mhg.GetUntypedHeader("token", "ns"); OperationContext.Current.OutgoingMessageHeaders.Add(untyped); proxy.DoOperation(...); } public void DoSomeOTHERStuff() { var proxy = new MyServiceClient(); Guid myToken = Guid.NewGuid(); MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken); MessageHeader untyped = mhg.GetUntypedHeader("token", "ns"); OperationContext.Current.OutgoingMessageHeaders.Add(untyped); proxy.DoOtherOperation(...); } 

In other words, is it safe to refactor the above code in this way?

 bool isSetup = false; public void SetupMessageHeader() { if(isSetup) { return; } Guid myToken = Guid.NewGuid(); MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken); MessageHeader untyped = mhg.GetUntypedHeader("token", "ns"); OperationContext.Current.OutgoingMessageHeaders.Add(untyped); isSetup = true; } public void DoSomeStuff() { var proxy = new MyServiceClient(); SetupMessageHeader(); proxy.DoOperation(...); } public void DoSomeOTHERStuff() { var proxy = new MyServiceClient(); SetupMessageHeader(); proxy.DoOtherOperation(...); } 

Since I really do not understand what is happening there, I do not want the cargo to buy it, and just change it, and let it fly if it works, I would like to hear your thoughts if this is normal or not.

+4
source share
2 answers

I think your reorganized code does not add any added value. Did you consider that OperationContext can be null?

I think this will be a safer approach:

  using(OperationContextScope contextScope = new OperationContextScope(proxy.InnerChannel)) { ..... OperationContext.Current.OutgoingMessageHeaders.Add(untyped); proxy.DoOperation(...); } 

The OperationContextScope constructor always invokes a replacement operation context for the current thread; The OperationContextScope Dispose method is called, which restores the old context, preventing problems with other objects in the same thread.

+4
source

I believe that your OperationContext will be cleared every time you new proxy.

You should plan to add custom message headers before each call. This is good practice anyway, since you should prefer call services and close the channel after each call.

There are several templates for managing custom headers.

  • You can create a header as part of the constructor for the proxy.

  • Alternatively, you can extend the binding with a behavior that automatically adds a custom header before making each call. This is a good example: http: //weblogs.asp.net/avnerk ...

0
source

Source: https://habr.com/ru/post/1306383/


All Articles