WCF-如何在每个Web请求上添加自定义SoapHeader?

WCF-如何在每个Web请求上添加自定义SoapHeader?,wcf,web-services,soap-client,soapheader,Wcf,Web Services,Soap Client,Soapheader,我想将客户机地址头(客户机的IP地址)添加到来自客户机/用户的每个web请求的SOAP头中 我使用WCF服务,我的实现类如下: public class Service : IService { public Guid UserId() { if (MemUser != null) return (Guid)MemUser.ProviderUserKey; else return Guid.Empty

我想将客户机地址头(客户机的IP地址)添加到来自客户机/用户的每个web请求的SOAP头中

我使用WCF服务,我的实现类如下:

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
}
如果我有一个由SoapHeader继承的ClientAddressHeader类,比如

public class ClientAddressHeader : SoapHeader
{
    public string IPAddress;
    public ClientAddressHeader(){}
}
我可以使用[WebMethod]轻松地在wse中传递SOAP头(ClientAddress),如:

[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;
    }
}
那么,当我使用WCF时,有人知道如何传递SoapHeader吗

如果有人向我解释清楚如何做到这一点,我真的很感激

PS:我也读过一些msdn中的博客,但没有一个对我真正有用,或者我不太了解这些内容。

检查可能的相关内容:
[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;
    }
}