How to Get Server IP Address in Scheduled Task

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hi,

I'm trying to getting server ip addres with
_httpContext.Request.ServerVariables["LOCAL_ADDR"]

It returns null value in scheduled task class.

Is there any other way to get server ip address?
8 years ago
Dumb question, but why do you want the IP?
8 years ago
Hi,
You can try to like these :
            HttpContext.Current.Request.UserHostAddress.ToString(),
            HttpContext.Current.Request.UserHostName.ToString(),
            HttpContext.Current.Request.Url.ToString() ,
            HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));


Thank you .
8 years ago
tony.wiredin wrote:
Dumb question, but why do you want the IP?


Because I have two server behind a F5 load balancer server. This two server doing a job/task sametime, for example mail sending. I created the business logic in order to make the two servers.

Thank you all. I have solved with following code.


IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress address in ipHostInfo.AddressList)
            {
                if (address.AddressFamily == AddressFamily.InterNetwork)
                    curServerIP = address.ToString();
            }
8 years ago
mtek wrote:
Hi,
You can try to like these :
            HttpContext.Current.Request.UserHostAddress.ToString(),
            HttpContext.Current.Request.UserHostName.ToString(),
            HttpContext.Current.Request.Url.ToString() ,
            HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));


Thank you .


Hi,

All
HttpContext.Current.Request.
objects are null in background tasks.

Thank you I solved this.
8 years ago
Hello,

Can you please tell how did you find way to get server IP address on scheduled task? My scheduled tasks work behind a load balancer and there are two servers. I want to find out which one is executing,

Thanks,
Berk
8 years ago
bereketbeyt wrote:
Hello,

Can you please tell how did you find way to get server IP address on scheduled task? My scheduled tasks work behind a load balancer and there are two servers. I want to find out which one is executing,

Thanks,
Berk


Hi Berk,

I'm using following code for same your scenario:


IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress address in ipHostInfo.AddressList)
            {
                if (address.AddressFamily == AddressFamily.InterNetwork)
                    curServerIP = address.ToString();
            }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.