none
How to pass a MessageHeaderAttribute in OperationContext RRS feed

  • Question

  • I have a requirement to send attributes in the soap header request. My current code is creating xml elements instead of attributes.

    MessageHeader messageHeaderServiceContext = MessageHeader.CreateHeader(
                                "ServiceContext",
                                "http://context.core.datamodel.fs.documentum.emc.com/",
                                xmlobject);

    OperationContext.Current.OutgoingMessageHeaders.Add(messageHeaderServiceContext);

    <Profiles xmlns="http://schemas.datacontract.org/2004/07/"><AllowAsyncContentTransfer>false</AllowAsyncContentTransfer><AllowCachedContentTransfer>false</AllowCachedContentTransfer><TransferMode>MTOM</TransferMode></Profiles>

    I want AllowCachedContentTransfer, TransferMode and AllowAsyncContentTransfer to be attributes and not fields. Please help.


    Thursday, June 14, 2018 10:53 PM

Answers

  • Sorry for late reply. I got busy with some other stuff and got to look into the issue back again. 

    I got the fix for this.

    I just updated the web.config file where I have added the URL information. Included entire header section as it is into the <headers> tag i.e. 

    <system.serviceModel>

    <client>

    <endpoint address="https://hostservices/MyServices/ObjectServiceSoap?wsdl"
                    binding="basicHttpBinding"
                    bindingConfiguration="MyServiceSoapBinding"
                    contract="MyMService.ObjectServicePort"
                    name="MyService">

    <headers>

    <ServiceContext xmlns="http://context.core.datamodel.fs.documentum.emc.com/" xmlns:ns2="http://properties.core.datamodel.fs.documentum.emc.com/" xmlns:ns3="http://profiles.core.datamodel.fs.documentum.emc.com/">
             <Identities password="dfsXYZ1!" repositoryName="ecms_xyz_test" userName="xyz_web_service" xsi:type="RepositoryIdentity" />
             <Profiles allowAsyncContentTransfer="false" allowCachedContentTransfer="false" transferMode="MTOM" xsi:type="ns3:ContentTransferProfile" />
             <Profiles filterMode="ALL_NON_SYSTEM" xsi:type="ns3:PropertyProfile" />
          </ServiceContext>

    </headers>

    </client>

    </system.serviceModel>

    Friday, July 20, 2018 10:08 PM

All replies

  • I am trying to invoke a WSDL which has a method named create and it requires below headers with some attributes.

    <soap:Header>
          <ServiceContext xmlns="http://context.core.datamodel.fs.documentum.emc.com/" xmlns:ns2="http://properties.core.datamodel.fs.documentum.emc.com/" xmlns:ns3="http://profiles.core.datamodel.fs.documentum.emc.com/">
             <Identities password="dfsXYZ1!" repositoryName="ecms_xyz_test" userName="xyz_web_service" xsi:type="RepositoryIdentity" />
             <Profiles allowAsyncContentTransfer="false" allowCachedContentTransfer="false" transferMode="MTOM" xsi:type="ns3:ContentTransferProfile" />
             <Profiles filterMode="ALL_NON_SYSTEM" xsi:type="ns3:PropertyProfile" />
          </ServiceContext>
       </soap:Header>

    I am using .NET 4.5 C# and have added the WSDL as a Service reference. The problem is that above header classes are not present in the WSDL reference, hence I am unable to use it.

    I tried below code and was able to get the headers added but the service is throwing authentication failures. Also the values userName, password are displayed as xml fields and not attributes.

    <ServiceContext>

    <Identities>

    <Password>dfsXYZ1!</Password>....

    </Identities>

    </ServiceContext>

    I have used - 

    using (new OperationContextScope(obj.InnerChannel))
                    {

     //// Add a SOAP Header (Header property in the envelope) to an outgoing request. 
                            MessageHeader messageHeaderIdentities = MessageHeader.CreateHeader("Identities", 
                                "http://context.core.datamodel.fs.documentum.emc.com/", 
                                new Identities { Password = "dfsXYZ1!", RepositoryName = "ecms_xyz_test", UserName = "xyz_web_service" });
                            OperationContext.Current.OutgoingMessageHeaders.Add(messageHeaderIdentities);

                            MessageHeader messageHeaderProfiles = MessageHeader.CreateHeader("Profiles", 
                                "http://context.core.datamodel.fs.documentum.emc.com/",
                                new Profiles { AllowAsyncContentTransfer = false, AllowCachedContentTransfer = false, TransferMode = "MTOM" });
                            OperationContext.Current.OutgoingMessageHeaders.Add(messageHeaderProfiles);

    }

    Can anyone please let me know how this can be fixed ?

    Wednesday, June 13, 2018 9:12 PM
  • Can anyone please provide any inputs ?
    Thursday, June 14, 2018 1:48 PM
  • Hi Sameer,

    I suggest you try Message Formatter.

    #Custom Message Formatting in WCF to add all Namespaces to the SOAP Envelope

    https://weblog.west-wind.com/posts/2016/Apr/02/Custom-Message-Formatting-in-WCF-to-add-all-Namespaces-to-the-SOAP-Envelope

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 15, 2018 4:43 AM
  • I tried this but the attributes in header are created as fields and i want attributes.

    At present it is created as - 

    <Profiles><AllowAsyncContentTransfer>false</AllowAsyncContentTransfer><AllowCachedContentTransfer>false</AllowCachedContentTransfer><TransferMode>MTOM</TransferMode></Profiles>

    I want it as 

    <Profiles allowAsyncContentTransfer="false" allowCachedContentTransfer="false" transferMode="MTOM" xsi:type="ns3:ContentTransferProfile" />

    If I try to add it as string then it the '<' gets converted into &lt; &gt;

    Friday, June 15, 2018 7:35 PM
  • Hi Sameer,

    Could you share us a simple project with Message Formatter to add your attibute?

    We will try to reproduce your issue based on this project.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 18, 2018 5:41 AM
  • Expected SOAP Request - 

    <soap:Header>
      <ServiceContext xmlns="http://context.core.datamodel.com/" xmlns:ns2="http://properties.core.datamodel.com/" xmlns:ns3="http://profiles.core.datamodel.com/">
    <Identities password="myPassword" repositoryName="myRepository" userName="myUserName" xsi:type="RepositoryIdentity" />
    <Profiles allowAsyncContentTransfer="false" allowCachedContentTransfer="false" transferMode="MTOM" xsi:type="ns3:ContentTransferProfile" />
      </ServiceContext>
    </soap:Header>


    My Classes - 

    public class Identities
    {
    /// <summary>
    /// Gets or sets Password
    /// </summary>
    public string Password { get; set; }

    /// <summary>
    /// Gets or sets RepositoryName
    /// </summary>
    public string RepositoryName { get; set; }

    /// <summary>
    /// Gets or sets UserName
    /// </summary>
    public string UserName { get; set; }
    }

    public class Profiles
    {
    /// <summary>
    /// Gets or sets a value indicating whether AllowAsyncContentTransfer
    /// </summary>
    [XmlAttributeAttribute("allowAsyncContentTransfer")]
    public bool AllowAsyncContentTransfer { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether AllowCachedContentTransfer
    /// </summary>
    [XmlAttributeAttribute("allowCachedContentTransfer")]
    public bool AllowCachedContentTransfer { get; set; }

    /// <summary>
    /// Gets or sets TransferMode
    /// </summary>
    [XmlAttributeAttribute("transferMode")]
    public string TransferMode { get; set; }
    }

    My Code - Try 1 with string headers -> Here the <> are converted int &lt; and &gt;

    ObjectServicePortClient obj = new ObjectServicePortClient();

    using (new OperationContextScope(obj.InnerChannel))
    {
    string Header = "<Identities xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" password=\"" + "myPassword" + "\" repositoryName=\"" + "myRepository" + "\" userName=\"" + "myUserName" + "\" xsi:type=\"RepositoryIdentity\"/>";
    Header += "<Profiles xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" allowAsyncContentTransfer=\"false\" allowCachedContentTransfer=\"false\" transferMode=\"MTOM\" xsi:type=\"ns3:ContentTransferProfile\"/>";

    MessageHeader messageHeaderServiceContext = MessageHeader.CreateHeader(
    "ServiceContext",
    "http://context.core.datamodel.com/",
    Header);

    OperationContext.Current.OutgoingMessageHeaders.Add(messageHeaderServiceContext);

    response = await obj.createAsync(request);
    }

    My Code - Try 2 with GetHeaders - The HTTP request was forbidden with client authentication scheme 'Anonymous'.

    ObjectServicePortClient obj = new ObjectServicePortClient();

    using (new OperationContextScope(obj.InnerChannel))
    {
    MessageHeader messageHeaderServiceContext = MessageHeader.CreateHeader(
    "ServiceContext",
    "http://context.core.datamodel.fs.documentum.emc.com/",
    this.GetHeaders());

    OperationContext.Current.OutgoingMessageHeaders.Add(messageHeaderServiceContext);

    response = await obj.createAsync(request);
    }

    /// <summary>
    /// Gets soap request headers in xml format
    /// </summary>
    /// The xml
    private string GetHeaders()
    {
    XmlDocument doc = new XmlDocument();

    doc.AppendChild(doc.CreateElement("ServiceContext"));
    doc.DocumentElement.SetAttribute("xmlns", "http://context.core.datamodel.com/");
    doc.DocumentElement.SetAttribute("xmlns:ns2", "http://properties.core.datamodel.com/");
    doc.DocumentElement.SetAttribute("xmlns:ns3", "http://profiles.core.datamodel.com/");

    XmlElement xmlIdentities = (XmlElement)doc.CreateElement("Identities");
    xmlIdentities.SetAttribute("password", "myPassword");
    xmlIdentities.SetAttribute("repositoryName", "myRepository");
    xmlIdentities.SetAttribute("userName", "myUserName");
    xmlIdentities.SetAttribute("type", "RepositoryIdentity");
    xmlIdentities.RemoveAttribute("xmlns");
    doc.DocumentElement.AppendChild(xmlIdentities);

    XmlElement xmlProfiles = (XmlElement)doc.CreateElement("Profiles");
    xmlProfiles.SetAttribute("allowAsyncContentTransfer", "false");
    xmlProfiles.SetAttribute("allowCachedContentTransfer", "false");
    xmlProfiles.SetAttribute("transferMode", "MTOM");
    xmlProfiles.SetAttribute("type", "ns3:ContentTransferProfile");
    xmlProfiles.RemoveAttribute("xmlns");
    doc.DocumentElement.AppendChild(xmlProfiles);

    return doc.InnerXml.Replace("type", "xsi:type");
    }

    Monday, June 18, 2018 4:41 PM
  • Hi Sameer,

    For your current code, it uses "OperationContext.Current.OutgoingMessageHeaders"?

    Have you tried my previous suggestion to use Message Formatter?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 19, 2018 3:12 AM
  • I tried that by inheriting the Identies class by Message and MessageHeader. But the OnWriteHeaderContents method was not getting invoked.

    Do I need to add any attributes in the services references.cs file ?

    Wednesday, June 20, 2018 2:32 AM
  • I tried that by inheriting the Identies class by Message and MessageHeader. But the OnWriteHeaderContents method was not getting invoked.

    Do I need to add any attributes in the services references.cs file ?

    What is "OnWriteHeaderContents"?

    Do you implement Identity in WCF? Why do you inherite from Identity?

    Do you have any issue implementing Message Formatter?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 25, 2018 7:07 AM
  • Sorry for late reply. I got busy with some other stuff and got to look into the issue back again. 

    I got the fix for this.

    I just updated the web.config file where I have added the URL information. Included entire header section as it is into the <headers> tag i.e. 

    <system.serviceModel>

    <client>

    <endpoint address="https://hostservices/MyServices/ObjectServiceSoap?wsdl"
                    binding="basicHttpBinding"
                    bindingConfiguration="MyServiceSoapBinding"
                    contract="MyMService.ObjectServicePort"
                    name="MyService">

    <headers>

    <ServiceContext xmlns="http://context.core.datamodel.fs.documentum.emc.com/" xmlns:ns2="http://properties.core.datamodel.fs.documentum.emc.com/" xmlns:ns3="http://profiles.core.datamodel.fs.documentum.emc.com/">
             <Identities password="dfsXYZ1!" repositoryName="ecms_xyz_test" userName="xyz_web_service" xsi:type="RepositoryIdentity" />
             <Profiles allowAsyncContentTransfer="false" allowCachedContentTransfer="false" transferMode="MTOM" xsi:type="ns3:ContentTransferProfile" />
             <Profiles filterMode="ALL_NON_SYSTEM" xsi:type="ns3:PropertyProfile" />
          </ServiceContext>

    </headers>

    </client>

    </system.serviceModel>

    Friday, July 20, 2018 10:08 PM