如何在 wcf 中添加自定义肥皂头? [英] How to add custom soap headers in wcf?

查看:19
本文介绍了如何在 wcf 中添加自定义肥皂头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 basicHttpBinding 的 WCF 传入/传出消息中添加自定义 SOAP 标头吗,就像我们可以在 ASMX Web 服务中添加自定义身份验证标头一样?应该可以使用 .net 2.0/1.1 Web 服务客户端(可通过 WSDL.EXE 工具访问)访问这些自定义 SOAP 标头.

Can I add Custom SOAP header in WCF incoming/outgoing messages in basicHttpBinding, like we can add custom authentication header in ASMX web services? Those custom SOAP header should be accessible using .net 2.0/1.1 web service clients (accessible by WSDL.EXE tool) .

推荐答案

查看 Codeplex 上的 WCF Extras- 这是一个简单的 WCF 扩展库,它提供了 - 除其他外 - 自定义 SOAP 标头.

Check out the WCF Extras on Codeplex - it's an easy extension library for WCF which offers - among other things - custom SOAP headers.

另一种选择是在 WCF 服务中使用 WCF 消息协定- 这也允许您轻松定义和设置 WCF SOAP 标头.

Another option is to use WCF message contracts in your WCF service - this also easily allows you to define and set WCF SOAP headers.

[MessageContract]
public class BankingTransaction
{
  [MessageHeader]
  public Operation operation;
  [MessageHeader] 
  public DateTime transactionDate;

  [MessageBodyMember] 
  private Account sourceAccount;
  [MessageBodyMember] 
  private Account targetAccount;
  [MessageBodyMember] 
  public int amount;
}

此处,操作"和事务日期"被定义为 SOAP 标头.

Here, the "operation" and the "transactionDate" are defined as SOAP headers.

如果这些方法都没有帮助,那么您应该查看 WCF 消息检查器的概念,您可以将其编写为扩展.它们允许您例如在客户端每次传出调用时将某些标头注入消息中,并从服务器上的消息中检索这些标头以供您使用.

If none of those methods help, then you should check out the concept of WCF Message Inspectors which you can write as extensions. They allow you to e.g. inject certain headers into the message on every outgoing call on the client, and retrieving those from the message on the server for your use.

请参阅此博客文章 通过 WCF 行为处理自定义 SOAP 标头,了解如何编写消息检查器以及如何将其包含在您的项目设置中.

See this blog post Handling custom SOAP headers via WCF Behaviors for a starting point on how to write a message inspector, and how to include it in your project setup.

客户端IClientMessageInspector定义了两个方法BeforeSendRequestAfterReceiveReply,而服务器端IDispatchMessageInspector有相反的方法,即 AfterReceiveRequestBeforeSendReply.

The client side IClientMessageInspector defines two methods BeforeSendRequest and AfterReceiveReply while the server side IDispatchMessageInspector has the opposite methods, i.e. AfterReceiveRequest and BeforeSendReply.

有了这个,您可以为通过线路的每条消息添加标头(或选择性地仅添加到少数).

With this, you could add headers to every message going across the wire (or selectively only to a few).

这是来自 IClientMessageInspector 实现器的一个片段,我们用来自动将区域设置信息(语言和文化信息)从客户端传输到服务器 - 应该让您了解如何开始:

Here's a snippet from a IClientMessageInspector implementor we use to automagically transmit the locale information (language and culture info) across from the clients to the server - should give you an idea how to get started:

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    International intlHeader = new International();
    intlHeader.Locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

    MessageHeader header = MessageHeader.CreateHeader(WSI18N.ElementNames.International, WSI18N.NamespaceURI, intlHeader);
    request.Headers.Add(header);

    return null;
}

在服务器端,您将检查这些标头是否存在,如果存在,则从 SOAP 信封中提取它们并使用它们.

On the server side, you'd then check for the presence of those headers, and if present, extract them from the SOAP envelope and use them.

更新:好吧,您的客户使用 .NET 2.0 并且使用 WCF - 好消息是,这应该仍然可以正常工作 - 请参阅此博客文章自定义 SOAP 标头:WCF和 ASMX 了解详情.您仍然可以使用服务器端的消息检查器来嗅探和提取 .NET 2.0 客户端发送的自定义标头.

UPDATE: okay, you're clients are on .NET 2.0 and not using WCF - good news is, this should still work just fine - see this blog post Custom SOAP Headers: WCF and ASMX for details. You could still use the message inspector on the server side to sniff and extract the custom headers being sent by your .NET 2.0 clients.

这篇关于如何在 wcf 中添加自定义肥皂头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆