views:

37

answers:

0

I am trying to determine the client's IP address following this link: http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/. I have operationContracts of WebInvoke/Post and WebGet. The code works when the client request is a WebGet. But when the client request is a WebInvoke, I will get the WCF host IP. Any solution? Thanks.

Here is the interface

        [OperationContract]
        [WebGet(UriTemplate = RestTemplate.hello_get)]
        Stream hello_get();

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
        Stream hello_post();

        // Code for getting IP
        private string getClientIP()
        {
            //WebOperationContext webContext = WebOperationContext.Current;

            OperationContext context = OperationContext.Current;

            MessageProperties messageProperties = context.IncomingMessageProperties;

            RemoteEndpointMessageProperty endpointProperty =

                messageProperties[RemoteEndpointMessageProperty.Name]

                as RemoteEndpointMessageProperty;
            return endpointProperty.Address;
        }

    public Stream hello_get()
    {
        string ip = getClientIP();
        ...
    }

    public Stream hello_post()
    {
        string ip = getClientIP();
        ...
    }