Binding an IP address just works for the first time

后端 未结 4 895
天涯浪人
天涯浪人 2021-01-11 16:02

I want to make a web request from one of available IP addresses on server so I use this class:

public class UseIP
{
    public string IP { get; private set;          


        
4条回答
  •  感动是毒
    2021-01-11 16:50

    Problem may be with the delegate getting reset on each new request. Try below:

    //servicePoint.BindIPEndPointDelegate = null; // Clears all delegates first, for testing
    servicePoint.BindIPEndPointDelegate += delegate
        {
            var address = IPAddress.Parse(this.IP);
            return new IPEndPoint(address, 0);
        };
    

    Also as far as I know, the endpoints are cached so even clearing the delegate may not work in some cases and they may get reset regardless. You may unload/reload the app domain as the worst case scenario.

提交回复
热议问题