0

I want to add the client address header (IP address of the client) to SOAP Header on each web request from client/user.

I use WCF services, and my implementation class is as follows:

public class Service : IService
{
    public Guid UserId()
    {
        if (MemUser != null)
            return (Guid)MemUser.ProviderUserKey;
        else
            return Guid.Empty;
    }
    public void SystemLog(Nullable<Guid> Customer, ResultCode Result, FacilityCode Facility, int Code, string Info)
    {
        // get IP address out of the SOAP header, if any
        string clientAddress = string.Empty;
        if ((ClientAddress != null) && !String.IsNullOrEmpty(ClientAddress.IPAddress))
            clientAddress = ClientAddress.IPAddress;
        else if ((HttpContext.Current != null) && (HttpContext.Current.Request != null) && !String.IsNullOrEmpty(HttpContext.Current.Request.UserHostAddress))
        clientAddress = HttpContext.Current.Request.UserHostAddress;

        SystemLog(Customer, Result, Facility, Code, Info, clientAddress); //writes into database
    }
//other methods
}

My interface looks like this:

[ServiceContract(Name = "ServiceSoap", Namespace = "http://www.example.com/webservice"), XmlSerializerFormat]
public interface IService
{
    [OperationContract(Action = "http://www.example.com/webservice/UserId")]
    Guid UserId();

    //other implementations
}

If i have a class for ClientAddressHeader inherited by SoapHeader like

public class ClientAddressHeader : SoapHeader
{
    public string IPAddress;
    public ClientAddressHeader(){}
}

I can easily pass the SOAP header (ClientAddress) in wse using [WebMethod] like:

[WebService(Namespace = "http://www.example.com/webservice")]
public class Service : System.Web.Services.WebService
{
    public ClientAddressHeader ClientAddress;

    [WebMethod, SoapHeader("ClientAddress", Direction = SoapHeaderDirection.In)] //client request
    public Guid UserId()
    {
        if (MemUser != null)
            return (Guid)MemUser.ProviderUserKey;
        else
            return Guid.Empty;
    }
}

So does anyone know how do I pass the SoapHeader when I use WCF??

I really appreciate if someone explain me clearly how to achieve this.

PS: I have also read some blogs in msdn and Agent Message Inspector none of them are really useful for me or i did not understand that very well.

Vinnie
  • 1,053
  • 11
  • 31

0 Answers0