是否可以重置ServicePointManager? [英] Is it possible to reset the ServicePointManager?

查看:176
本文介绍了是否可以重置ServicePointManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类似于 System.Net.Mail.SMTPClient如何进行本地IP绑定的代码, a>我在具有多个IP地址的机器上使用Windows 7和.Net 4.0。我已定义了 BindIPEndPointDelegate >

I am attempting to following the code similar to the one given at How does System.Net.Mail.SMTPClient do its local IP binding I am using Windows 7 and .Net 4.0 on a machine with multiple IP Addresses. I have the BindIPEndPointDelegate defined

private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    string IPAddr = //some logic to return a different IP each time
    return new IPEndPoint(IPAddr, 0);
}

然后我使用

SmtpClient client = new SmtpClient();
client.Host = SMTP_SERVER; //IP Address as string
client.Port = 25;
client.EnableSsl = false;
client.ServicePoint.BindIPEndPointDelegate 
   = new System.Net.BindIPEndPoint(BindIPEndPointCallback);
client.ServicePoint.ConnectionLeaseTimeout = 0;
client.Send(msg);  //msg is of type MailMessage properly initialized/set
client = null;

这个代码第一次被调用,代理被调用,任何IP地址被设置,它得到用过的。随后这段代码被调用,代表从不被称为 ,即。第一个IP地址随后被使用。 是否可以改变这种情况,每次代码被调用时,委托回调被调用?

The first time this code gets called, the delegate gets called and whatever IP address gets set, it gets used. The subsequent times this code gets called, the delegate never gets called i.e. the first IP Address is used subsequently. Is it possible to change this situation where each time the code gets called, the delegate callback is called?

我在想 ServicePointManager (这是一个静态类)将第一次调用的结果缓存到委托。是否可以重新设置此类?我不在乎表演。

I am thinking the ServicePointManager (which is a static class) caches the result of the first call to the delegate. Is it possible to reset this class? I don’t care about performance.

谢谢
OO

Thank you, O. O.

推荐答案

我遇到了一个类似的问题,想要重置 ServicePointManager 并改变certs以获得不同的测试结果。为我工作的方式是将MaxServicePointIdleTime设置为低值,这将有效地重置它。

I ran into a similar problem and wanted to reset ServicePointManager and changing certs for different outcomes of tests. The way that worked for me was to set MaxServicePointIdleTime to a low value, which would effectively reset it.

ServicePointManager.MaxServicePointIdleTime = 1;

这篇关于是否可以重置ServicePointManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆