Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I attach a SOAP Header to my C# client?

I have my web service set up to recieve a soap header of name "TestHeader" with param "Name"

How do i create a soap header in my client AND send it to the service?

So far I have created it in my client.

public class TestHeader : SoapHeader
{
    public String Name;
}

Initialised my service,

    Test.TestServicesClient SOAP = new Test.TestServicesClient();

Initialised my header.

  TestHeader header = new TestHeader();

set variable in header

header.Name = "BoB";

Now what? Ive tried following MSDN, and other tutorials but not got anywhere.

TestService.cs

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace Test
{

    // Define a SOAP header by deriving from the SoapHeader class.
    public class TestHeader : SoapHeader
    {
        public String Name;
    }

   public class TestService : ITestServices
    {      

        public TestHeader TestInHeader;


       [WebMethod]
        [SoapHeader("TestInHeader", Direction = SoapHeaderDirection.In)]
        public List<ServiceDetails> GetServiceDetails(Int32 CostCentreNo, Int32 ServiceCode, Boolean Recurring)
        {
            throw new System.Exception(TestInHeader.ToString());

        }
    }
}
like image 494
IAmGroot Avatar asked Sep 26 '11 11:09

IAmGroot


People also ask

What is the requirement of SOAP header?

There is only one SOAP header section in a SOAP request. If the SOAP header element is present, it must be the first child element of the envelope element. SOAP headers can be input, output, or input and output, and you do not need to specify them in the WSDL file.


1 Answers

I guess I'm a little late, but here's the answer:

    Test.TestHeader header = new Test.TestHeader();
    header.Name = "BoB";
    Test.TestService SOAP = new Test.TestService();
    SOAP.TestHeaderValue = header;
    SOAP.GetServiceDetails(0,0,False);

Here's a LINK that clarifies the subject: "...Visual Studio will create a property in web service proxy called 'UserCredentialsValue' which will map the 'consumer' public property [which inherits from SoapHeader] in the web service."

like image 89
BKM Avatar answered Oct 01 '22 14:10

BKM