[英]Delegate method not invoked in C#
我正在尝试呼叫代表以验证从FTP服务器传入的SSL证书。
当我在OnCertificateReceived函数中放置一个调试点时,执行将永不停止 ,并且SSL证书也不会被验证。
有人可以指出我在这里做错了什么吗?
class Program
{
var hostname = '';
var username = '';
var password = '';
public static void main()
{
new Program().XceedFtpWithSSL();
}
void XceedFtpWithSSL()
{
connection = new FtpConnection(hostname,21,username,password,AuthenticationMethod.TlsAuto,VerificationFlags.None,null,DataChannelProtection.Private,false);
connection.CertificateReceived += new CertificateReceivedEventHandler( this.OnCertificateReceived );
}
// When I put debug point in this method, execution never stops
private void OnCertificateReceived(object sender, CertificateReceivedEventArgs e)
{
// The Status argument property tells you if the server certificate was accepted
// based on the VerificationFlags you provided.
if (e.Status != VerificationStatus.ValidCertificate)
{
Console.WriteLine("Do you want to accept this certificate anyway? [Y/N]");
int answer = Console.Read();
if ((answer == 'y') || (answer == 'Y'))
{
e.Action = VerificationAction.Accept;
}
}
else
{
Console.WriteLine("Valid certificate received from server.");
// e.Action's default value is VerificationAction.Accept
}
} // End of Delegate
}//End of class
然后,您将一个事件应用于委托,要使他执行defato,必须在FtpConnection中具有一些函数来执行此委托事件:Ex
public delegate void ExDelegate(string value);
class Program
{
static void Main(string[] args)
{
Connection connection = new Connection();
connection.ExDelegate += OnConnection_ExDelegate;
connection.Init();
}
public static void OnConnection_ExDelegate(string value)
{
Console.WriteLine(value);
Console.ReadLine();
}
}
public class Connection
{
public event ExDelegate ExDelegate;
public void Init()
{
Console.Write("Enter your name: ");
ExDelegate?.Invoke(Console.ReadLine());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.