Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

wcf client - How to add custom soap headers in wcf?

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) .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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;
}

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

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.

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.

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).

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;
}

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...