6

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());

        }
    }
}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154

2 Answers2

5

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

BKM
  • 51
  • 1
  • 2
2

Since the information you provide is not detailed enough only some general pointers:

The above links contains solutions including sample code for WCF (first link) and SOAP (second link)...

EDIT - as per comments:

This article (archived) gives you a complete walkthrough on implementing custom SOAP headers and setting them before calling the respective service.

Bascially you need to extend the Test.TestServicesClient class with the TestHeader you defined and then you can just set its value before calling a method of the web service.

Madis Otenurm
  • 61
  • 1
  • 7
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • following link 2, can you provide example code on how to connect to it in C# (i.e. creating and sending the soap header) – IAmGroot Sep 26 '11 at 12:12
  • see my EDIT above - it contains a link to a complete walktrhough with source etc. – Yahia Sep 26 '11 at 12:19
  • Since `Test.TestServicesClient` class is initialised as `WCF`, and `TestHeader` as `header`, it should be `WCF.TestHeader = header` or something? I cant seem to either initialise the proxy, (unless I already have, i.e. WCF) or find this option to set the header. – IAmGroot Sep 26 '11 at 13:17
  • you don't provide enough information... are you using WCF or are you using some generic SOAP implementation ? – Yahia Sep 26 '11 at 13:20
  • Generic Soap. It is merely called WCF because in the distant future it is suppose to support both. Sorry, it is confusing. I am merely using SOAP atm. Treat my service as the same as link 2. I just need to send this header to it. – IAmGroot Sep 26 '11 at 13:23
  • 1
    ok - please show the declaration of your client class or just add `TestHeader` as a public property to `Test.TestServicesClient`... – Yahia Sep 26 '11 at 13:25
  • that sounds like it would do the trick. But how do I do that :O – IAmGroot Sep 26 '11 at 13:30
  • to create an example: please edit your question and show the declaration of `Test.TestServicesClien`... – Yahia Sep 26 '11 at 14:22
  • This problem is so frustrating, taken me all day :( And i bet it seomthing simple, but i cant see what – IAmGroot Sep 26 '11 at 15:13
  • you put the server side class into the question... but your question was about extending your `TestServicesClient` to send a custom header... now I am really confused... what is your goal exactly ? – Yahia Sep 26 '11 at 16:18
  • Aye, I was hoping that an example of how to send a soap header to my service as shown, could be provided :). My client code is the above in bits, but is incomplete anyway (as it doesnt work). – IAmGroot Sep 27 '11 at 08:33