My program is a WCF service which publishes several methods and have multiple client. It store list of clients in the database. In some of the methods I need to query the caller's data from the database. This question How can service know the caller? and the linked answer Get the Client’s Address in WCF shows how to get the IP of the caller. However, normally the address field in my clients table don't have IP, but stuff like:
http://localhost:80/
http://computerName:80/
http://computerName.domain.com:80/
Which are valid endpoint addresses. Let's imagine I use the solution in the linked answer, and I get the IP of my caller (say http://192.80.212.21:80/
). However in the database, the client is stored as http://computerName:80/
How can I check that these two addresses are the same, so that I can get the corresponding client's entry from the database?
The number of clients is very small, so performance issue from iterating every row in the database can be negligible.
You'll have to do a DNS lookup to determine the host name that's associated the client's IP address:
var clientEndpoint = OperationContext.Current
.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
var clientHostName = Dns.GetHostEntry(clientEndpoint.Address).HostName;
var clientPort = clientEndpoint.Port;
var clientUri = new UriBuilder("http", clientHostName, clientPort).ToString();
At that point you can match the obtained clientUri
to the addresses stored in the database.