0

I have a Windows Service (.NET 3.5/C#) which is connecting to a WCF service on a external server on the public internet.

Can I use that WCF connection to get my computer's external IP address? The WCF service is hosted inside IIS on our application server. Can I do anything with that to get my external IP address?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
pearcewg
  • 9,545
  • 21
  • 79
  • 125
  • 1
    sorry, who has the unknown IP? the Windows Service? – Davide Piras Sep 20 '11 at 22:50
  • my winforms developer says that he doesn't know how to accurately get a public facing IP, so I'm trying to help him. Specifically: the computer running the windows service, which is connecting to a central server hosting ASP.NET & WCF. – pearcewg Sep 20 '11 at 22:51
  • 1
    Do you want to get an external IP from within the windows service that sends a request? – Dmitry Sep 20 '11 at 23:13
  • yes. the service is installed on the machine(s) that talk to my central server, and I want them to be able to get their own IP address and send it to the server. – pearcewg Sep 20 '11 at 23:30

2 Answers2

2

Can I use that WCF connection to get my computer's external IP address?

If you want to get your external IP from within the service that sends a request than the answer is no. And it has nothing to do with WCF. Your computer may not even have an external IP because it may not be connected to internet directly. External IP may belong to the router that your computer is connected to. This external IP will be visible to the WCF service you connecting to. So this service can potentially let you know what external IP your request was coming from (if you add new 'MyIP' field in to the data contract).

Your machine   ->   Request                        ->   External WCF Service
Your machine   <-   Response(your IP is X.X.X.X)   <-   External WCF Service

Or you can scrap it from automation.whatismyip.com

Update based on your comment:

... and I want them to be able to get their own IP address and send it to the server.

They don't know their external IP addresses, but server will know so there is no need to send IPs.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • +1 for the automation.whatismyip.com as probably the easiest way to retrieve it, as in. string serverIP = new WebClient().DownloadString("http://automation.whatismyip.com/n09230945.asp"); – tonycoupland Sep 21 '11 at 07:42
1

if you are in .NET 3.5 or 4, in your WCF service use this code:

using System.ServiceModel;
using System.ServiceModel.Channels;

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
    prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;

source: Obtaining client IP address in WCF 3.0

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147