This interface makes it easy to resolve the public ip v4 address used by the computer to access the internet.
public interface IPublicIpResolver
{
string Resolve();
}
The resolver tries to resolve the ip address via list aof given simple web services and returns the result of the first service with a valid formatted result.
For convenience reasons the default resolver uses the following services if no list of services is provided.
- https://ipinfo.io/ip
- https://checkip.amazonaws.com/"
- https://api.ipify.org
- https://icanhazip.com
- https://wtfismyip.com/text
The resolver returns the public ip v4 address formatted as string. e.g. 123.456.789.0 or throws an exception if the resolver failed. Use the TryResolve() extension method if you want to work around the error handling.
var ip = new DefaultPublicIpResolver().Resolve();
var resolver = new DefaultPublicIpResolver();
if (resolver.TryResolve(out var ip))
{
// use the ip address
}
var serviceUrls = new [] {"https://customIpServcie.com"};
var resolver = new DefaultPublicIpResolver(serviceUrls: serviceUrls);
var ip = resolver.Resolve();