How to add custom soap headers in wcf?











up vote
26
down vote

favorite
14












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










share|improve this question
























  • updated my answer with info about message inspectors
    – marc_s
    Dec 29 '09 at 21:53










  • updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
    – marc_s
    Dec 29 '09 at 22:13










  • thanks marc_s, this looks good, i will try it out.
    – nRk
    Dec 29 '09 at 22:17















up vote
26
down vote

favorite
14












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










share|improve this question
























  • updated my answer with info about message inspectors
    – marc_s
    Dec 29 '09 at 21:53










  • updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
    – marc_s
    Dec 29 '09 at 22:13










  • thanks marc_s, this looks good, i will try it out.
    – nRk
    Dec 29 '09 at 22:17













up vote
26
down vote

favorite
14









up vote
26
down vote

favorite
14






14





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










share|improve this question















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







wcf wcf-client






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 '14 at 21:06









John Saunders

147k22202357




147k22202357










asked Dec 29 '09 at 18:47









nRk

59351747




59351747












  • updated my answer with info about message inspectors
    – marc_s
    Dec 29 '09 at 21:53










  • updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
    – marc_s
    Dec 29 '09 at 22:13










  • thanks marc_s, this looks good, i will try it out.
    – nRk
    Dec 29 '09 at 22:17


















  • updated my answer with info about message inspectors
    – marc_s
    Dec 29 '09 at 21:53










  • updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
    – marc_s
    Dec 29 '09 at 22:13










  • thanks marc_s, this looks good, i will try it out.
    – nRk
    Dec 29 '09 at 22:17
















updated my answer with info about message inspectors
– marc_s
Dec 29 '09 at 21:53




updated my answer with info about message inspectors
– marc_s
Dec 29 '09 at 21:53












updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
– marc_s
Dec 29 '09 at 22:13




updated my answer again with info on ASMX/WCF interoperability with regard to SOAP headesr
– marc_s
Dec 29 '09 at 22:13












thanks marc_s, this looks good, i will try it out.
– nRk
Dec 29 '09 at 22:17




thanks marc_s, this looks good, i will try it out.
– nRk
Dec 29 '09 at 22:17












2 Answers
2






active

oldest

votes

















up vote
26
down vote



accepted










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.






share|improve this answer























  • Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
    – nRk
    Dec 29 '09 at 21:45










  • yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
    – nRk
    Dec 29 '09 at 22:03


















up vote
0
down vote













This solution was simpler for me:



            var client = "Your Service Client"; 
using (var scope = new OperationContextScope(client.InnerChannel))
{
System.Xml.XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

XmlElement newChild = null;
newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
newChild.InnerText = "finance";
element.AppendChild(newChild);

newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
newChild.InnerText = "387";
element.AppendChild(newChild);

MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);

OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

var result = client.GetCorporations(new CorporationType { pageNo = 1 });
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1976217%2fhow-to-add-custom-soap-headers-in-wcf%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    26
    down vote



    accepted










    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.






    share|improve this answer























    • Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
      – nRk
      Dec 29 '09 at 21:45










    • yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
      – nRk
      Dec 29 '09 at 22:03















    up vote
    26
    down vote



    accepted










    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.






    share|improve this answer























    • Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
      – nRk
      Dec 29 '09 at 21:45










    • yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
      – nRk
      Dec 29 '09 at 22:03













    up vote
    26
    down vote



    accepted







    up vote
    26
    down vote



    accepted






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 29 '09 at 22:13

























    answered Dec 29 '09 at 21:15









    marc_s

    565k12610911243




    565k12610911243












    • Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
      – nRk
      Dec 29 '09 at 21:45










    • yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
      – nRk
      Dec 29 '09 at 22:03


















    • Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
      – nRk
      Dec 29 '09 at 21:45










    • yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
      – nRk
      Dec 29 '09 at 22:03
















    Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
    – nRk
    Dec 29 '09 at 21:45




    Thanks marc_s, I tried to us WCF Extras, I added "XmlSerializerFormat" attribute to Service Contract WCF Extras throw error while accessing Services. Is there any other solution available like WCF Extras? Can I add MessageContract as attribute to every methods as I added SoapHeader in ASMX services? My Requirement is that I need to send the some (same say username & password ) data for every call to WCF service. Is there any way to implement this one?
    – nRk
    Dec 29 '09 at 21:45












    yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
    – nRk
    Dec 29 '09 at 22:03




    yah marc_s, that looks great. but in the article mentioned the client should be in .net 3.0 or higher..but in my case, clients are in .net 2.0 & 1.1. Is it still possible to implement this one?
    – nRk
    Dec 29 '09 at 22:03












    up vote
    0
    down vote













    This solution was simpler for me:



                var client = "Your Service Client"; 
    using (var scope = new OperationContextScope(client.InnerChannel))
    {
    System.Xml.XmlDocument document = new XmlDocument();
    XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

    XmlElement newChild = null;
    newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    newChild.InnerText = "finance";
    element.AppendChild(newChild);

    newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    newChild.InnerText = "387";
    element.AppendChild(newChild);

    MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);

    OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

    var result = client.GetCorporations(new CorporationType { pageNo = 1 });
    }





    share|improve this answer

























      up vote
      0
      down vote













      This solution was simpler for me:



                  var client = "Your Service Client"; 
      using (var scope = new OperationContextScope(client.InnerChannel))
      {
      System.Xml.XmlDocument document = new XmlDocument();
      XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

      XmlElement newChild = null;
      newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
      newChild.InnerText = "finance";
      element.AppendChild(newChild);

      newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
      newChild.InnerText = "387";
      element.AppendChild(newChild);

      MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);

      OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

      var result = client.GetCorporations(new CorporationType { pageNo = 1 });
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This solution was simpler for me:



                    var client = "Your Service Client"; 
        using (var scope = new OperationContextScope(client.InnerChannel))
        {
        System.Xml.XmlDocument document = new XmlDocument();
        XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

        XmlElement newChild = null;
        newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        newChild.InnerText = "finance";
        element.AppendChild(newChild);

        newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        newChild.InnerText = "387";
        element.AppendChild(newChild);

        MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);

        OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

        var result = client.GetCorporations(new CorporationType { pageNo = 1 });
        }





        share|improve this answer












        This solution was simpler for me:



                    var client = "Your Service Client"; 
        using (var scope = new OperationContextScope(client.InnerChannel))
        {
        System.Xml.XmlDocument document = new XmlDocument();
        XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

        XmlElement newChild = null;
        newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        newChild.InnerText = "finance";
        element.AppendChild(newChild);

        newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        newChild.InnerText = "387";
        element.AppendChild(newChild);

        MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);

        OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

        var result = client.GetCorporations(new CorporationType { pageNo = 1 });
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 13:20









        B.Tekkan

        20125




        20125






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1976217%2fhow-to-add-custom-soap-headers-in-wcf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)