Ricardo Pontual Ricardo Pontual - 3 months ago 16
C# Question

Is it possible from WCF client to get the service IP behind a load balancer?

I know some ways to get the client IP from a WCF service, but I'm not able to get the server IP.

I can make some changes in the service, so it would be able to return the IP address, but I'm trying to do this without modify the service.

My scenario:
I call a service by a dns, that runs in a load balance (e.g. services.company.com, that resolves to 192.168.0.1), so I cannot know which server is responding the request (192.168.0.10 or 192.168.0.11).

enter image description here

For log purpose, I would like to know which server processed my request.

I think that probably it's not possible, but who knows can anyone help me or give a clue?

Answer

As I thought, I could not find any way to get the IP from a server without make changes in server side. But I've tried to hit the goal without modifying all services, and I ended up with this approach:

https://www.iis.net/configreference/system.webserver/httpprotocol/customheaders

I added a custom header to my IIS Server, so when I consume a service, the server add a value to the header, that I can recover in client. Basically you can add a custom header in IIS Manager, or you can add to the configuration file, adding those lines to system.webServer node:

<httpProtocol>
    <customHeaders>
        <add name="X-ServerName" value="myServerNameOrIP" />
    </customHeaders>
</httpProtocol>

It can be done at server level or to some application in particular. This solved the issue.

Of course, I'm putting this only in internal servers, I don't want my external clients receiving this information.

Comments