0

我想在来自客户端/用户的每个 Web 请求上将客户端地址标头(客户端的 IP 地址)添加到 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
}

我的界面如下所示:

[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
}

如果我有一个由 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和Agent Message Inspector中的一些博客,它们都对我没有真正的用处,或者我不太了解。

4

0 回答 0