none
ServicePoint.BindIPEndPointDelegate freeze http request RRS feed

  • Question

  • I have 2 IP addresses on my server. By default, first interface's ip uses in every application. But i want bind my c# program to another ip. I use code from this thread Choose one of many Internet connections for an application . All works correctly, but one moment. My simple http request by default ip is faster more than 50ms than binded. Why so? I suspect it because i use BindIPEndPointDelegate, but i dont understand why.

    UseIP ip = new UseIP("10.8.17.245");
    
    HttpWebRequest req = ip.CreateWebRequest(new Uri("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCUSDT"));
    using(WebResponse response = req.GetResponse()) {
     Stream dataStream = response.GetResponseStream();
     StreamReader reader = new StreamReader(dataStream);
     string responseFromServer = reader.ReadToEnd();
    }
    
    public class UseIP {
     public string IP {
      get;
      private set;
     }
    
     public UseIP(string IP) {
      this.IP = IP;
     }
    
     public HttpWebRequest CreateWebRequest(Uri uri) {
      ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
      servicePoint.BindIPEndPointDelegate = (servicePoint1, remoteEndPoint, retryCount) => {
       IPAddress address = IPAddress.Parse(this.IP);
       return new IPEndPoint(address, 0);
      };
    
      //Will cause bind to be called periodically
      servicePoint.ConnectionLeaseTimeout = 0;
    
      HttpWebRequest req = (HttpWebRequest) WebRequest.Create(uri);
      //will cause bind to be called for each request (as long as the consumer of the request doesn't set it back to true!
      req.KeepAlive = false;
    
      return req;
     }

    Tuesday, March 5, 2019 8:37 PM

All replies

  • I have 2 IP addresses on my server. By default, first interface's ip uses in every application. But i want bind my c# program to another ip. I use code from this thread Choose one of many Internet connections for an application . All works correctly, but one moment. My simple http request by default ip is faster more than 50ms than binded. Why so? I suspect it because i use BindIPEndPointDelegate, but i dont understand why.

    UseIP ip = new UseIP("10.8.17.245");
    
    HttpWebRequest req = ip.CreateWebRequest(new Uri("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCUSDT"));
    using(WebResponse response = req.GetResponse()) {
     Stream dataStream = response.GetResponseStream();
     StreamReader reader = new StreamReader(dataStream);
     string responseFromServer = reader.ReadToEnd();
    }
    
    public class UseIP {
     public string IP {
      get;
      private set;
     }
    
     public UseIP(string IP) {
      this.IP = IP;
     }
    
     public HttpWebRequest CreateWebRequest(Uri uri) {
      ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
      servicePoint.BindIPEndPointDelegate = (servicePoint1, remoteEndPoint, retryCount) => {
       IPAddress address = IPAddress.Parse(this.IP);
       return new IPEndPoint(address, 0);
      };
    
      //Will cause bind to be called periodically
      servicePoint.ConnectionLeaseTimeout = 0;
    
      HttpWebRequest req = (HttpWebRequest) WebRequest.Create(uri);
      //will cause bind to be called for each request (as long as the consumer of the request doesn't set it back to true!
      req.KeepAlive = false;
    
      return req;
     }

    Tuesday, March 5, 2019 8:38 PM
  • Hello Radzhab,

    This forum is Win32 C++ development focused.

    Since this issue is .Net API related I'll move it to .Net forum for more professional support.

    Best regards,

    Rita


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 6, 2019 2:02 AM
  • Hi Radzhab,

    As I know, when the WebRequest use the default ip address, it would ignores settings of the hosts file and always uses first ip address of the server. Hence it is faster than the other ip address.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 6, 2019 6:41 AM
    Moderator