简体   繁体   English

OperationContext.Current中的WCF MessageHeaders

[英]WCF MessageHeaders in OperationContext.Current

If I use code like this [just below] to add Message Headers to my OperationContext, will all future out-going messages contain that data on any new ClientProxy defined from the same "run" of my application? 如果我使用这样的代码[正好在下面]将Message Headers添加到我的OperationContext中,那么所有未来的外出消息都将包含从我的应用程序的相同“运行”定义的任何新ClientProxy上的数据吗?

The objective, is to pass a parameter or two to each OpeartionContract w/out messing with the signature of the OperationContract, since the parameters being passed will be consistant for all requests for a given run of my client application. 目标是将一个参数或两个参数传递给每个OpeartionContract,而不是弄乱OperationContract的签名,因为传递的参数对于我的客户端应用程序的给定运行的所有请求都是一致的。

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 like this? 换句话说,像这样重构上面的代码是否安全?

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 don't really understand what's happening there, I don't want to cargo cult it and just change it and let it fly if it works, I'd like to hear your thoughts on if it is OK or not. 因为我真的不明白那里发生了什么,所以我不想把它变成货物而只是改变它,如果它有效就让它飞起来,我想听听你的想法是否合适。

I think your refactored code doesn't put any added-value. 我认为你的重构代码没有任何附加价值。 Have you taken in account that the OperationContext can be null? 您是否考虑过OperationContext可以为null?

I think this will be a safer approach: 我认为这将是一种更安全的方法:

 using(OperationContextScope contextScope =
        new OperationContextScope(proxy.InnerChannel))
  {

      .....
      OperationContext.Current.OutgoingMessageHeaders.Add(untyped); 
      proxy.DoOperation(...); 
  }

OperationContextScope's constructor will always cause replacement of the Operation context of the current thread; OperationContextScope的构造函数将始终导致替换当前线程的Operation上下文; The OperationContextScope's Dispose method is called which restores the old context preventing problems with other objects on the same thread. 调用OperationContextScope的Dispose方法,该方法恢复旧上下文,防止同一线程上的其他对象出现问题。

I believe your OperationContext is going to get wiped each time you new the proxy. 我相信每次新建代理时,您的OperationContext都会被擦除。

You should plan on adding the custom message headers prior to each call. 您应该计划在每次调用之前添加自定义消息标头。 This is good practice in any case as you should prefer per call services and close the channel after each call. 在任何情况下,这都是很好的做法,因为您应该更喜欢每个呼叫服务并在每次呼叫后关闭频道。

There are a couple patterns for managing custom headers. 有几种模式可用于管理自定义标头。

  1. You can create the header as part of the constructor to the proxy. 您可以将头创建为代理的构造函数的一部分。

  2. Alternatively, you can extend the binding with a behavior that automatically adds the custom header prior to making each call. 或者,您可以使用在进行每次调用之前自动添加自定义标头的行为来扩展绑定。 This is a good example: http://weblogs.asp.net/avnerk... 这是一个很好的例子: http://weblogs.asp.net/avnerk ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何模拟OperationContext.Current(WCF消息) - How to mock OperationContext.Current (WCF Message) 当OperationContext.Current为null时访问WCF MessageHeader - Accessing a WCF MessageHeader when OperationContext.Current is null 我的第一个WCF服务器 - 为什么OperationContext.Current为null? - my first WCF Server - why OperationContext.Current is null? 访问WCF编码器中的传入消息属性(OperationContext.Current为null) - Get access to incoming message properties in WCF encoder (OperationContext.Current is null) 将其存储到静态变量时无法访问OperationContext.Current - I cannot reach OperationContext.Current when I store it to a static variable 为什么IAuthorizationPolicy.Evaluate()中的OperationContext.Current为null,而只有前两次? - Why is OperationContext.Current null in IAuthorizationPolicy.Evaluate() but only the first two times? WCF Windows服务中当前OperationContext为null - current OperationContext is null in WCF Windows Service WCF服务中的Mock OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name - Mock OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name in WCF service 如何将MessageHeaders注入WCF调用 - How to inject MessageHeaders into WCF calls 将OperationContext传播到异步WCF调用 - Propagate OperationContext into Async WCF Call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM