Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 委托方法未在C中调用#_C#_.net_.net Core_Delegates_Xceed - Fatal编程技术网

C# 委托方法未在C中调用#

C# 委托方法未在C中调用#,c#,.net,.net-core,delegates,xceed,C#,.net,.net Core,Delegates,Xceed,我正在尝试调用一个代理来验证从FTP服务器传入的SSL证书 当我在OnCertificateReceived函数中放置调试点时,执行从未停止过,SSL证书也从未验证过 如果我在这里做错了什么,请有人指出。 class Program { var hostname = ''; var username = ''; var password = ''; public static void main() { new Program().XceedFtpWithSSL(); } void X

我正在尝试调用一个代理来验证从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());
    }
}

我猜你真的需要做点什么来让这个班打开一个连接。您是否尝试调用
TestConnection()
方法来验证连接是否可行?@Prasoon:您是说
OnCertificateReceived
从不执行?或者它执行后挂起?是的,它从不执行,我指的是Xceed的文档,其中他们首先初始化连接,然后调用委托来验证证书。文档说明它初始化一个新实例,而不是连接。我认为当您实际尝试使用
FtpFile
FtpFolder
执行某些操作时,可能会发生连接尝试。正如我前面所说的,调用
TestConnection()
方法,查看它是否导致执行
OnCertificateReceived
。Xceed文档中的证书验证示例使用
FtpClient
接口。您正在使用“文件系统”界面。文档没有说明当您使用“显式TLS”时,
FtpConnection::TestConnection
是否协商TLS,而是说它“检查连接是否可行”。这可能仅仅意味着打开一个套接字,甚至不发送/接收welcom消息。尝试实际执行一项操作,例如列出文件夹()中的所有文件。看看会发生什么。